TensorDictBase.attrs#
- TensorDictBase.attrs(*, fields: Sequence[str] = ('device', 'dtype', 'shape'), num_threads: int | None = None) Self#
Returns a tensordict that describes each tensor leaf with a
TensorAttrs.Each tensor leaf is replaced by a
TensorAttrsthat records its device, dtype and shape. The result has an empty batch size and no device. Pass it toto()to cast another tensordict leaf by leaf, to the device and dtype of the matching leaf, when the leaves do not all share one device or dtype.- Keyword Arguments:
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(fields=("device", "dtype")) >>> target = TensorDict(a=torch.zeros(3, device="cpu"), b=torch.zeros(3), batch_size=[3]) >>> out = target.to(attrs) # casts "b" to int32 >>> out["b"].dtype torch.int32
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.