TensorDictBase.chunk#
- TensorDictBase.chunk(chunks: int, dim: int = 0) tuple[TensorCollection, ...]#
Splits the tensordict into
chunkspieces along a batch dimension, astorch.chunk()does.Each piece has
ceil(batch_size[dim] / chunks)elements, except the last one, which can be smaller. So there can be fewer thanchunkspieces. The pieces hold views of the original tensors.- Parameters:
- Returns:
A tuple of tensordicts.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(x=torch.arange(5), batch_size=[5]) >>> [t["x"] for t in td.chunk(2)] [tensor([0, 1, 2]), tensor([3, 4])] >>> [t["x"] for t in td.chunk(4)] [tensor([0, 1]), tensor([2, 3]), tensor([4])]
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.