stack#
- tensordict.stack(input: Sequence['T'], dim: int = 0, *, out=None) T#
Stacks tensordicts into one dense tensordict along a new dimension
dim.Same as
torch.stack(input, dim). Every input must have the same keys, entry shapes and batch size. If they do not, this function raises aRuntimeError. To fall back to a lazy stack when the inputs differ, usemaybe_dense_stack(). To always get a lazy stack, uselazy_stack(). Stacking and variable-size data compares these functions.- Parameters:
input (sequence of TensorDictBase or tensorclass instances) – the tensordicts to stack.
dim (int, optional) – the new dimension. Defaults to
0. Adimoutside[-batch_dims - 1, batch_dims]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:
out (TensorDictBase, optional) – a tensordict to write the result into. Its batch size must be the stacked batch size. Defaults to
None.- Returns:
A
TensorDictwhose batch size isbatch_size[:dim] + [len(input)] + batch_size[dim:]. Tensorclass inputs give an instance of their class. If every input is aLazyStackedTensorDict, the result is aLazyStackedTensorDict.
Examples
>>> import torch >>> import tensordict >>> from tensordict import TensorDict >>> tds = [TensorDict(a=torch.zeros(3), batch_size=[3]) for _ in range(2)] >>> td = tensordict.stack(tds, dim=1) >>> td.batch_size torch.Size([3, 2]) >>> type(td).__name__ 'TensorDict'