快捷方式

terminated_or_truncated

torchrl.envs.terminated_or_truncated(data: TensorDictBase, full_done_spec: TensorSpec | None = None, key: str = '_reset', write_full_false: bool = False) bool[源代码]

读取 tensordict 中的 done / terminated / truncated 键,并写入一个新张量,其中两个信号的值被聚合。

修改是就地发生在提供的 TensorDict 实例中。此函数可用于在批量或多代理设置中计算“_reset”信号,因此输出键的默认名称。

参数:
  • data (TensorDictBase) – 输入数据,通常是调用 step() 的结果。

  • full_done_spec (TensorSpec, 可选) – env 的 done_spec,指示 done 键的位置。如果未提供,将在数据中搜索默认的 "done""terminated""truncated" 条目。

  • key (NestedKey, 可选) –

    聚合结果应写入的位置。如果为 None,则函数将不写入任何键,而仅输出提供的任何 done 值是否为 true。 .. note:: 如果 key 条目已存在值,

    则先前的值将优先,并且不会实现任何更新。

  • write_full_false (bool, 可选) – 如果为 True,则即使输出为 False(即在提供的数据结构中没有 done 值为 True),重置键也将被写入。默认为 False

返回: 一个布尔值,指示数据中找到的任何 done 状态

包含 True

示例

>>> from torchrl.data.tensor_specs import Categorical
>>> from tensordict import TensorDict
>>> spec = Composite(
...     done=Categorical(2, dtype=torch.bool),
...     truncated=Categorical(2, dtype=torch.bool),
...     nested=Composite(
...         done=Categorical(2, dtype=torch.bool),
...         truncated=Categorical(2, dtype=torch.bool),
...     )
... )
>>> data = TensorDict({
...     "done": True, "truncated": False,
...     "nested": {"done": False, "truncated": True}},
...     batch_size=[]
... )
>>> data = _terminated_or_truncated(data, spec)
>>> print(data["_reset"])
tensor(True)
>>> print(data["nested", "_reset"])
tensor(True)

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源