TensorDictBase.get#
- TensorDictBase.get(key)#
- TensorDictBase.get(key, default)
Returns the value stored at
key, ordefaultif the key is missing.td.get(key)is the same astd[key], except for a missing key:td[key]raisesKeyError, andgetreturnsdefault.- Parameters:
key (NestedKey) – the key to read. A tuple reads a nested entry:
td.get(("a", "b"))istd["a"]["b"]. See Keys, nested keys, and locking.default (optional) – the value returned if the key is missing. Defaults to
None, as fordict.get(). A nested key that runs through a tensor is missing too:getreturns the default passed to it, and without one raises aValueErrorthat names the tensor.
Changed in version 0.7: A missing key returns
Noneinstead of raisingKeyError.set_get_defaults_to_none(False)andTD_GET_DEFAULTS_TO_NONE=0restore the old behavior. They are deprecated and will be removed in 0.17.- Keyword Arguments:
as_list (bool, optional) – if
True, a lazy stack returns the entries as a list, one item per stacked tensordict. Exclusive withas_padded_tensorandas_nested_tensor. Defaults toFalse.as_padded_tensor (bool, optional) – if
True, a lazy stack pads the entries to the largest shape and returns them as one tensor: each dim is padded to its largest size, also across the levels of a lazy stack of lazy stacks.padding_valuesets the padding value andpadding_sidesets the side of every padded dim. Exclusive withas_listandas_nested_tensor. Defaults toFalse.as_nested_tensor (bool, optional) – if
True, a lazy stack returns the entries as a nested tensor (torch.nested).layoutsets its layout. A lazy stack of lazy stacks raises aValueError, as a nested tensor has a single nested level. Exclusive withas_listandas_padded_tensor. Defaults toFalse.layout (layout, optional) – the layout of the nested tensor when
as_nested_tensor=True:torch.jaggedortorch.strided. Defaults totorch.jagged.padding_side (str) – The side of padding. Must be “left” or “right”. Defaults to “right”.
padding_value (scalar or bool, optional) – The padding value. Defaults to 0.0.
A lazy stack reads the entry of every stacked tensordict and stacks the results into a new tensor. If the entry shapes differ, the plain call raises a
RuntimeError: passas_list,as_padded_tensororas_nested_tensorto get a list, a padded tensor or a nested tensor instead.Note
A dense tensordict ignores the keyword arguments today and warns for
as_list=Trueandas_nested_tensor=True. Starting with TensorDict 0.17, the default will change to returning a list or a nested tensor, as a lazy stack does.Examples
>>> from tensordict import TensorDict >>> td = TensorDict({"x": 1, "nested": {"y": 2}}, batch_size=[]) >>> td.get("x") tensor(1) >>> td.get(("nested", "y")) tensor(2) >>> print(td.get("z")) None >>> td.get("z", 0) 0
>>> import torch >>> from tensordict import lazy_stack >>> td = lazy_stack([ ... TensorDict({"x": torch.ones(1,)}), ... TensorDict({"x": torch.ones(2,) * 2}), ... ]) >>> nested = td.get("x", as_nested_tensor=True) >>> nested.is_nested, nested.offsets() (True, tensor([0, 1, 3])) >>> td.get("x", as_padded_tensor=True) tensor([[1., 0.], [2., 2.]])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.