TensorDictBase.repeat_interleave#
- TensorDictBase.repeat_interleave(repeats: Tensor | int, dim: int | None = None, *, output_size: int | None = None, inplace: bool = False) Self#
Repeats each element of the batch along
dim, astorch.Tensor.repeat_interleave()does.Each element is repeated next to itself, as with
numpy.repeat(). To repeat the whole batch instead, as withnumpy.tile(), userepeat(). The data is copied. With adim, the names are kept.- Parameters:
repeats (Tensor or int) – the number of repetitions of each element, broadcast to the size of
dim.dim (int, optional) – the batch dimension to repeat along. Defaults to
None, which flattens the batch dimensions first, so the result has one batch dimension. Adimpast 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.
- Keyword Arguments:
output_size (int, optional) – the size of the result along
dim(the sum ofrepeats). Passing it avoids the device synchronization that computes it. Defaults toNone, which computes it.inplace (bool, optional) – if
True, the tensordict keeps its identity and keys, its leaves are replaced one by one, and the call returnsself. It needs adimand at least one batch dimension. A lazy stack, or a tensordict that holds one, raises aNotImplementedError: callto_tensordict()first. See In-place operations. Defaults toFalse.
- Returns:
A tensordict with the same batch size, except along
dim, orselfwithinplace=True.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.tensor([[0, 1], [2, 3]]), batch_size=[2, 2]) >>> td.repeat_interleave(2, dim=0)["a"] tensor([[0, 1], [0, 1], [2, 3], [2, 3]]) >>> td.repeat_interleave(torch.tensor([1, 2]), dim=1)["a"] tensor([[0, 1, 1], [2, 3, 3]]) >>> td.repeat_interleave(2).batch_size torch.Size([8])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.