TensorDictBase.to_zarr#
- TensorDictBase.to_zarr(filename, *, allow_pickle: bool | None = None, **kwargs) Any#
Writes the tensordict to a new zarr store and returns a tensordict backed by it.
Each leaf becomes an array of the store, and each nested tensordict a group. An existing store at
filenameis overwritten. The batch size and the names of the batch dimensions are saved in the store attributes, so thatfrom_zarr()restores them. Requireszarr>=3.1.1. See Save and load tensordicts.- Parameters:
filename (str, path or zarr store) – the path of the zarr store (a directory), or a
zarr.abc.store.Storeinstance (e.g. azarr.storage.ZipStore).**kwargs – passed to
zarr.Group.create_array(). By default each tensor is stored as one uncompressed chunk. Passchunks=...orcompressors=...to change that (for example to read rows without loading the whole array, or to compress on disk).chunksapplies to the leading dimensions of each leaf, and the trailing dimensions are left whole:chunks=(16,)chunks every leaf along its first dimension in blocks of 16.
- Keyword Arguments:
allow_pickle (bool, optional) – the read policy of the returned tensordict for pickled non-tensor values.
Falserejects them.NoneandTrueread them, since this call wrote them. Opening the store again withfrom_zarr()uses the policy of that call. Defaults toNone.- Returns:
A
PersistentTensorDictbacked by the new store.
Examples
>>> import os >>> import tempfile >>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.zeros(1000), b={"c": torch.zeros(1000, 3)}, batch_size=[1000]) >>> with tempfile.TemporaryDirectory() as tmpdir: ... td_zarr = td.to_zarr(os.path.join(tmpdir, "store.zarr"), chunks=(100,)) ... print(type(td_zarr).__name__, td_zarr.batch_size, td_zarr["b", "c"].shape) PersistentTensorDict torch.Size([1000]) torch.Size([1000, 3])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.