快捷方式

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 (sequence of NestedKey) – 待重新命名的條目。

  • out_keys (sequence of NestedKey) – 重新命名後的條目名稱。

  • in_keys_inv (sequence of NestedKey, optional) – 在輸入 tensordict 中待重新命名的條目,這些條目將傳遞給 EnvBase._step()

  • out_keys_inv (sequence of NestedKey, optional) – 在輸入 tensordict 中重新命名後的條目名稱。

  • create_copy (bool, optional) – 如果為 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) – 變換前的規格

返回值:

變換後預期的規格

transform_output_spec(output_spec: Composite) Composite[source]

變換輸出規格,使得到的規格與變換對映匹配。

此方法通常應保持不變。修改應使用 transform_observation_spec(), transform_reward_spec()transform_full_done_spec() 實現。:param output_spec: 變換前的規格 :type output_spec: TensorSpec

返回值:

變換後預期的規格

文件

訪問 PyTorch 的完整開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源