Rate this Page
★ ★ ★ ★ ★

TensorDictBase.create_nested#

TensorDictBase.create_nested(key)#

Adds an empty nested tensordict at key, and returns self.

The new tensordict has the batch size, device and names of the tensordict it is added to. An existing entry at key is replaced. For a tuple key, a plain TensorDict keeps a plain, unlocked TensorDict already on the path with its entries: ("a", "b") adds "b" to an existing td["a"]. An indexed view of a plain TensorDict and a typed tensordict that wraps one do the same; the view keeps the entries in every row of the tensordict it indexes. A lazy stack of plain TensorDict instances, or of such stacks, does this in each member, and a tensorclass or a TensorDictParams follows the rule of the tensordict it wraps. A PersistentTensorDict keeps the groups already on the path. Any other entry on the path, such as a tensor, a lazy stack, a tensorclass, a typed tensordict or a locked tensordict, is replaced by a new empty tensordict, and so is every level of the path when the tensordict is of any other type, such as a lazy view or a store.

Parameters:

key (NestedKey) – the key of the new tensordict.

Raises:

RuntimeError – if the tensordict is locked.

Examples

>>> from tensordict import TensorDict
>>> data = TensorDict({}, [3, 4, 5])
>>> _ = data.create_nested(("some", "nested", "value"))
>>> data["some", "nested", "value"].batch_size
torch.Size([3, 4, 5])
>>> list(data.keys(include_nested=True))
[('some', 'nested', 'value'), ('some', 'nested'), 'some']
>>> _ = data.create_nested(("some", "other"))
>>> list(data.keys(include_nested=True))
[('some', 'nested', 'value'), ('some', 'nested'), ('some', 'other'), 'some']

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