Rate this Page
★ ★ ★ ★ ★

tensorclass and non-tensor data#

This page lists TensorClass, the class form of a tensordict, and the classes for non-tensor data and unbatched entries. A tensorclass is a class that you declare like a dataclass, with typed fields and methods, and that has the tensor operations of a TensorDict. For a guide to fields, auto-casting, the class options, and static type checking, see TensorClass: a dataclass of tensors.

To declare a tensorclass, subclass TensorClass. The tensorclass() decorator is the original way, kept for existing code (see Legacy: the @tensorclass decorator).

A tensorclass has the methods of TensorDictBase, listed by task in Methods by task. On a tensorclass, the methods that return a TensorDict on a TensorDict return an instance of the tensorclass. Examples are select, clone, to, apply, indexing, and reshaping. On a tensorclass, rename_keys() raises AttributeError for a destination that is not a field, and keeps the sub-tensordict fields that it empties.

Tensorclasses#

TensorClass(*args, **kwargs)

A base class for dataclass-like containers of tensors.

tensorclass([cls, autocast, frozen, nocast, ...])

A decorator that turns a class with annotated fields into a tensorclass.

from_dataclass(obj, *[, dest_cls, ...])

Converts a dataclass instance to a tensorclass instance, or a dataclass type to a tensorclass type.

Non-tensor data#

A tensordict or a tensorclass can hold values that are not tensors, such as strings or Python objects. It wraps each one in a NonTensorData, and a stack of them in a NonTensorStack. If you stack MetaData entries that hold the same object, the result is one MetaData, not a NonTensorStack. TensorAttrs describes the device, dtype, and shape of an entry: attrs() returns them. See Non-tensor data and unbatched entries.

NonTensorDataBase(data[, _metadata, ...])

A base class for tensordict entries that hold a Python object instead of a tensor.

NonTensorData(data[, _metadata, ...])

A tensordict entry that holds a Python object instead of a tensor.

NonTensorStack(*args, **kwargs)

A lazy stack of NonTensorData, one value per batch element.

MetaData(data[, _metadata, _is_non_tensor, ...])

A tensordict entry that holds one Python object for the whole batch.

TensorAttrs([tgt_device, tgt_dtype, ...])

A tensorclass that holds the device, dtype and shape of one tensor.

Entries that ignore the batch shape#

Warning

UnbatchedTensor is experimental: its API can change in a minor release without a deprecation cycle.

An UnbatchedTensor is a tensor that shape operations on its tensordict (indexing, reshaping, stacking) leave unchanged. Operations on its data, such as arithmetic or to(), still apply to it. See UnbatchedTensor.

UnbatchedTensor(data[, batch_size])

A tensor entry that has no batch dimensions and that batch operations leave unchanged.

See also#