快捷方式

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 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源