Rate this Page
★ ★ ★ ★ ★

parse_tensor_dict_string#

tensordict.parse_tensor_dict_string(s: str)#

Builds a TensorDict from its printed form.

It is a debugging tool: it rebuilds a tensordict with the keys, shapes, dtypes, devices and batch sizes of a printed one, so that you can reproduce a tensordict from a log. The values are not in the repr, so every tensor of the result is filled with zeros.

It reads both the compact repr, the default, and the one printed with set_printoptions(compact=False).

Parameters:

s (str) – the repr of a tensordict, as printed with the default print options or with compact=False (see set_printoptions()).

Returns:

A new TensorDict.

Raises:

ValueError – if a field is not a Tensor or a TensorDict printed with the default print options, or if s is the repr of a lazy stack with exclusive keys.

Examples

>>> import torch
>>> from tensordict import TensorDict, parse_tensor_dict_string
>>> td = TensorDict(
...     obs=torch.ones(3, 4),
...     next=TensorDict(done=torch.ones(3, 1, dtype=torch.bool)),
...     batch_size=[3],
... )
>>> rebuilt = parse_tensor_dict_string(repr(td))
>>> rebuilt["next", "done"].dtype, rebuilt.batch_size
(torch.bool, torch.Size([3]))
>>> rebuilt["obs"].sum()
tensor(0.)