TensorDictBase.movedim#
- TensorDictBase.movedim(source: int | tuple[int, ...], destination: int | tuple[int, ...])#
Returns a tensordict with batch dimensions moved to new positions, as
torch.movedim()does.The batch dimensions that are not moved keep their order, at the positions that
destinationleaves free. The leaves are views of the original leaves, and the names move with the dimensions.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:
- Returns:
A tensordict with the batch dimensions moved.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict({"a": torch.zeros(3, 4, 5)}, batch_size=[3, 4]) >>> print(td.movedim(0, 1).shape) torch.Size([4, 3]) >>> print(td.movedim((0, 1), (1, 0)).shape) torch.Size([4, 3])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.