TD0Estimator¶
- class torchrl.objectives.value.TD0Estimator(*args, **kwargs)[source]¶
優勢函式的時序差分 (TD(0)) 估計。
也稱為自舉時序差分或單步回報。
- 關鍵字引數:
gamma (scalar) – 折扣因子。
value_network (TensorDictModule) – 用於獲取價值估計的價值運算元。
shifted (bool, optional) – 如果
True,則透過一次呼叫價值網路來估計價值和下一個價值。這更快,但僅在以下情況有效:(1) “next” 價值僅偏移了一個時間步長(例如,多步價值估計就不是這種情況);以及 (2) 時間t和t+1使用的引數相同(使用目標引數時不是這種情況)。預設為False。average_rewards (bool, optional) – 如果
True,則在計算 TD 之前會對獎勵進行標準化。differentiable (bool, optional) –
如果
True,則在價值函式的計算過程中傳播梯度。預設為False。注意
使函式呼叫不可微分的正確方法是將其包含在 torch.no_grad() 上下文管理器/裝飾器中,或為函式式模組傳遞分離的引數。
skip_existing (bool, optional) – 如果
True,則價值網路將跳過其輸出已存在於 tensordict 中的模組。預設為None,即tensordict.nn.skip_existing()的值不受影響。advantage_key (str or tuple of str, optional) – [已棄用] 優勢條目的鍵。預設為
"advantage"。value_target_key (str or tuple of str, optional) – [已棄用] 優勢條目的鍵。預設為
"value_target"。value_key (str or tuple of str, optional) – [已棄用] 要從輸入 tensordict 讀取的價值鍵。預設為
"state_value"。device (torch.device, optional) – 將例項化緩衝區的裝置。預設為
torch.get_default_device()。
- forward(tensordict=None, *, params: TensorDictBase | None = None, target_params: TensorDictBase | None = None)[source]¶
根據 tensordict 中的資料計算 TD(0) 優勢。
如果提供了函式式模組,則可以將包含引數(以及相關的目標引數)的巢狀 TensorDict 傳遞給該模組。
- 引數:
tensordict (TensorDictBase) – 一個 TensorDict,包含計算價值估計和 TDEstimate 所需的資料(觀察鍵、
"action"、("next", "reward")、("next", "done")、("next", "terminated")以及環境返回的"next"tensordict 狀態)。傳遞給此模組的資料應結構化為[*B, T, *F],其中B是批處理大小,T是時間維度,F是特徵維度。tensordict 的形狀必須為[*B, T]。- 關鍵字引數:
params (TensorDictBase, optional) – 一個巢狀的 TensorDict,包含要傳遞給函式式價值網路模組的引數。
target_params (TensorDictBase, optional) – 一個巢狀的 TensorDict,包含要傳遞給函式式價值網路模組的目標引數。
- 返回:
一個更新後的 TensorDict,包含建構函式中定義的 advantage 和 value_error 鍵。
示例
>>> from tensordict import TensorDict >>> value_net = TensorDictModule( ... nn.Linear(3, 1), in_keys=["obs"], out_keys=["state_value"] ... ) >>> module = TDEstimate( ... gamma=0.98, ... value_network=value_net, ... ) >>> obs, next_obs = torch.randn(2, 1, 10, 3) >>> reward = torch.randn(1, 10, 1) >>> done = torch.zeros(1, 10, 1, dtype=torch.bool) >>> terminated = torch.zeros(1, 10, 1, dtype=torch.bool) >>> tensordict = TensorDict({"obs": obs, "next": {"obs": next_obs, "done": done, "terminated": terminated, "reward": reward}}, [1, 10]) >>> _ = module(tensordict) >>> assert "advantage" in tensordict.keys()
該模組也支援非 tensordict(即解包的 tensordict)輸入
示例
>>> value_net = TensorDictModule( ... nn.Linear(3, 1), in_keys=["obs"], out_keys=["state_value"] ... ) >>> module = TDEstimate( ... gamma=0.98, ... value_network=value_net, ... ) >>> obs, next_obs = torch.randn(2, 1, 10, 3) >>> reward = torch.randn(1, 10, 1) >>> done = torch.zeros(1, 10, 1, dtype=torch.bool) >>> terminated = torch.zeros(1, 10, 1, dtype=torch.bool) >>> advantage, value_target = module(obs=obs, next_reward=reward, next_done=done, next_obs=next_obs, next_terminated=terminated)
- value_estimate(tensordict, target_params: TensorDictBase | None = None, next_value: torch.Tensor | None = None, **kwargs)[source]¶
獲取價值估計,通常用作價值網路的目標價值。
如果狀態價值鍵存在於
tensordict.get(("next", self.tensor_keys.value))下,則將直接使用該價值,而無需呼叫價值網路。- 引數:
tensordict (TensorDictBase) – 包含要讀取資料的 tensordict。
target_params (TensorDictBase, optional) – 一個巢狀的 TensorDict,包含要傳遞給函式式價值網路模組的目標引數。
next_value (torch.Tensor, optional) – 下一個狀態或狀態-動作對的價值。與
target_params互斥。**kwargs – 將傳遞給價值網路的關鍵字引數。
返回:與狀態價值對應的張量。