快捷方式

TensorStorage

class torchrl.data.replay_buffers.TensorStorage(storage, max_size=None, *, device: device = 'cpu', ndim: int = 1, compilable: bool =False)[原始碼]

用於儲存張量 (tensor) 和 tensordict 的儲存類。

引數:
  • storage (tensorTensorDict) – 要使用的資料緩衝區。

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

關鍵字引數:
  • device (torch.device, 可選) – 取樣到的張量將儲存和傳送到的裝置。預設值為 torch.device("cpu")。如果傳入 "auto",則裝置將自動從傳入的第一批資料中獲取。此選項預設不啟用,以避免資料被錯誤地放置在 GPU 上,導致 OOM 問題。

  • ndim (int, 可選) – 計算儲存大小時要考慮的維度數量。例如,形狀為 [3, 4] 的儲存,如果 ndim=1,容量為 3;如果 ndim=2,容量為 12。預設為 1

  • compilable (bool, 可選) – 儲存是否可編譯。如果為 True,則寫入器不能在多個程序之間共享。預設為 False

示例

>>> data = TensorDict({
...     "some data": torch.randn(10, 11),
...     ("some", "nested", "data"): torch.randn(10, 11, 12),
... }, batch_size=[10, 11])
>>> storage = TensorStorage(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=None,
                    is_shared=False)},
            batch_size=torch.Size([11]),
            device=None,
            is_shared=False)},
    batch_size=torch.Size([11]),
    device=None,
    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 = TensorStorage(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=None,
    is_shared=False)
attach(buffer: Any) None

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

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

引數:

buffer – 從此儲存讀取資料的物件。

dump(*args, **kwargs)

dumps() 的別名。

load(*args, **kwargs)

loads() 的別名。

save(*args, **kwargs)

dumps() 的別名。

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並解答您的疑問

檢視資源