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)#
Creates a PersistentTensorDict from a h5 file.
- Parameters:
filename (str) – The path to the h5 file.
- Keyword Arguments:
mode (str, optional) – how the file is opened:
"r"(read only),"r+"(read and write), or another mode ofh5py.File(seePersistentTensorDict). Defaults to"r".auto_batch_size (bool, optional) – If
True, the batch size will be computed automatically. Defaults toFalse.batch_dims (int, optional) – If auto_batch_size is
True, defines how many dimensions the output tensordict should have. Defaults toNone(full batch-size at each level).batch_size (Size, optional) – The batch size of the TensorDict. Defaults to
None.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 dims equal the batch size. Otherwise it reads as oneNonTensorDatathat holds the whole dataset. Defaults toFalse.
- Returns:
A PersistentTensorDict representation of the input h5 file.
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( fields={ key1: Tensor(shape=torch.Size([3]), device=cpu, dtype=torch.float32, is_shared=False), key2: Tensor(shape=torch.Size([3]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([3]), device=None, is_shared=False)
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.