NonTensorDataBase#
NonTensorDataBase has the methods of TensorDictBase,
listed by task in Methods by task. This page lists only the
methods that NonTensorDataBase adds, and the overrides that have their own
documentation.
- class tensordict.NonTensorDataBase(data: Any, _metadata: dict | None = None, _is_non_tensor: bool = True, *, batch_size, device=None, names=None)#
Bases:
TensorClassA base class for tensordict entries that hold a Python object instead of a tensor.
There are two subclasses:
NonTensorDataholds one value per batch element. Stacking orunsqueezegives aNonTensorStack.MetaDataholds one value for the whole batch. Stacking items that hold the same object (checked withis) gives oneMetaDatawith the new batch size. Other items give aNonTensorStack.unsqueezeand indexing keep aMetaData.
See Non-tensor data and unbatched entries.
- Parameters:
data (Any) – the value. A
NonTensorDatais unwrapped: its value is used.- Keyword Arguments:
batch_size (Size or list of int, optional) – the batch size of the entry, usually that of the tensordict that holds it. Defaults to
torch.Size([]).device (device, str or int, optional) – the device of the entry. The value itself is not moved. Defaults to
None.names (list of str, optional) – the names of the batch dimensions, one per dimension. Defaults to
None.
- copy()#
Returns a shallow copy: a new entry that holds the same value, as
clone(recurse=False)does.A
list,tupleordictvalue is copied, but not its items.
- property device: device#
The device of the tensorclass, or
Noneif its leaves may be on different devices.It is read-only: use
to()to move the tensorclass to another device.
- extend(tensordict: list[TensorDictBase] | TensorDictBase) None#
Appends tensordicts to the lazy stack that the instance holds.
Deprecated since version 0.15: Build a new instance with
tensordict.lazy_stack()ortorch.cat()instead.
- classmethod fields() tuple[Field, ...]#
Returns the fields of the tensorclass.
Deprecated since version 0.15: Use
dataclasses.fields(cls)instead. It returns the same tuple ofdataclasses.Fieldobjects.
- from_module(as_module: bool = False, lock: bool = False, use_state_dict: bool = False, filter_empty: bool = True)#
Returns a tensordict that holds the parameters and buffers of a module.
The entries are the parameters and buffers of the module themselves, not copies. Submodules become nested tensordicts.
Note
This method does not lock the result by default (
lock=False). The functiontensordict.from_module()locks it by default (lock=True).- Parameters:
module (Module) – the module to get the parameters from.
as_module (bool, optional) – if
True, aTensorDictParamsinstance is returned, which can be used to store parameters within atorch.nn.Module. Defaults toFalse.lock (bool, optional) – if
True, the resulting tensordict is locked. Defaults toFalse.use_state_dict (bool, optional) – if
True, the state-dict from the module is used and unflattened into a TensorDict with the tree structure of the model. Use it when the module has state-dict hooks. Defaults toFalse.filter_empty (bool, optional) – if
True, submodules without parameters or buffers (nn.ReLU, for example) get no entry. IfFalse, they get an empty tensordict. Defaults toTrue.
- Returns:
A
TensorDict, or aTensorDictParamsifas_module=True.
Examples
>>> from torch import nn >>> from tensordict import TensorDict >>> module = nn.Sequential(nn.Linear(3, 4), nn.ReLU()) >>> params = TensorDict.from_module(module) >>> params["0", "weight"] is module[0].weight True >>> list(params.keys()) ['0'] >>> params.is_locked False
- classmethod from_tensordict(tensordict: TensorDictBase, non_tensordict: dict | None = None, safe: bool | None = None) Self#
Wraps a tensordict in a new instance of the tensorclass, without copying the leaves.
If
tensordictis aTensorDict, the entries of the fields annotated with a tensorclass (orOptionalof one) that are tensordicts become instances of that tensorclass, recursively. They are set in a shallow copy oftensordict, which keeps its own entries. An entry with keys that the annotated class does not declare stays a tensordict. Other backends are wrapped as they are: setting a tensorclass in them would write to their storage (e.g. an H5 file).- Parameters:
tensordict (TensorDictBase) – the tensordict that holds the tensor fields.
non_tensordict (dict, optional) – the values of the non-tensor fields. The fields that neither argument holds are set to
None. Defaults toNone.safe (bool, optional) – deprecated in 0.15, will be removed in TensorDict 0.17. If
False, skips the check thattensordictis aTensorDictBase. By default, the check runs, and another type raises aRuntimeError.
- Returns:
A new instance of the class, whose fields are the entries of
tensordict.
Examples
>>> import torch >>> from tensordict import TensorClass >>> class Pose(TensorClass): ... q: torch.Tensor >>> class Obs(TensorClass): ... a: Pose >>> obs = Obs(a=Pose(q=torch.zeros(5, 4), batch_size=[5]), batch_size=[5]) >>> type(Obs.from_tensordict(obs.to_tensordict()).a).__name__ 'Pose'
- maybe_to_stack()#
Returns a
NonTensorStackwith the value in every batch element.The elements share the value: it is not copied. If the batch size is empty, returns
self.Examples
>>> from tensordict import NonTensorData >>> stack = NonTensorData("a", batch_size=[2]).maybe_to_stack() >>> type(stack).__name__, stack.tolist() ('NonTensorStack', ['a', 'a'])
- rename_keys(mapping, *, strict: bool = True)#
Returns a tensorclass with several keys renamed at once.
Works as
rename_keys(), which describesmappingandstrict, with two differences: each destination must be a field of the class, or the call raises anAttributeError, and a field that the renaming empties is kept.Examples
>>> import torch >>> from tensordict import TensorClass >>> class Foo(TensorClass): ... x: torch.Tensor ... y: torch.Tensor >>> foo = Foo(x=torch.zeros(3), y=torch.ones(3), batch_size=[3]) >>> foo.rename_keys({"x": "y", "y": "x"}).x tensor([1., 1., 1.])
- select(*keys, inplace: bool = False, strict: bool = True, as_tensordict: bool = False)#
Returns a tensorclass that holds only the fields of
keys, with the other fields set toNone.Works as
select(), which describeskeys: the leaves are not copied.- Keyword Arguments:
inplace (bool, optional) – if
True, sets the other fields of the tensorclass toNoneand returnsself. Defaults toFalse.strict (bool, optional) – if
True, a missing key raises aKeyError. IfFalse, missing keys are ignored. Defaults toTrue.as_tensordict (bool, optional) – if
True, returns the selected entries in aTensorDictinstead of a tensorclass. Defaults toFalse.
Examples
>>> import torch >>> from tensordict import TensorClass >>> class Foo(TensorClass): ... x: torch.Tensor ... y: torch.Tensor >>> foo = Foo(x=torch.zeros(3), y=torch.ones(3), batch_size=[3]) >>> print(foo.select("x").y) None >>> type(foo.select("x", as_tensordict=True)).__name__ 'TensorDict'
- tolist(*, convert_tensors: bool | Literal['numpy'] = False, tolist_first: bool = False, as_linked_list: bool = False)#
Returns the value in a nested list with one item per batch element.
If the batch size is empty, returns the value itself.
- Keyword Arguments:
convert_tensors (bool or "numpy", optional) – accepted for compatibility with
tolist(). It has no effect: the items are the stored value. Defaults toFalse.tolist_first (bool, optional) – accepted for compatibility with
tolist(). It has no effect. Defaults toFalse.as_linked_list (bool, optional) – if
True, returns atensordict.utils.LinkedListbound to the entry instead of a list. ANonTensorDataholds one value, so setting an item of the list to a non-tensor value, such as a string, raises aValueErrorand leaves the list unchanged. Callmaybe_to_stack()first to get aNonTensorStack, whose list writes back. Defaults toFalse.
Examples
>>> from tensordict import NonTensorData >>> NonTensorData("a", batch_size=[2, 1]).tolist() [['a'], ['a']] >>> NonTensorData("a").tolist() 'a'