DiscreteCQLLoss¶
- class torchrl.objectives.DiscreteCQLLoss(*args, **kwargs)[原始碼]¶
離散 CQL 損失的 TorchRL 實作。
此類別實作離散保守 Q 學習 (CQL) 損失函數,如論文「離線強化學習的保守 Q 學習」(Conservative Q-Learning for Offline Reinforcement Learning) 中所呈現 (https://arxiv.org/abs/2006.04779)。
- 參數:
value_network (Union[QValueActor, nn.Module]) – 用於估計狀態-動作值的 Q 值網路。
- 關鍵字引數:
loss_function (Optional[str]) – 用於計算預測 Q 值和目標 Q 值之間距離的距離函數。預設為
l2
。delay_value (bool) – 是否將目標 Q 值網路與用於資料收集的 Q 值網路分離。預設值為
True
。gamma (float, optional) – 折扣因子。預設值為
None
。action_space – 環境的動作空間。如果為 None,則從值網路推斷。預設為 None。
reduction (str, optional) – 指定要應用於輸出的縮減:
"none"
|"mean"
|"sum"
。"none"
:不套用縮減,"mean"
:輸出總和將除以輸出中的元素數量,"sum"
:輸出將被加總。預設值:"mean"
。
範例
>>> from torchrl.modules import MLP, QValueActor >>> from torchrl.data import OneHot >>> from torchrl.objectives import DiscreteCQLLoss >>> n_obs, n_act = 4, 3 >>> value_net = MLP(in_features=n_obs, out_features=n_act) >>> spec = OneHot(n_act) >>> actor = QValueActor(value_net, in_keys=["observation"], action_space=spec) >>> loss = DiscreteCQLLoss(actor, action_space=spec) >>> batch = [10,] >>> data = TensorDict({ ... "observation": torch.randn(*batch, n_obs), ... "action": spec.rand(batch), ... ("next", "observation"): torch.randn(*batch, n_obs), ... ("next", "done"): torch.zeros(*batch, 1, dtype=torch.bool), ... ("next", "terminated"): torch.zeros(*batch, 1, dtype=torch.bool), ... ("next", "reward"): torch.randn(*batch, 1) ... }, batch) >>> loss(data) TensorDict( fields={ loss_cql: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, is_shared=False), loss_qvalue: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, is_shared=False), pred_value: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, is_shared=False), target_value: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, is_shared=False), td_error: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([]), device=None, is_shared=False)
此類別也與非基於 tensordict 的模組相容,並且可以在不使用任何與 tensordict 相關的原始元素的情況下使用。在這種情況下,預期的關鍵字引數為:
["observation", "next_observation", "action", "next_reward", "next_done", "next_terminated"]
,並且傳回單一損失值。範例
>>> from torchrl.objectives import DiscreteCQLLoss >>> from torchrl.data import OneHot >>> from torch import nn >>> import torch >>> n_obs = 3 >>> n_action = 4 >>> action_spec = OneHot(n_action) >>> value_network = nn.Linear(n_obs, n_action) # a simple value model >>> dcql_loss = DiscreteCQLLoss(value_network, action_space=action_spec) >>> # define data >>> observation = torch.randn(n_obs) >>> next_observation = torch.randn(n_obs) >>> action = action_spec.rand() >>> next_reward = torch.randn(1) >>> next_done = torch.zeros(1, dtype=torch.bool) >>> next_terminated = torch.zeros(1, dtype=torch.bool) >>> loss_val = dcql_loss( ... observation=observation, ... next_observation=next_observation, ... next_reward=next_reward, ... next_done=next_done, ... next_terminated=next_terminated, ... action=action)
- forward(tensordict: TensorDictBase = None) TensorDict [原始碼]¶
計算從重播緩衝區取樣的 tensordict 的 (DQN) CQL 損失。
- 此函數還會寫入一個 “td_error” 鍵,優先重播緩衝區可以使用該鍵為 tensordict 中的項目分配
優先權。
- 參數:
tensordict (TensorDictBase) – 一個 tensordict,其鍵為 ["action"],且包含數值網路的 in_keys (observations, “done”, “terminated”, “reward” 在一個 “next” tensordict 中)。
- 回傳:
包含 CQL 損失的張量。
- make_value_estimator(value_type: Optional[ValueEstimators] = None, **hyperparams)[source]¶
數值函數建構子。
如果想要使用非預設的數值函數,則必須使用此方法建立。
- 參數:
value_type (ValueEstimators) – 一個
ValueEstimators
列舉類型,指示要使用的數值函數。 如果未提供,將使用儲存在default_value_estimator
屬性中的預設值。 產生的數值估算器類別將在self.value_type
中註冊,以便將來進行改進。**hyperparams – 用於數值函數的超參數。 如果未提供,將使用
default_value_kwargs()
指示的值。
範例
>>> from torchrl.objectives import DQNLoss >>> # initialize the DQN loss >>> actor = torch.nn.Linear(3, 4) >>> dqn_loss = DQNLoss(actor, action_space="one-hot") >>> # updating the parameters of the default value estimator >>> dqn_loss.make_value_estimator(gamma=0.9) >>> dqn_loss.make_value_estimator( ... ValueEstimators.TD1, ... gamma=0.9) >>> # if we want to change the gamma value >>> dqn_loss.make_value_estimator(dqn_loss.value_type, gamma=0.9)