TensorDictBase.create_nested#
- TensorDictBase.create_nested(key)#
Adds an empty nested tensordict at
key, and returnsself.The new tensordict has the batch size, device and names of the tensordict it is added to. An existing entry at
keyis replaced. For a tuple key, a plainTensorDictkeeps a plain, unlockedTensorDictalready on the path with its entries:("a", "b")adds"b"to an existingtd["a"]. An indexed view of a plainTensorDictand 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 plainTensorDictinstances, or of such stacks, does this in each member, and a tensorclass or aTensorDictParamsfollows the rule of the tensordict it wraps. APersistentTensorDictkeeps 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.