Shortcuts

make_composite_from_td

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

Creates a CompositeSpec instance from a tensordict, assuming all values are unbounded.

Parameters:
  • data (tensordict.TensorDict) – a tensordict to be mapped onto a CompositeSpec.

  • unsqueeze_null_shapes (bool, optional) – if True, every empty shape will be unsqueezed to (1,). Defaults to True.

Examples

>>> 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()

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources