TensorDictBase.from_h5#
- classmethod TensorDictBase.from_h5(filename, *, mode: str = 'r', auto_batch_size: bool = False, batch_dims: int | None = None, batch_size: Size | None = None, non_tensor_as_stack: bool = False)#
Opens an HDF5 file as a
PersistentTensorDict.The data stays in the file: entries are read when they are accessed. Requires
h5py. See Save and load tensordicts andPersistentTensorDict.- Parameters:
filename (str or path) – the path to the HDF5 file.
- Keyword Arguments:
mode (str, optional) – how the file is opened, as in
h5py.File:"r"(read only),"r+"(read and write), and so on. Defaults to"r".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.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. Defaults toNone, which gives each nested tensordict the largest batch size that its entries share.non_tensor_as_stack (bool, optional) – if
True, a non-tensor dataset that tensordict did not write, such as a string column with one string per row, reads as aNonTensorStackof its rows when its leading dimensions equal the batch size. Otherwise it reads as oneNonTensorDatathat holds the whole dataset. Defaults toFalse.
- Returns:
A
PersistentTensorDictbacked by the file.- Raises:
TypeError – if both
auto_batch_size=Trueandbatch_sizeare given.
Examples
>>> import os >>> import tempfile >>> import torch >>> from tensordict import TensorDict >>> data = TensorDict(key1=torch.zeros(3), key2=torch.ones(3), batch_size=[3]) >>> with tempfile.TemporaryDirectory() as tmpdir: ... path = os.path.join(tmpdir, "file.h5") ... _ = data.to_h5(path) ... td = TensorDict.from_h5(path) ... print(td) PersistentTensorDict(batch_size=[3], fields={key1: f32[], key2: f32[]})
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.