DistributionalQValueModule¶
- class torchrl.modules.tensordict_module.DistributionalQValueModule(*args, **kwargs)[source]¶
用于 Q 值策略的分布式 Q 值 Hook。
此模块根据给定的动作空间(one-hot、binary 或 categorical),将包含动作值 logits 的张量处理为其 argmax 分量(即生成的贪婪动作)。它既适用于 tensordict,也适用于常规张量。
输入动作值预期是 log-softmax 操作的结果。
有关分布式 DQN 的更多详细信息,请参阅论文《A Distributional Perspective on Reinforcement Learning》,链接:https://arxiv.org/pdf/1707.06887.pdf
- 参数:
action_space (str, optional) – 动作空间。必须是
"one-hot"
、"mult-one-hot"
、"binary"
或"categorical"
之一。此参数与spec
参数互斥,因为spec
会限定动作空间。support (torch.Tensor) – 动作值的支撑(support)。
action_value_key (str 或 tuple of str, optional) – 表示动作值的输入键。默认为
"action_value"
。action_mask_key (str 或 tuple of str, optional) – 表示动作掩码的输入键。默认为
"None"
(相当于没有掩码)。out_keys (list of str 或 tuple of str, optional) – 表示动作和动作值的输出键。默认为
["action", "action_value"]
。var_nums (int, optional) – 如果
action_space = "mult-one-hot"
,此值表示每个动作分量的基数(cardinality)。spec (TensorSpec, optional) – 如果提供,则为动作(和/或其他输出)的规范(spec)。此参数与
action_space
互斥,因为 spec 会限定动作空间。safe (bool) – 如果为
True
,则检查输出值是否符合输入规范。由于探索策略或数值下溢/上溢问题,可能会发生超出范围的采样。如果此值超出范围,则使用TensorSpec.project
方法将其投影回所需空间。默认为False
。
示例
>>> from tensordict import TensorDict >>> torch.manual_seed(0) >>> action_space = "categorical" >>> action_value_key = "my_action_value" >>> support = torch.tensor([-1, 0.0, 1.0]) # the action value is between -1 and 1 >>> actor = DistributionalQValueModule(action_space, support=support, action_value_key=action_value_key) >>> # This module works with both tensordict and regular tensors: >>> value = torch.full((3, 4), -100) >>> # the first bin (-1) of the first action is high: there's a high chance that it has a low value >>> value[0, 0] = 0 >>> # the second bin (0) of the second action is high: there's a high chance that it has an intermediate value >>> value[1, 1] = 0 >>> # the third bin (0) of the this action is high: there's a high chance that it has an high value >>> value[2, 2] = 0 >>> actor(my_action_value=value) (tensor(2), tensor([[ 0, -100, -100, -100], [-100, 0, -100, -100], [-100, -100, 0, -100]])) >>> actor(value) (tensor(2), tensor([[ 0, -100, -100, -100], [-100, 0, -100, -100], [-100, -100, 0, -100]])) >>> actor(TensorDict({action_value_key: value}, [])) TensorDict( fields={ action: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int64, is_shared=False), my_action_value: Tensor(shape=torch.Size([3, 4]), device=cpu, dtype=torch.int64, is_shared=False)}, batch_size=torch.Size([]), device=None, is_shared=False)