make_composite_from_td¶
- torchrl.envs.make_composite_from_td(data, *, unsqueeze_null_shapes: bool = True, dynamic_shape: bool = False)[源代码]¶
从 tensordict 创建 Composite 实例,假定所有值都是无界的。
- 参数:
data (tensordict.TensorDict) – 要映射到 Composite 的 tensordict。
- 关键字参数:
unsqueeze_null_shapes (bool, optional) – 如果为
True
,则所有空 shape 都将被 unsqueeze 到 (1,)。默认为True
。dynamic_shape (bool, optional) – 如果为
True
,则假定所有张量沿最后一个维度都具有动态 shape。默认为False
。
示例
>>> 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) Composite( obs: UnboundedContinuous( shape=torch.Size([3]), space=None, device=cpu, dtype=torch.float32, domain=continuous), action: UnboundedContinuous( shape=torch.Size([2]), space=None, device=cpu, dtype=torch.int32, domain=continuous), next: Composite( obs: UnboundedContinuous( shape=torch.Size([3]), space=None, device=cpu, dtype=torch.float32, domain=continuous), reward: UnboundedContinuous( 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()