快捷方式

StepCounter

torchrl.envs.transforms.StepCounter(max_steps: Optional[int] = None, truncated_key: str | None = 'truncated', step_count_key: str | None = 'step_count', update_done: bool =True)[source]

計算重置後的步數,並可選地在達到一定步數後將 truncated 狀態設定為 True

"done" 狀態也會相應調整(因為 done 是任務完成和提前截斷的邏輯或)。

引數:
  • max_steps (int, 可選) – 一個正整數,表示在將 truncated_key 條目設定為 True 之前可採取的最大步數。

  • truncated_key (str, 可選) – 應寫入 truncated 條目的鍵。預設為 "truncated",資料收集器會將其識別為重置訊號。此引數只能是一個字串(不能是巢狀鍵),因為它將與父環境中每個葉子 done 鍵匹配(例如,如果使用 "truncated" 鍵名,("agent", "done") 鍵將伴隨 ("agent", "truncated"))。

  • step_count_key (str, 可選) – 應寫入步數計數條目的鍵。預設為 "step_count"。此引數只能是一個字串(不能是巢狀鍵),因為它將與父環境中每個葉子 done 鍵匹配(例如,如果使用 "step_count" 鍵名,("agent", "done") 鍵將伴隨 ("agent", "step_count"))。

  • update_done (bool, 可選) – 如果為 True,則 "truncated" 級別的 "done" 布林張量將被更新。此訊號表示軌跡已結束,原因可能是任務完成("completed" 條目為 True)或軌跡已被截斷("truncated" 條目為 True)。預設為 True

注意

為確保與具有多個 done_key 的環境相容,此 transform 將為 tensordict 中的每個 done 條目寫入一個 step_count 條目。

示例

>>> import gymnasium
>>> from torchrl.envs import GymWrapper
>>> base_env = GymWrapper(gymnasium.make("Pendulum-v1"))
>>> env = TransformedEnv(base_env,
...     StepCounter(max_steps=5))
>>> rollout = env.rollout(100)
>>> print(rollout)
TensorDict(
    fields={
        action: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.float32, is_shared=False),
        done: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False),
        completed: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
        next: TensorDict(
            fields={
                done: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False),
                completed: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
                observation: Tensor(shape=torch.Size([5, 3]), device=cpu, dtype=torch.float32, is_shared=False),
                reward: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.float32, is_shared=False),
                step_count: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.int64, is_shared=False),
                truncated: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
            batch_size=torch.Size([5]),
            device=cpu,
            is_shared=False),
        observation: Tensor(shape=torch.Size([5, 3]), device=cpu, dtype=torch.float32, is_shared=False),
        step_count: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.int64, is_shared=False),
        truncated: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
    batch_size=torch.Size([5]),
    device=cpu,
    is_shared=False)
>>> print(rollout["next", "step_count"])
tensor([[1],
        [2],
        [3],
        [4],
        [5]])
forward(tensordict: TensorDictBase) TensorDictBase[source]

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

transform_input_spec(input_spec: Composite) Composite[source]

轉換輸入 spec,使結果 spec 與 transform 對映匹配。

引數:

input_spec (TensorSpec) – transform 之前的 spec

返回:

transform 之後預期的 spec

transform_observation_spec(observation_spec: Composite) Composite[source]

轉換觀測 spec,使結果 spec 與 transform 對映匹配。

引數:

observation_spec (TensorSpec) – transform 之前的 spec

返回:

transform 之後預期的 spec

transform_output_spec(output_spec: Composite) Composite[source]

轉換輸出 spec,使結果 spec 與 transform 對映匹配。

通常應保持此方法不變。應使用 transform_observation_spec()transform_reward_spec()transform_full_done_spec() 實現更改。 :param output_spec: transform 之前的 spec :type output_spec: TensorSpec

返回:

transform 之後預期的 spec

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源