RenameTransform¶
- class torchrl.envs.transforms.RenameTransform(in_keys, out_keys, in_keys_inv=None, out_keys_inv=None, create_copy=False)[source]¶
一種轉換,用於重新命名輸出 tensordict 中的條目(或透過反向鍵輸入 tensordict)。
- 參數:
in_keys (NestedKey 序列) – 要重新命名的條目。
out_keys (NestedKey 序列) – 重新命名後條目的名稱。
in_keys_inv (NestedKey 序列, 可選) – 要在輸入 tensordict 中重新命名的條目,這些條目將傳遞給
EnvBase._step()
。out_keys_inv (NestedKey 序列, 可選) – 重新命名後輸入 tensordict 中條目的名稱。
create_copy (bool, 可選) – 如果
True
,則條目將被複製並使用不同的名稱,而不是被重新命名。這允許重新命名不可變的條目,例如"reward"
和"done"
。
範例
>>> from torchrl.envs.libs.gym import GymEnv >>> env = TransformedEnv( ... GymEnv("Pendulum-v1"), ... RenameTransform(["observation", ], ["stuff",], create_copy=False), ... ) >>> tensordict = env.rollout(3) >>> print(tensordict) TensorDict( fields={ action: Tensor(shape=torch.Size([3, 1]), device=cpu, dtype=torch.float32, is_shared=False), done: Tensor(shape=torch.Size([3, 1]), device=cpu, dtype=torch.bool, is_shared=False), next: TensorDict( fields={ done: Tensor(shape=torch.Size([3, 1]), device=cpu, dtype=torch.bool, is_shared=False), reward: Tensor(shape=torch.Size([3, 1]), device=cpu, dtype=torch.float32, is_shared=False), stuff: Tensor(shape=torch.Size([3, 3]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([3]), device=cpu, is_shared=False), stuff: Tensor(shape=torch.Size([3, 3]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([3]), device=cpu, is_shared=False) >>> # if the output is also an input, we need to rename if both ways: >>> from torchrl.envs.libs.brax import BraxEnv >>> env = TransformedEnv( ... BraxEnv("fast"), ... RenameTransform(["state"], ["newname"], ["state"], ["newname"]) ... ) >>> _ = env.set_seed(1) >>> tensordict = env.rollout(3) >>> assert "newname" in tensordict.keys() >>> assert "state" not in tensordict.keys()
- forward(tensordict: TensorDictBase) TensorDictBase ¶
讀取輸入 tensordict,並針對選定的鍵,應用轉換。
- transform_input_spec(input_spec: Composite) Composite [source]¶
轉換輸入規格,使產生的規格與轉換映射相符。
- 參數:
input_spec (TensorSpec) – 轉換前的 spec
- 回傳:
轉換後預期的 spec
- transform_output_spec(output_spec: Composite) Composite [原始碼]¶
轉換 output spec,使結果 spec 符合轉換映射。
這個方法通常不應修改。 更改應使用
transform_observation_spec()
、transform_reward_spec()
和transformfull_done_spec()
來實作。 :param output_spec: 轉換前的 spec :type output_spec: TensorSpec- 回傳:
轉換後預期的 spec