TensorDictBase.unflatten#
- TensorDictBase.unflatten(dim, unflattened_size, *, inplace: bool = False)#
Returns a tensordict whose batch dimension
dimis split into several.It works as
torch.unflatten()does on the batch. The leaves keep their feature dimensions, and are views of the original leaves when the memory layout allows it. The last new dimension keeps the name ofdim, and the others have no name.It can be used as a context manager, as
unsqueeze()can: when thewithblock exits, the entries of the block are written back to the tensordict with the inverse operation.- Parameters:
- Keyword Arguments:
inplace (bool, optional) – if
True, the tensordict keeps its identity and keys, its leaves are replaced one by one, and the call returnsself. A leaf keeps its storage when the unflatten can return a view of it. A lazy stack, or a tensordict that holds one, raises aNotImplementedError: callto_tensordict()first. See In-place operations. Defaults toFalse.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict({ ... "a": torch.arange(60).view(3, 4, 5), ... "b": torch.arange(12).view(3, 4)}, ... batch_size=[3, 4]) >>> td_flat = td.flatten(0, 1) >>> td_unflat = td_flat.unflatten(0, [3, 4]) >>> assert (td == td_unflat).all()
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.