terminated_or_truncated¶
- torchrl.envs.utils.terminated_or_truncated(data: TensorDictBase, full_done_spec: Optional[TensorSpec] = None, key: str = '_reset', write_full_false: bool = False) bool [source]¶
读取 tensordict 中的 done / terminated / truncated 键,并写入一个新的张量,其中聚合了两个信号的值。
修改发生在提供的 TensorDict 实例中。此函数可用于计算批量或多智能体设置中的“_reset”信号,因此输出键的默认名称。
- 参数:
data (TensorDictBase) – 输入数据,通常来自调用
step()
的结果。full_done_spec (TensorSpec, optional) – 来自 env 的 done_spec,指示必须在何处找到 done leaves。如果未提供,则将在数据中搜索默认的
"done"
、"terminated"
和"truncated"
条目。key (NestedKey, optional) –
聚合结果应写入的位置。如果
None
,则该函数将不写入任何键,而仅输出任何 done 值是否为 true。 .. note:: 如果key
条目已存在值,则前一个值将占主导地位,并且不会实现更新。
write_full_false (bool, optional) – 如果
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)