快捷方式

Timer

class torchrl.envs.transforms.Timer(out_keys: Optional[Sequence[NestedKey]] = None, time_key: str = 'time')[source]

一个转换,用于测量环境中 invcall 操作之间的时间间隔。

Timer 转换用于跟踪 inv 调用和 call 之间,以及 callinv 调用之间经过的时间。这对于环境中的性能监控和调试非常有用。时间以秒为单位测量,并存储为具有 PyTorch 默认 dtype 的张量。如果 tensordict 具有批次大小(例如,在批处理环境中),则时间将扩展到输入 tensordict 的大小。

变量:
  • out_keys – 逆转换的输出 tensordict 的键。默认为 out_keys = [f”{time_key}_step”, f”{time_key}_policy”, f”{time_key}_reset”],其中第一个键表示在环境中执行一步所需的时间,第二个键表示执行策略所需的时间,第三个键表示 reset 调用所需的时间。

  • time_key – 用于存储时间间隔在 tensordict 中的键的前缀。默认为 “time”

注意

在一系列 rollout 中,reset 的时间标记被写入根目录(在 “next” tensordict 中,“time_reset”条目或等效键始终为 0)。在根目录,当存在 reset 时,“time_policy”和“time_step”条目将为 0。它们在 “next” 中永远不会为 0

示例

>>> from torchrl.envs import Timer, GymEnv
>>>
>>> env = GymEnv("Pendulum-v1").append_transform(Timer())
>>> r = env.rollout(10)
>>> print("time for policy", r["time_policy"])
time for policy tensor([0.0000, 0.0882, 0.0004, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002,
        0.0002])
>>> print("time for step", r["time_step"])
time for step tensor([9.5797e-04, 1.6289e-03, 9.7990e-05, 8.0824e-05, 9.0837e-05, 7.6056e-05,
        8.2016e-05, 7.6056e-05, 8.1062e-05, 7.7009e-05])
forward(tensordict: TensorDictBase) TensorDictBase[source]

读取输入 tensordict,并对选定的键应用转换。

默认情况下,此方法

  • 直接调用 _apply_transform()

  • 不调用 _step()_call()

此方法在任何时候都不会在 env.step 中调用。但是,它会在 sample() 中调用。

注意

forward 也通过使用 dispatch 将参数名称转换为键,与常规关键字参数一起工作。

示例

>>> class TransformThatMeasuresBytes(Transform):
...     '''Measures the number of bytes in the tensordict, and writes it under `"bytes"`.'''
...     def __init__(self):
...         super().__init__(in_keys=[], out_keys=["bytes"])
...
...     def forward(self, tensordict: TensorDictBase) -> TensorDictBase:
...         bytes_in_td = tensordict.bytes()
...         tensordict["bytes"] = bytes
...         return tensordict
>>> t = TransformThatMeasuresBytes()
>>> env = env.append_transform(t) # works within envs
>>> t(TensorDict(a=0))  # Works offline too.
transform_observation_spec(observation_spec: TensorSpec) TensorSpec[source]

转换观察规范,使结果规范与转换映射匹配。

参数:

observation_spec (TensorSpec) – 转换前的规范

返回:

转换后的预期规范

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源