DiscreteIQLLoss¶
- class torchrl.objectives.DiscreteIQLLoss(*args, **kwargs)[源代码]¶
TorchRL 中离散 IQL 损失的实现。
在“具有隐式 Q 学习的离线强化学习”中提出 https://arxiv.org/abs/2110.06169
- 参数:
actor_network (ProbabilisticActor) – 随机执行器
qvalue_network (TensorDictModule) – Q(s, a) 参数模型。
value_network (TensorDictModule, 可选) – V(s) 参数模型。
- 关键字参数:
action_space (str 或 TensorSpec) – 动作空间。必须是
"one-hot"
、"mult_one_hot"
、"binary"
或"categorical"
之一,或对应规格的实例 (torchrl.data.OneHotDiscreteTensorSpec
、torchrl.data.MultiOneHotDiscreteTensorSpec
、torchrl.data.BinaryDiscreteTensorSpec
或torchrl.data.DiscreteTensorSpec
)。num_qvalue_nets (整数, 可选) – 使用的 Q 值网络数量。默认值为
2
。loss_function (str, 可选) – 用于值函数损失的损失函数。默认值为 “smooth_l1”。
temperature (浮点数, 可选) – 逆温度 (beta)。对于较小的超参数值,目标函数的行为类似于行为克隆,而对于较大的值,它试图恢复 Q 函数的最大值。
expectile (浮点数, 可选) – 期望值 \(\tau\)。较大的 \(\tau\) 值对于需要动态规划(“stichting”)的 antmaze 任务至关重要。
priority_key (str, 可选) – [已弃用,请改用 .set_keys(priority_key=priority_key) 而不是] 应该写入优先级 (用于优先级回放缓冲区使用) 的 tensordict 键。默认值为 “td_error”。
separate_losses (bool, 可选) – 如果为
True
,则策略和评论家之间共享的参数将仅在策略损失上进行训练。默认值为False
,即梯度将针对策略和评论家损失传播到共享参数。reduction (str, 可选) – 指定要应用于输出的缩减:
"none"
|"mean"
|"sum"
。"none"
:不应用任何缩减,"mean"
:输出的总和将除以输出中元素的数量,"sum"
:输出将被求和。默认值:"mean"
。
示例
>>> import torch >>> from torch import nn >>> from torchrl.data.tensor_specs import OneHotDiscreteTensorSpec >>> from torchrl.modules.distributions.discrete import OneHotCategorical >>> from torchrl.modules.tensordict_module.actors import ProbabilisticActor >>> from torchrl.modules.tensordict_module.common import SafeModule >>> from torchrl.objectives.iql import DiscreteIQLLoss >>> from tensordict import TensorDict >>> n_act, n_obs = 4, 3 >>> spec = OneHotDiscreteTensorSpec(n_act) >>> module = SafeModule(nn.Linear(n_obs, n_act), in_keys=["observation"], out_keys=["logits"]) >>> actor = ProbabilisticActor( ... module=module, ... in_keys=["logits"], ... out_keys=["action"], ... spec=spec, ... distribution_class=OneHotCategorical) >>> qvalue = SafeModule( ... nn.Linear(n_obs, n_act), ... in_keys=["observation"], ... out_keys=["state_action_value"], ... ) >>> value = SafeModule( ... nn.Linear(n_obs, 1), ... in_keys=["observation"], ... out_keys=["state_value"], ... ) >>> loss = DiscreteIQLLoss(actor, qvalue, value) >>> batch = [2, ] >>> action = spec.rand(batch).long() >>> data = TensorDict({ ... "observation": torch.randn(*batch, n_obs), ... "action": action, ... ("next", "done"): torch.zeros(*batch, 1, dtype=torch.bool), ... ("next", "terminated"): torch.zeros(*batch, 1, dtype=torch.bool), ... ("next", "reward"): torch.randn(*batch, 1), ... ("next", "observation"): torch.randn(*batch, n_obs), ... }, batch) >>> loss(data) TensorDict( fields={ entropy: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, is_shared=False), loss_actor: 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), loss_value: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([]), device=None, is_shared=False)
此类还与非 tensordict 基于的模块兼容,并且可以在不使用任何 tensordict 相关原语的情况下使用。在这种情况下,预期关键字参数为:
["action", "next_reward", "next_done", "next_terminated"]
+ 执行器、值和 q 值网络的 in_keys。返回值是按以下顺序排列的张量元组:["loss_actor", "loss_qvalue", "loss_value", "entropy"]
。示例
>>> import torch >>> import torch >>> from torch import nn >>> from torchrl.data.tensor_specs import OneHotDiscreteTensorSpec >>> from torchrl.modules.distributions.discrete import OneHotCategorical >>> from torchrl.modules.tensordict_module.actors import ProbabilisticActor >>> from torchrl.modules.tensordict_module.common import SafeModule >>> from torchrl.objectives.iql import DiscreteIQLLoss >>> _ = torch.manual_seed(42) >>> n_act, n_obs = 4, 3 >>> spec = OneHotDiscreteTensorSpec(n_act) >>> module = SafeModule(nn.Linear(n_obs, n_act), in_keys=["observation"], out_keys=["logits"]) >>> actor = ProbabilisticActor( ... module=module, ... in_keys=["logits"], ... out_keys=["action"], ... spec=spec, ... distribution_class=OneHotCategorical) >>> qvalue = SafeModule( ... nn.Linear(n_obs, n_act), ... in_keys=["observation"], ... out_keys=["state_action_value"], ... ) >>> value = SafeModule( ... nn.Linear(n_obs, 1), ... in_keys=["observation"], ... out_keys=["state_value"], ... ) >>> loss = DiscreteIQLLoss(actor, qvalue, value) >>> batch = [2, ] >>> action = spec.rand(batch).long() >>> loss_actor, loss_qvalue, loss_value, entropy = loss( ... observation=torch.randn(*batch, n_obs), ... action=action, ... next_done=torch.zeros(*batch, 1, dtype=torch.bool), ... next_terminated=torch.zeros(*batch, 1, dtype=torch.bool), ... next_observation=torch.zeros(*batch, n_obs), ... next_reward=torch.randn(*batch, 1)) >>> loss_actor.backward()
还可以使用
DiscreteIQLLoss.select_out_keys()
方法过滤输出键。示例
>>> _ = loss.select_out_keys('loss_actor', 'loss_qvalue', 'loss_value') >>> loss_actor, loss_qvalue, loss_value = loss( ... observation=torch.randn(*batch, n_obs), ... action=action, ... next_done=torch.zeros(*batch, 1, dtype=torch.bool), ... next_terminated=torch.zeros(*batch, 1, dtype=torch.bool), ... next_observation=torch.zeros(*batch, n_obs), ... next_reward=torch.randn(*batch, 1)) >>> loss_actor.backward()
- forward(tensordict: TensorDictBase) TensorDictBase ¶
它旨在读取输入 TensorDict 并返回另一个包含名为“loss*”的损失键的 tensordict。
将损失分成其组件之后,训练器可以使用它在整个训练过程中记录各种损失值。输出 tensordict 中存在的其他标量也将被记录。
- 参数:
tensordict – 一个包含计算损失所需的值的输入 tensordict。
- 返回值:
一个新的没有批量维度的 tensordict,其中包含将被命名为“loss*”的各种损失标量。在反向传播之前,训练器将读取这些损失,因此使用此名称返回损失至关重要。