Rate this Page
★ ★ ★ ★ ★

TensorDictBase.from_list#

classmethod TensorDictBase.from_list(input, *, auto_batch_size: bool | None = None, batch_size: Size | None = None, device: device | None = None, batch_dims: int | None = None, names: list[str] | None = None, lazy: bool | None = None) → Self#

Builds a tensordict by stacking a list of dicts or tensor collections.

Each dict is converted with from_dict() and the results are stacked along a new leading dimension.

Parameters:

input (list) – the dicts or tensor collections to stack.

Keyword Arguments:
  • auto_batch_size (bool, optional) – if True, the batch size of each element is computed automatically. See from_dict().

  • batch_size (iterable of int, optional) – the batch size of the result, with the length of the list first. Each element gets batch_size[1:].

  • device (device or compatible type, optional) – a device for the result.

  • batch_dims (int, optional) – the maximum number of batch dimensions of the result, the list dimension included. Exclusive with batch_size.

  • names (list of str, optional) – the dimension names of the result, one per batch dimension, with the name of the list dimension first.

  • lazy (bool, optional) – if True, the result is a lazy stack; if False, a dense stack. Defaults to None, which builds a dense stack when the elements have the same shape and a lazy stack otherwise.

Examples

>>> import torch
>>> from tensordict import TensorDict
>>> td = TensorDict.from_list(
...     [{"a": torch.zeros(3)}, {"a": torch.zeros(3)}],
...     batch_size=[2, 3],
...     names=["x", "y"],
... )
>>> td.batch_size
torch.Size([2, 3])
>>> td.names
['x', 'y']

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.