Rate this Page
★ ★ ★ ★ ★

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)#

Recursively converts any object to a TensorDict.

Note

from_any is less restrictive than the regular TensorDict constructor. It can cast data structures like dataclasses or tuples to a tensordict using custom heuristics. This approach may incur some extra overhead and involves more opinionated choices in terms of mapping strategies.

Note

This method recursively converts the input object to a TensorDict. If the object is already a TensorDict (or any similar tensor collection object), it will be returned as is.

Parameters:

obj – The object to be converted.

Keyword Arguments:
  • auto_batch_size (bool, optional) – if True, the batch size will be computed automatically. Defaults to False.

  • batch_dims (int, optional) – If auto_batch_size is True, defines how many dimensions the output tensordict should have. Defaults to None (full batch-size at each level).

  • device (device, optional) – The device on which the TensorDict will be created.

  • batch_size (Size, optional) – The batch size of the TensorDict. Exclusive with auto_batch_size.

Returns:

A TensorDict representation of the input object.

Supported objects:

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.