快捷方式

DistributionalQValueHook

class torchrl.modules.DistributionalQValueHook(action_space: str, support: Tensor, var_nums: Optional[int] = None, action_value_key: Optional[NestedKey] = None, action_mask_key: Optional[NestedKey] = None, out_keys: Optional[Sequence[NestedKey]] = None)[源代码]

用于 Q 值策略的 Distributional Q 值 Hook。

给定映射操作符的输出(代表可用不同动作值 bin 的对数概率),DistributionalQValueHook 将使用提供的 support 将这些值转换为它们的 argmax 分量。

有关 Distributional DQN 的更多详细信息,请参阅“A Distributional Perspective on Reinforcement Learning”,https://arxiv.org/pdf/1707.06887.pdf

参数:
  • action_space (str) – 动作空间。必须是 "one-hot""mult-one-hot""binary""categorical" 之一。

  • action_value_key (strtuple of str可选) – 在连接到 TensorDictModule 时使用。表示动作值的输入键。默认为 "action_value"

  • action_mask_key (strtuple of str可选) – 表示动作掩码的输入键。默认为 "None"(等同于没有掩码)。

  • support (torch.Tensor) – 动作值的 support。

  • var_nums (int可选) – 如果 action_space = "mult-one-hot",此值表示每个动作分量的基数。

示例

>>> import torch
>>> from tensordict import TensorDict
>>> from torch import nn
>>> from torchrl.data import OneHot
>>> from torchrl.modules.tensordict_module.actors import DistributionalQValueHook, Actor
>>> td = TensorDict({'observation': torch.randn(5, 4)}, [5])
>>> nbins = 3
>>> class CustomDistributionalQval(nn.Module):
...     def __init__(self):
...         super().__init__()
...         self.linear = nn.Linear(4, nbins*4)
...
...     def forward(self, x):
...         return self.linear(x).view(-1, nbins, 4).log_softmax(-2)
...
>>> module = CustomDistributionalQval()
>>> params = TensorDict.from_module(module)
>>> action_spec = OneHot(4)
>>> hook = DistributionalQValueHook("one_hot", support = torch.arange(nbins))
>>> module.register_forward_hook(hook)
>>> qvalue_actor = Actor(module=module, spec=action_spec, out_keys=["action", "action_value"])
>>> with params.to_module(module):
...     qvalue_actor(td)
>>> print(td)
TensorDict(
    fields={
        action: Tensor(torch.Size([5, 4]), dtype=torch.int64),
        action_value: Tensor(torch.Size([5, 3, 4]), dtype=torch.float32),
        observation: Tensor(torch.Size([5, 4]), dtype=torch.float32)},
    batch_size=torch.Size([5]),
    device=None,
    is_shared=False)

文档

获取 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并解答您的疑问

查看资源