TensorDictBase.get#
- TensorDictBase.get(key)#
- TensorDictBase.get(key, default)
Returns the value stored at
key, ordefaultif the key is missing.td.get(key)reads the same entry astd[key], with two differences: a missing key returnsdefaultwheretd[key]raisesKeyError, and a non-tensor entry returns itsNonTensorDatawheretd[key]returns the content.A lazy stack reads the entry of every stacked tensordict and stacks the results into a new tensor. If their shapes differ, this raises a
RuntimeError: passas_list,as_padded_tensororas_nested_tensorto get a list, a padded tensor or a nested tensor instead. See Variable-size data.- 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 (Any, 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.
- 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 size of each dimension, also across the levels of a lazy stack of lazy stacks, and stacks them into one tensor. 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). 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) – with
as_nested_tensor=True, the layout of the nested tensor:torch.jaggedortorch.strided. Defaults totorch.jagged.padding_side (str, optional) – with
as_padded_tensor=True, the side of every dimension that gets the padding:"left"or"right". Defaults to"right".padding_value (float, int or bool, optional) – with
as_padded_tensor=True, the value of the padding. Defaults to0.0.
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.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 TensorDict 0.17.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
On a lazy stack of entries of different shapes:
>>> import torch >>> from tensordict import lazy_stack >>> td = lazy_stack([ ... TensorDict({"x": torch.ones(1,)}), ... TensorDict({"x": torch.ones(2,) * 2}), ... ]) >>> td.get("x", as_list=True) [tensor([1.]), tensor([2., 2.])] >>> td.get("x", as_padded_tensor=True) tensor([[1., 0.], [2., 2.]]) >>> nested = td.get("x", as_nested_tensor=True) >>> nested.is_nested, nested.offsets() (True, tensor([0, 1, 3]))
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.