快捷方式

DiscreteCQLLoss

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

離散 CQL 損失的 TorchRL 實現。

此類實現了離散保守 Q-learning (CQL) 損失函式,如論文“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)
default_keys

別名:_AcceptedKeys

forward(tensordict: TensorDictBase = None) TensorDict[原始碼]

給定從回放緩衝區取樣的 tensordict,計算 (DQN) CQL 損失。

此函式還將寫入一個 “td_error” 鍵,該鍵可用於優先順序回放緩衝區為 tensordict 中的項分配

優先順序。

引數:

tensordict (TensorDictBase) – 一個包含鍵 [“action”] 和值網路的 in_keys(觀察值、“done”、“terminated”、“reward”)的 tensordict(在“next” tensordict 中)。

返回:

包含 CQL 損失的張量。

make_value_estimator(value_type: Optional[ValueEstimators] = None, **hyperparams)[原始碼]

值函式建構函式。

如果需要非預設值函式,必須使用此方法構建。

引數:
  • 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)

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源