PermuteTransform¶
- class torchrl.envs.transforms.PermuteTransform(dims, in_keys=None, out_keys=None, in_keys_inv=None, out_keys_inv=None)[source]¶
置換轉換。
沿著所需的維度置換輸入張量。置換必須沿著特徵維度 (而非批次維度) 提供。
- 參數:
dims (list of int) – 維度的置換順序。必須是維度
[-(len(dims)), ..., -1]
的重新排序。in_keys (list of NestedKeys) – 輸入條目 (讀取)。
out_keys (list of NestedKeys) – 輸入條目 (寫入)。如果未提供,則預設為
in_keys
。in_keys_inv (list of NestedKeys) – 在
inv()
呼叫期間的輸入條目 (讀取)。out_keys_inv (list of NestedKeys) – 在
inv()
呼叫期間的輸入條目 (寫入)。如果未提供,則預設為in_keys_in
。
範例
>>> from torchrl.envs.libs.gym import GymEnv >>> base_env = GymEnv("ALE/Pong-v5") >>> base_env.rollout(2) TensorDict( fields={ action: Tensor(shape=torch.Size([2, 6]), device=cpu, dtype=torch.int64, is_shared=False), done: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.bool, is_shared=False), next: TensorDict( fields={ done: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.bool, is_shared=False), pixels: Tensor(shape=torch.Size([2, 210, 160, 3]), device=cpu, dtype=torch.uint8, is_shared=False), reward: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([2]), device=cpu, is_shared=False), pixels: Tensor(shape=torch.Size([2, 210, 160, 3]), device=cpu, dtype=torch.uint8, is_shared=False)}, batch_size=torch.Size([2]), device=cpu, is_shared=False) >>> env = TransformedEnv(base_env, PermuteTransform((-1, -3, -2), in_keys=["pixels"])) >>> env.rollout(2) # channels are at the end TensorDict( fields={ action: Tensor(shape=torch.Size([2, 6]), device=cpu, dtype=torch.int64, is_shared=False), done: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.bool, is_shared=False), next: TensorDict( fields={ done: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.bool, is_shared=False), pixels: Tensor(shape=torch.Size([2, 3, 210, 160]), device=cpu, dtype=torch.uint8, is_shared=False), reward: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([2]), device=cpu, is_shared=False), pixels: Tensor(shape=torch.Size([2, 3, 210, 160]), device=cpu, dtype=torch.uint8, is_shared=False)}, batch_size=torch.Size([2]), device=cpu, is_shared=False)
- transform_input_spec(input_spec)[source]¶
轉換輸入規格,使產生的規格與轉換對應匹配。
- 參數:
input_spec (TensorSpec) – 轉換前的規格
- 傳回:
轉換後預期的規格
- transform_observation_spec(observation_spec: TensorSpec) TensorSpec [source]¶
轉換 observation spec,使產生的 spec 符合轉換映射。
- 參數:
observation_spec (TensorSpec) – 轉換前的 spec
- 傳回:
轉換後預期的規格