terminated_or_truncated¶
- torchrl.envs.utils.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, optional) – 環境的 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)