NestedKey#
- class tensordict.NestedKey#
Bases:
objectAn abstract class for nested keys.
Nested keys are the generic key type accepted by TensorDict.
A nested key is either a string or a non-empty tuple of NestedKeys instances.
td["a", "b"]andtd[("a", "b")]read the entry"b"of the nested tensordict"a". Nested tuples are flattened:("a", ("b",))is the same key as("a", "b"). See Keys, nested keys, and locking.The NestedKey class supports instance checks. Use it in type hints and in
isinstancechecks.Examples
>>> from tensordict import NestedKey, TensorDict >>> isinstance("a", NestedKey), isinstance(("a", ("b",)), NestedKey) (True, True) >>> isinstance(("a", 1), NestedKey), isinstance((), NestedKey) (False, False) >>> td = TensorDict({"a": {"b": 0}}) >>> td["a", "b"] is td[("a", ("b",))] True