快捷方式

Stack

class torchrl.envs.transforms.Stack(in_keys: Sequence[NestedKey], out_key: NestedKey, in_key_inv: NestedKey | None = None, out_keys_inv: Sequence[NestedKey] | None = None, dim: int = - 1, allow_positive_dim: bool = False, *, del_keys: bool = True)[原始碼]

堆疊張量和 tensordicts。

沿新維度連線一系列張量或 tensordicts。 in_keys 下的 tensordicts 或張量必須具有相同的形狀。

此轉換僅將輸入堆疊到一個輸出鍵中。將多組輸入鍵堆疊到不同的輸出鍵需要多個轉換。

此轉換對於具有不同鍵下具有相同規格的多個智慧體的環境很有用。智慧體的規格和 tensordicts 可以堆疊在一個共享鍵下,以便執行期望張量(如觀察、獎勵等)包含所有智慧體的批處理資料的 MARL 演算法。

引數:
  • in_keys (NestedKey 序列) – 要堆疊的鍵。

  • out_key (NestedKey) – 結果堆疊條目的鍵。

  • in_key_inv (NestedKey, 可選) – 在呼叫 inv() 時要解堆疊的鍵。預設為 None

  • out_keys_inv (NestedKey 序列, 可選) – 呼叫 inv() 呼叫後解堆疊條目的結果鍵。預設為 None

  • dim (int, 可選) – 要插入的維度。預設為 -1

  • allow_positive_dim (bool, 可選) – 如果為 True,則接受正維度。預設為 False,即不允許非負維度。

關鍵字引數:

del_keys (bool, 可選) – 如果為 True,則輸入值將在堆疊後被刪除。預設為 True

示例

>>> import torch
>>> from tensordict import TensorDict
>>> from torchrl.envs import Stack
>>> td = TensorDict({"key1": torch.zeros(3), "key2": torch.ones(3)}, [])
>>> td
TensorDict(
    fields={
        key1: Tensor(shape=torch.Size([3]), device=cpu, dtype=torch.float32, is_shared=False),
        key2: Tensor(shape=torch.Size([3]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([]),
    device=None,
    is_shared=False)
>>> transform = Stack(in_keys=["key1", "key2"], out_key="out", dim=-2)
>>> transform(td)
TensorDict(
    fields={
        out: Tensor(shape=torch.Size([2, 3]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([]),
    device=None,
    is_shared=False)
>>> td["out"]
tensor([[0., 0., 0.],
        [1., 1., 1.]])
>>> agent_0 = TensorDict({"obs": torch.rand(4, 5), "reward": torch.zeros(1)})
>>> agent_1 = TensorDict({"obs": torch.rand(4, 5), "reward": torch.zeros(1)})
>>> td = TensorDict({"agent_0": agent_0, "agent_1": agent_1})
>>> transform = Stack(in_keys=["agent_0", "agent_1"], out_key="agents")
>>> transform(td)
TensorDict(
    fields={
        agents: TensorDict(
            fields={
                obs: Tensor(shape=torch.Size([2, 4, 5]), device=cpu, dtype=torch.float32, is_shared=False),
                reward: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.float32, is_shared=False)},
            batch_size=torch.Size([2]),
            device=None,
            is_shared=False)},
    batch_size=torch.Size([]),
    device=None,
    is_shared=False)
forward(next_tensordict: TensorDictBase) TensorDictBase

讀取輸入 tensordict,並對選定的鍵應用轉換。

預設情況下,此方法

  • 直接呼叫 _apply_transform()

  • 不呼叫 _step()_call()

此方法不會在任何時候在 env.step 中呼叫。但是,它會在 sample() 中呼叫。

注意

forward 也可以使用 dispatch 將引數名稱轉換為鍵,並使用常規關鍵字引數。

示例

>>> class TransformThatMeasuresBytes(Transform):
...     '''Measures the number of bytes in the tensordict, and writes it under `"bytes"`.'''
...     def __init__(self):
...         super().__init__(in_keys=[], out_keys=["bytes"])
...
...     def forward(self, tensordict: TensorDictBase) -> TensorDictBase:
...         bytes_in_td = tensordict.bytes()
...         tensordict["bytes"] = bytes
...         return tensordict
>>> t = TransformThatMeasuresBytes()
>>> env = env.append_transform(t) # works within envs
>>> t(TensorDict(a=0))  # Works offline too.
transform_done_spec(done_spec: TensorSpec) TensorSpec[原始碼]

變換 done spec,使結果 spec 與變換對映匹配。

引數:

done_spec (TensorSpec) – 變換前的 spec

返回:

轉換後的預期規範

transform_input_spec(input_spec: TensorSpec) TensorSpec[原始碼]

轉換輸入規範,使結果規範與轉換對映匹配。

引數:

input_spec (TensorSpec) – 轉換前的規範

返回:

轉換後的預期規範

transform_observation_spec(observation_spec: TensorSpec) TensorSpec[原始碼]

轉換觀察規範,使結果規範與轉換對映匹配。

引數:

observation_spec (TensorSpec) – 轉換前的規範

返回:

轉換後的預期規範

transform_reward_spec(reward_spec: TensorSpec) TensorSpec[原始碼]

轉換獎勵的 spec,使其與變換對映匹配。

引數:

reward_spec (TensorSpec) – 變換前的 spec

返回:

轉換後的預期規範

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源