Rate this Page
★ ★ ★ ★ ★

default_is_leaf#

tensordict.default_is_leaf(cls: Type) → bool#

Returns True if a type is a leaf: anything but a tensordict or a tensorclass.

This is the default is_leaf of keys() and the methods that walk the nested entries. A UnbatchedTensor is a leaf too. A NonTensorData is not, so leaves_only=True skips the non-tensor entries. See Keys, nested keys, and locking.

Parameters:

cls (type) – the type of an entry, as returned by entry_class().

Examples

>>> import torch
>>> from tensordict import TensorDict, NonTensorData, default_is_leaf
>>> default_is_leaf(torch.Tensor), default_is_leaf(NonTensorData), default_is_leaf(TensorDict)
(True, False, False)
>>> td = TensorDict(a={}, b="a string!", c=torch.zeros(()))
>>> list(td.keys(leaves_only=True, is_leaf=default_is_leaf))
['c']