TensorDictBase.from_any#
- classmethod TensorDictBase.from_any(obj, *, auto_batch_size: bool = False, batch_dims: int | None = None, device: device | None = None, batch_size: Size | None = None)#
Converts a dict, tuple, dataclass, array or file to a tensordict, recursively.
from_anypicks the conversion from the type ofobj, and applies it to the values insideobjtoo. It accepts more types than theTensorDictconstructor, at the cost of some overhead and of opinionated choices about how each type maps to entries:a dict or
collections.UserDict:from_dict();a named tuple:
from_namedtuple();a tuple:
from_tuple(), with the keys"0","1", … This includes the results oftorch.return_types(max,sort,topk, …), which also emit aFutureWarning. Starting with TensorDict 0.17, the default will change to their field names ("values","indices", …), asfrom_namedtuple()gives;a dataclass instance:
from_dataclass()(the result is a tensordict, not a tensorclass). A dataclass type whose fields all have defaults gives a tensordict of the defaults too, with aFutureWarning. Starting with TensorDict 0.17, the default will change to returning the type unchanged, as for any other class;a NumPy structured array:
from_struct_array();a pandas DataFrame:
from_pandas();an open
h5py.File: aPersistentTensorDict(seefrom_h5()).
A tensor or a tensor collection is returned as it is. A list becomes a tensor if
torch.tensor()takes it, and aNonTensorStackotherwise. Any other object is returned unchanged.- Parameters:
obj – the object to convert.
- Keyword Arguments:
auto_batch_size (bool, optional) – if
True, sets the batch size to the largest leading shape that all the entries share. Exclusive withbatch_size, except for a dict, wherebatch_sizewins, and for anh5py.File, whereauto_batch_sizewins. Defaults toFalse.batch_dims (int, optional) – with
auto_batch_size, the maximum number of batch dimensions. Ignored withoutauto_batch_size, with aFutureWarningwhen it would change the result. Starting with TensorDict 0.17, the default will change to turning onauto_batch_sizewhenbatch_dimsis given, asfrom_dictdoes. Defaults toNone.device (device, str or int, optional) – the device of the result. Leaves on another device are moved to it.
Noneleaves each leaf where it is, and the result has no device. Defaults toNone.batch_size (Size or list of int, optional) – the batch size of the result: the leading dimensions that all its entries share. Exclusive with
auto_batch_size, except for a dict, wherebatch_sizewins and no batch size is computed, and for anh5py.File, whereauto_batch_sizewins andbatch_sizeis ignored. Defaults toNone.
- Returns:
A tensordict (a
PersistentTensorDictfor an HDF5 file), a tensor or aNonTensorStackfor a list, orobjitself for the other types.
Examples
>>> import dataclasses >>> import torch >>> from tensordict import TensorDict >>> @dataclasses.dataclass ... class Data: ... obs: torch.Tensor ... extra: tuple >>> data = Data(obs=torch.zeros(3, 4), extra=(torch.ones(3), {"x": torch.ones(3)})) >>> td = TensorDict.from_any(data, auto_batch_size=True) >>> td.batch_size torch.Size([3]) >>> sorted(td.keys(include_nested=True, leaves_only=True), key=str) [('extra', '0'), ('extra', '1', 'x'), 'obs'] >>> TensorDict.from_any([1, 2, 3]) tensor([1, 2, 3])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.