LazyStackStorage¶
- class torchrl.data.replay_buffers.LazyStackStorage(max_size: int | None = None, *, compilable: bool = False, stack_dim: int = - 1)[source]¶
一個返回 LazyStackTensorDict 例項的 ListStorage。
該儲存允許將異構結構索引為單個 TensorDict 表示。它使用
LazyStackedTensorDict,後者對不連續的 tensordicts 列表進行操作,在查詢時才惰性地堆疊項。這意味著該儲存取樣速度快,但資料訪問可能較慢(因為它需要堆疊)。形狀異構的張量也可以儲存在該儲存中並堆疊在一起。由於儲存表示為列表,儲存在記憶體中的張量數量將隨緩衝區大小線性增長。如果可能,還可以透過
densify()建立巢狀張量(參見nested)。- 引數:
max_size (int, 可選) – 儲存中儲存的最大元素數量。如果未提供,則建立一個無限制的儲存。
- 關鍵字引數:
compilable (bool, 可選) – 如果為
True,則該儲存將與compile()相容,代價是無法在多程序設定中執行。stack_dim (int, 可選) – 按 TensorDict 批次大小計算的堆疊維度。預設為 -1。
示例
>>> 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()的別名。