Rate this Page
★ ★ ★ ★ ★

TensorDictBase.to_namedtuple#

TensorDictBase.to_namedtuple(dest_cls: type | None = None) → Any#

Converts a tensordict to a namedtuple.

Parameters:

dest_cls (Type, optional) – an optional namedtuple class to use for the result. A nested tensordict is converted to dest_cls too if dest_cls takes its keys, and to a GenericDict otherwise.

Examples

>>> from tensordict import TensorDict
>>> import torch
>>> data = TensorDict({
...     "a_tensor": torch.zeros((3)),
...     "nested": {"a_tensor": torch.zeros((3)), "a_string": "zero!"}}, [3])
>>> data.to_namedtuple()
GenericDict(a_tensor=tensor([0., 0., 0.]), nested=GenericDict(a_tensor=tensor([0., 0., 0.]), a_string='zero!'))

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