TensorDictBase.tolist#
- TensorDictBase.tolist(*, convert_nodes: bool = True, convert_tensors: bool | Literal['numpy'] = False, tolist_first: bool = False, as_linked_list: bool = False) List[Any]#
Returns the elements of the tensordict as nested lists, one level per batch dimension.
The innermost elements are the tensordict indexed at every batch index, converted with
to_dict(): for batch size[2, 3], the result is a list of 2 lists of 3 dicts. A tensordict without batch dimensions returns one dict.- Keyword Arguments:
convert_nodes (bool, optional) – if
True, the elements are converted to dicts. IfFalse, they are kept as tensordicts. Defaults toTrue.convert_tensors (bool or str, optional) – as in
to_dict(): ifTrue, tensors become Python lists, and if"numpy", NumPy arrays. Requiresconvert_nodes=True. Defaults toFalse.tolist_first (bool, optional) – as in
to_dict(): ifTrue, a nested tensordict with batch dimensions becomes a list of dicts. Defaults toFalse.as_linked_list (bool, optional) – if
True, returns aLinkedList: assigning an element of the outer list (result[0] = {...}) writes it into the tensordict. Defaults toFalse.
- Returns:
A list (nested once per batch dimension), or a dict (or
selfifconvert_nodes=False) when the tensordict has no batch dimensions.- Raises:
TypeError – if
convert_tensorsis set andconvert_nodes=False.
See also
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict( ... a=torch.arange(4).view(2, 2), ... b=TensorDict(c=torch.arange(8).view(2, 2, 2), batch_size=(2, 2, 2)), ... batch_size=(2,), ... ) >>> td.tolist() [{'a': tensor([0, 1]), 'b': {'c': tensor([[0, 1], [2, 3]])}}, {'a': tensor([2, 3]), 'b': {'c': tensor([[4, 5], [6, 7]])}}] >>> td.tolist(convert_tensors=True) [{'a': [0, 1], 'b': {'c': [[0, 1], [2, 3]]}}, {'a': [2, 3], 'b': {'c': [[4, 5], [6, 7]]}}] >>> td.tolist(tolist_first=True, convert_tensors=True)[0] {'a': [0, 1], 'b': [[{'c': 0}, {'c': 1}], [{'c': 2}, {'c': 3}]]} >>> type(td.tolist(convert_nodes=False)[0]).__name__ 'TensorDict'
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.