快捷方式

TD1Estimator

class torchrl.objectives.value.TD1Estimator(*args, **kwargs)[原始碼]

優勢函數的 \(\infty\)-時間差 (TD(1)) 估計。

關鍵字引數:
  • gamma (純量) – 指數平均折扣。

  • value_network (TensorDictModule) – 用於檢索值估計值的值運算子。

  • average_rewards (bool, 選用) – 如果 True,獎勵將在計算 TD 之前進行標準化。

  • differentiable (bool, 選用) –

    如果 True,梯度將通過值函數的計算傳播。預設值為 False

    注意

    使函數呼叫不可微分的正確方法是在 torch.no_grad() 上下文管理器/裝飾器中裝飾它,或為函數模組傳遞分離的參數。

  • skip_existing (bool, 選用) – 如果 True,值網路將跳過 tensordict 中已存在輸出的模組。預設值為 None,即 tensordict.nn.skip_existing() 的值不受影響。

  • advantage_key (strstr 的 tuple, 選用) – [已棄用] 優勢項目的鍵。預設值為 "advantage"

  • value_target_key (strstr 的 tuple, 選用) – [已棄用] 優勢項目的鍵。預設值為 "value_target"

  • value_key (strstr 的 tuple, 選用) – [已棄用] 要從輸入 tensordict 讀取的值鍵。預設值為 "state_value"

  • shifted (bool, 選用) – 如果 True,則使用對值網路的單一呼叫來估計值和下一個值。這更快,但僅在 (1) "next" 值僅移動一個時間步長時有效(例如,多步值估計的情況並非如此),並且 (2) 在時間 tt+1 使用的參數相同時有效(在使用目標參數時並非如此)。預設值為 False

  • device (torch.device, 選用) – 模組的裝置。

  • time_dim (int, optional) – 輸入 tensordict 中對應時間的維度。如果未提供,預設為標記有 "time" 名稱的維度 (如果有的話),否則為最後一個維度。可以在呼叫 value_estimate() 時覆蓋。負數維度是相對於輸入 tensordict 而言。

forward(tensordict: TensorDictBase = None, *, params: tensordict.base.TensorDictBase | None = None, target_params: tensordict.base.TensorDictBase | None = None) TensorDictBase[source]

計算給定 tensordict 中資料的 TD(1) 優勢。

如果提供了函數式模組,則包含參數(以及相關的目標參數)的巢狀 TensorDict 可以傳遞給該模組。

參數:

tensordict (TensorDictBase) – 一個包含資料的 TensorDict(一個觀察鍵, "action", ("next", "reward"), ("next", "done"), ("next", "terminated"), 和環境回傳的 "next" tensordict 狀態)用於計算價值估計和 TDEstimate。傳遞到此模組的資料結構應為 [*B, T, *F],其中 B 是批次大小, T 是時間維度, F 是特徵維度。 tensordict 的形狀必須是 [*B, T]

關鍵字引數:
  • params (TensorDictBase, optional) – 一個巢狀 TensorDict,包含要傳遞到函數式價值網路模組的參數。

  • target_params (TensorDictBase, optional) – 一個巢狀 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, "reward": reward, "terminated": terminated}}, [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, time_dim: Optional[int] = 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 – 要傳遞給價值網路的關鍵字引數。

回傳:對應於狀態價值的 tensor。

文件

存取 PyTorch 的完整開發人員文件

檢視文件

教學課程

取得適用於初學者和高級開發人員的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得您的問題解答

檢視資源