TensorDictBase.split#
- TensorDictBase.split(split_size: int | list[int], dim: int = 0) tuple[TensorDictBase, ...]#
Splits the tensordict along a batch dimension, as
torch.split()does.The pieces hold views of the original tensors, and keep the names.
On a
LazyStackedTensorDict, sizes along the stack dim that are negative or do not add up to the stack size emit aFutureWarning, and the pieces miss members or have a wrong batch size. Starting with TensorDict 0.17, the default will change to raising aRuntimeError, asTensorDict.splitdoes.- Parameters:
- Returns:
A tuple of tensordicts.
- Raises:
RuntimeError – if the sizes of a list do not add up to
batch_size[dim].
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(x=torch.arange(5), batch_size=[5]) >>> [t["x"] for t in td.split(2)] [tensor([0, 1]), tensor([2, 3]), tensor([4])] >>> [t["x"] for t in td.split([1, 4])] [tensor([0]), tensor([1, 2, 3, 4])]
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.