TensorAttrs#
TensorAttrs has the methods of TensorDictBase,
listed by task in Methods by task. This page lists only the
methods that TensorAttrs adds, and the overrides that have their own
documentation.
- class tensordict.TensorAttrs(tgt_device: Any = None, tgt_dtype: Any = None, tgt_shape: Any = None, *, batch_size, device=None, names=None)#
Bases:
TensorClassA tensorclass that holds the device, dtype and shape of one tensor.
attrs()returns a tensordict with oneTensorAttrsper leaf. Pass it toto()to move or cast each leaf of another tensordict to the device and dtype of the matching leaf, for example when the leaves are on different devices. The names follow the PyTorch Tensor Attributes.The fields start with
tgt_so that they do not hide thedevice,dtypeandshapeattributes of the tensorclass.- Parameters:
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict({"a": torch.zeros(3, device="cpu"), ... "b": torch.zeros(3, dtype=torch.int32)}, batch_size=[3]) >>> attrs = td.attrs() >>> attrs["a"].tgt_device, attrs["a"].tgt_dtype (device(type='cpu'), torch.float32)
- 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_tensor(tensor, *, fields=('device', 'dtype', 'shape'))#
Returns a
TensorAttrswith the attributes oftensor.- Parameters:
tensor (Tensor) – the tensor to describe.
- Keyword Arguments:
fields (tuple of str, optional) – the attributes to read, among
"device","dtype"and"shape". The other fields areNone. Defaults to("device", "dtype", "shape").
Examples
>>> import torch >>> from tensordict import TensorAttrs >>> attrs = TensorAttrs.from_tensor(torch.zeros(2, 3), fields=("dtype",)) >>> attrs.tgt_dtype, attrs.tgt_shape (torch.float32, None)
- 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'
- 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'