TD0Estimator¶
- class torchrl.objectives.value.TD0Estimator(*args, **kwargs)[source]¶
優勢函數的時間差 (TD(0)) 估計。
又稱為引導式時間差或 1 步回報。
- 關鍵字引數:
gamma (純量) – 指數平均折扣。
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
,則 value network 會跳過 tensordict 中已存在的輸出模組。預設值為None
,亦即tensordict.nn.skip_existing()
的值不受影響。advantage_key (str 或 str 的 tuple, optional) – [已棄用] 優勢條目的鍵值。預設值為
"advantage"
。value_target_key (str 或 str 的 tuple, optional) – [已棄用] 優勢條目的鍵值。預設值為
"value_target"
。value_key (str 或 str 的 tuple, optional) – [已棄用] 要從輸入 tensordict 讀取的 value 鍵值。預設值為
"state_value"
。device (torch.device, optional) – 模組的 device。
- forward(tensordict: TensorDictBase = None, *, params: tensordict.base.TensorDictBase | None = None, target_params: tensordict.base.TensorDictBase | None = None) TensorDictBase [source]¶
計算給定 tensordict 中資料的 TD(0) 優勢。
如果提供了函數式模組,則包含參數 (如果相關,則包含目標參數) 的巢狀 TensorDict 可以傳遞給該模組。
- 參數:
tensordict (TensorDictBase) – 包含資料的 TensorDict(觀察鍵、
"action"
、("next", "reward")
、("next", "done")
、("next", "terminated")
以及環境返回的"next"
tensordict 狀態),這些資料是用於計算 value 估計值和 TDEstimate 所必需的。 傳遞給此模組的資料結構應為[*B, T, *F]
,其中B
是批次大小,T
是時間維度,而F
是特徵維度。tensordict 的形狀必須為[*B, T]
。- 關鍵字引數:
params (TensorDictBase, optional) – 包含要傳遞給函數式 value network 模組的參數的巢狀 TensorDict。
target_params (TensorDictBase, optional) – 包含要傳遞給函數式 value network 模組的目標參數的巢狀 TensorDict。
- 回傳:
更新後的 TensorDict,其中包含建構函式中定義的優勢和 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: Optional[TensorDictBase] = None, next_value: Optional[Tensor] = None, **kwargs)[source]¶
取得 value 估計值,通常用作 value network 的目標值。
如果 state value 鍵存在於
tensordict.get(("next", self.tensor_keys.value))
下,則將使用此值,而無需重新使用 value network。- 參數:
tensordict (TensorDictBase) – 包含要讀取資料的 tensordict。
target_params (TensorDictBase, optional) – 包含要傳遞給函數式 value network 模組的目標參數的巢狀 TensorDict。
next_value (torch.Tensor, optional) – 下一個狀態或狀態-動作對的值。與
target_params
互斥。**kwargs – 要傳遞給 value network 的關鍵字引數。
回傳: 對應於 state value 的 tensor。