LazyStackStorage¶
- class torchrl.data.replay_buffers.LazyStackStorage(max_size: int | None = None, *, compilable: bool = False, stack_dim: int = 0, device: torch.device | str | int | None = None)[原始碼]¶
返回 LazyStackTensorDict 例項的 ListStorage。
此儲存允許將異構結構索引為單個 TensorDict 表示。它使用
LazyStackedTensorDict,它操作非連續的 tensordicts 列表,並在查詢時惰性地堆疊項。這意味著此儲存的取樣速度會很快,但資料訪問可能會很慢(因為它需要堆疊)。儲存中還可以儲存具有異構形狀的張量並一起堆疊。由於儲存被表示為一個列表,因此需要儲存在記憶體中的張量數量將隨著緩衝區的大小呈線性增長。如果可能,還可以透過
densify()建立巢狀張量(請參閱nested)。- 引數:
max_size (int, optional) – 儲存中儲存的最大元素數量。如果未提供,則建立一個無限制的儲存。
- 關鍵字引數:
compilable (bool, optional) – 如果為
True,則儲存將與compile()相容,但代價是在多程序環境中執行。stack_dim (int, optional) – TensorDict 批次大小方面的堆疊維度。預設為 0。
device (str, optional) – 要用於儲存的裝置。預設為 None(輸入不會移動到裝置)。
示例
>>> import torch >>> from torchrl.data import ReplayBuffer, LazyStackStorage >>> from tensordict import TensorDict >>> _ = torch.manual_seed(0) >>> rb = ReplayBuffer(storage=LazyStackStorage(max_size=1000, stack_dim=-1)) >>> data0 = TensorDict(a=torch.randn((10,)), b=torch.rand(4), c="a string!") >>> data1 = TensorDict(a=torch.randn((11,)), b=torch.rand(4), c="another string!") >>> _ = rb.add(data0) >>> _ = rb.add(data1) >>> rb.sample(10) LazyStackedTensorDict( fields={ a: Tensor(shape=torch.Size([10, -1]), device=cpu, dtype=torch.float32, is_shared=False), b: Tensor(shape=torch.Size([10, 4]), device=cpu, dtype=torch.float32, is_shared=False), c: NonTensorStack( ['another string!', 'another string!', 'another st..., batch_size=torch.Size([10]), device=None)}, exclusive_fields={ }, batch_size=torch.Size([10]), device=None, is_shared=False, stack_dim=0)
- attach(buffer: Any) None¶
此函式將取樣器附加到此儲存。
從該儲存讀取的緩衝區必須透過呼叫此方法作為已附加實體包含進來。這確保了當儲存中的資料發生變化時,元件能夠感知到這些變化,即使該儲存與其他緩衝區(例如,Priority Samplers)共享。
- 引數:
buffer – 讀取此儲存的物件。
- dump(*args, **kwargs)¶
dumps()的別名。
- load(*args, **kwargs)¶
loads()的別名。
- save(*args, **kwargs)¶
dumps()的別名。