快捷方式

LazyTensorStorage

class torchrl.data.replay_buffers.LazyTensorStorage(max_size: int, *, device: device = 'cpu', ndim: int = 1)[source]

用於張量和 Tensordict 的預先分配張量儲存空間。

參數:

max_size (int) – 儲存空間的大小,即緩衝區中儲存的最大元素數量。

關鍵字參數:
  • device (torch.device, optional) – 儲存和傳送取樣張量的裝置。預設值為 torch.device("cpu")。 如果傳遞 "auto",則會自動從傳遞的第一批資料中收集裝置。 預設情況下不啟用此功能,以避免資料錯誤地放置在 GPU 上,導致 OOM 問題。

  • ndim (int, optional) – 測量儲存空間大小時要考慮的維度數量。 例如,形狀為 [3, 4] 的儲存空間,如果 ndim=1 則容量為 3,如果 ndim=2 則容量為 12。 預設為 1

範例

>>> data = TensorDict({
...     "some data": torch.randn(10, 11),
...     ("some", "nested", "data"): torch.randn(10, 11, 12),
... }, batch_size=[10, 11])
>>> storage = LazyTensorStorage(100)
>>> storage.set(range(10), data)
>>> len(storage)  # only the first dimension is considered as indexable
10
>>> storage.get(0)
TensorDict(
    fields={
        some data: Tensor(shape=torch.Size([11]), device=cpu, dtype=torch.float32, is_shared=False),
        some: TensorDict(
            fields={
                nested: TensorDict(
                    fields={
                        data: Tensor(shape=torch.Size([11, 12]), device=cpu, dtype=torch.float32, is_shared=False)},
                    batch_size=torch.Size([11]),
                    device=cpu,
                    is_shared=False)},
            batch_size=torch.Size([11]),
            device=cpu,
            is_shared=False)},
    batch_size=torch.Size([11]),
    device=cpu,
    is_shared=False)
>>> storage.set(0, storage.get(0).zero_()) # zeros the data along index ``0``

此類別也支援 tensorclass 資料。

範例

>>> from tensordict import tensorclass
>>> @tensorclass
... class MyClass:
...     foo: torch.Tensor
...     bar: torch.Tensor
>>> data = MyClass(foo=torch.randn(10, 11), bar=torch.randn(10, 11, 12), batch_size=[10, 11])
>>> storage = LazyTensorStorage(10)
>>> storage.set(range(10), data)
>>> storage.get(0)
MyClass(
    bar=Tensor(shape=torch.Size([11, 12]), device=cpu, dtype=torch.float32, is_shared=False),
    foo=Tensor(shape=torch.Size([11]), device=cpu, dtype=torch.float32, is_shared=False),
    batch_size=torch.Size([11]),
    device=cpu,
    is_shared=False)
attach(buffer: Any) None

此函式將取樣器附加到此儲存空間。

從此儲存空間讀取的緩衝區必須透過呼叫此方法作為附加實體包含在內。 這保證了當儲存空間中的資料發生變化時,即使儲存空間與其他緩衝區(例如,優先順序取樣器)共享,元件也能感知到變化。

參數:

buffer – 從此儲存空間讀取的物件。

dump(*args, **kwargs)

dumps() 的別名。

load(*args, **kwargs)

loads() 的別名。

save(*args, **kwargs)

dumps() 的別名。

文件

取得 PyTorch 的完整開發人員文件

查看文件

教學

取得適合初學者和進階開發人員的深度教學課程

查看教學

資源

尋找開發資源並獲得問題解答

查看資源