TensorDictBase.replace#
- TensorDictBase.replace(*args, **kwargs)#
Returns a shallow copy of the tensordict with some entries replaced.
The copy is made with
copy(), and the new entries are written into it withupdate(): nested tensordicts are merged, and the tensordict does not change. With no entry to write,replacereturnsself.When both a positional argument and
**kwargsare given, the keyword entries are merged into a copy of the positional argument (withdictunpacking for a dictionary, and withcopy()andupdate()for a tensordict), so the argument does not change. A file or a store, such as aPersistentTensorDict, or an indexed view of one, shares its storage with its copy. So where the keyword entries go (the argument itself, each member of a lazy stack, and each nested tensordict that they merge into), a file or a store is replaced by aTensorDictthat holds its entries; the other entries stay views of the file or store. Called on a file or a store, or on a tensordict that holds one as a nested entry that the new entries merge into,replacewrites the new entries into the file or store.- Parameters:
*args (dict or TensorDictBase) – at most one, the entries to write.
**kwargs – top-level entries to write, by key. They take precedence over the entries of the positional argument.
- Returns:
A shallow copy of the tensordict, or
selfif there is no entry to write.- Raises:
RuntimeError – if more than one positional argument is passed, or if it is not a dictionary or a tensordict.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.zeros(3), b=torch.zeros(3), batch_size=[3]) >>> new = td.replace(a=torch.ones(3)) >>> new["a"], td["a"], new["b"] is td["b"] (tensor([1., 1., 1.]), tensor([0., 0., 0.]), True)
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.