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