DiscreteCQLLoss¶
- class torchrl.objectives.DiscreteCQLLoss(*args, **kwargs)[源代码]¶
TorchRL 对离散 CQL 损失的实现。
此类实现了离散保守 Q 学习 (CQL) 损失函数,如论文“用于离线强化学习的保守 Q 学习”(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 OneHotDiscreteTensorSpec >>> from torchrl.objectives import DiscreteCQLLoss >>> n_obs, n_act = 4, 3 >>> value_net = MLP(in_features=n_obs, out_features=n_act) >>> spec = OneHotDiscreteTensorSpec(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 OneHotDiscreteTensorSpec >>> from torch import nn >>> import torch >>> n_obs = 3 >>> n_action = 4 >>> action_spec = OneHotDiscreteTensorSpec(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) TensorDict [源代码]¶
计算给定从重放缓冲区采样的 tensordict 的 (DQN) CQL 损失。
- 此函数还将写入一个“td_error”键,可由优先重放缓冲区用于为 tensordict 中的项目分配
优先级。
- 参数:
tensordict (TensorDictBase) – 包含键 [“action”] 以及值网络的 in_keys(观察结果,“done”,“terminated”,“reward” 在“next” tensordict 中)的 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)