TensorDictBase.popitem#
- TensorDictBase.popitem() Tuple[NestedKey, Tensor | TensorCollection]#
Removes the last top-level entry and returns it as a
(key, value)pair.The last entry is the last key of
keys(), which is the last one inserted for aTensorDict. A nested tensordict is returned whole.- Raises:
KeyError – if the tensordict is empty.
RuntimeError – if the tensordict is locked.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.zeros(3), b=torch.ones(3), batch_size=[3]) >>> td.popitem() ('b', tensor([1., 1., 1.])) >>> list(td.keys()) ['a']
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.