TensorDictBase.separates#
- TensorDictBase.separates(*keys: NestedKey, default: Any = NO_DEFAULT, strict: bool = True, filter_empty: bool = True) Self#
Removes the entries of
keysfrom the tensordict in place, and returns them in a new tensordict.The values are not copied. The result keeps the nested tensordicts that hold the removed entries, with the batch size of the tensordict. This is
split_keys()withinplace=Trueand one key set, except that only the separated part is returned: the tensordict holds the rest.- Parameters:
*keys (NestedKey) – the keys to remove.
- Keyword Arguments:
default (Any, optional) – if given, a missing key does not raise, and the result holds
defaultunder it, assplit_keys()does. AdefaultofNonegives aNoneentry. Adefaultthat does not fit the batch size (for example0with a batch size of[3]) also gives aNoneentry, with aFutureWarning: starting with TensorDict 0.17, the default will change to raising the error ofsplit_keys().strict (bool, optional) – if
True, a missing key raises aKeyError. IfFalse, the result holdsdefaultunder it, or aNoneentry if nodefaultis given. Defaults toTrue.filter_empty (bool, optional) – if
True, the nested tensordicts of the tensordict that the removal leaves empty are removed too. Defaults toTrue.
- Returns:
A new tensordict with the removed entries.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict( ... a=torch.zeros(3), ... b=torch.zeros(3), ... nested=TensorDict(c=torch.zeros(3), batch_size=[3]), ... batch_size=[3], ... ) >>> out = td.separates("a", ("nested", "c")) >>> sorted(out.keys(include_nested=True, leaves_only=True), key=str) [('nested', 'c'), 'a'] >>> list(td.keys()) ['b']
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.