parse_tensor_dict_string#
- tensordict.parse_tensor_dict_string(s: str)#
Builds a
TensorDictfrom 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(seeset_printoptions()).- Returns:
A new
TensorDict.- Raises:
ValueError – if a field is not a
Tensoror aTensorDictprinted with the default print options, or ifsis 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.)