快捷方式

make_composite_from_td

torchrl.envs.utils.make_composite_from_td(data, unsqueeze_null_shapes: bool = True)[源代码]

从 tensordict 创建 CompositeSpec 实例,假设所有值都是无界的。

参数:
  • data (tensordict.TensorDict) – 要映射到 CompositeSpec 的 tensordict。

  • unsqueeze_null_shapes (bool, 可选) – 如果为 True,则每个空形状将被展平为 (1,)。默认值为 True

示例

>>> from tensordict import TensorDict
>>> data = TensorDict({
...     "obs": torch.randn(3),
...     "action": torch.zeros(2, dtype=torch.int),
...     "next": {"obs": torch.randn(3), "reward": torch.randn(1)}
... }, [])
>>> spec = make_composite_from_td(data)
>>> print(spec)
CompositeSpec(
    obs: UnboundedContinuousTensorSpec(
         shape=torch.Size([3]), space=None, device=cpu, dtype=torch.float32, domain=continuous),
    action: UnboundedContinuousTensorSpec(
         shape=torch.Size([2]), space=None, device=cpu, dtype=torch.int32, domain=continuous),
    next: CompositeSpec(
        obs: UnboundedContinuousTensorSpec(
             shape=torch.Size([3]), space=None, device=cpu, dtype=torch.float32, domain=continuous),
        reward: UnboundedContinuousTensorSpec(
             shape=torch.Size([1]), space=ContinuousBox(low=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, contiguous=True), high=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, contiguous=True)), device=cpu, dtype=torch.float32, domain=continuous), device=cpu, shape=torch.Size([])), device=cpu, shape=torch.Size([]))
>>> assert (spec.zero() == data.zero_()).all()

文档

访问 PyTorch 的综合开发者文档

查看文档

教程

获取针对初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题的解答

查看资源