TensorDictBase.from_tuple#
- classmethod TensorDictBase.from_tuple(obj, *, auto_batch_size: bool = False, batch_dims: int | None = None, device: device | None = None, batch_size: Size | None = None)#
Converts a tuple to a tensordict with the keys
"0","1", …Each element is converted with
from_any(), so a nested tuple or dict becomes a nested tensordict and a number becomes a tensor.- Parameters:
obj (tuple) – the tuple 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. 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. It applies to the nested tensordicts too. Exclusive with
auto_batch_size. Defaults toNone, an empty batch size.
- Returns:
A
TensorDict.- Raises:
TypeError – if both
auto_batch_size=Trueandbatch_sizeare given.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict.from_tuple((torch.zeros(3, 4), 1), auto_batch_size=True) >>> td["0"].shape, td["1"], td.batch_size (torch.Size([3, 4]), tensor(1), torch.Size([])) >>> td = TensorDict.from_tuple((torch.zeros(3, 4), torch.ones(3)), auto_batch_size=True) >>> td.batch_size torch.Size([3])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.