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 演算法。
- 引數:
- 關鍵字引數:
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
- 返回:
轉換後的預期規範