TensorDictBase.flatten#
- TensorDictBase.flatten(start_dim: int | None = None, end_dim: int | None = None, *, inplace: bool = False)#
Returns a tensordict whose batch dimensions
start_dimtoend_dimare flattened into one.The leaves keep their feature dimensions. As for
torch.flatten(), a leaf of the result is a view of the original leaf when the memory layout allows it, and a copy otherwise. The flattened dimension has no name. A tensordict without batch dimensions gets one batch dimension of size 1.A
start_dimorend_dimoutside the batch dimensions warns, and is used on the dimensions of the leaves. Starting with TensorDict 0.17, the default will change to raising anIndexError, as for a tensor.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 flatten 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_flat.batch_size torch.Size([12]) >>> td_flat["a"].shape torch.Size([12, 5]) >>> td_flat["b"] tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.