快捷方式

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 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並解答您的疑問

檢視資源