快捷方式

DistributionalQValueModule

class torchrl.modules.tensordict_module.DistributionalQValueModule(*args, **kwargs)[原始碼]

Q 值 Actor 策略的分佈 Q 值 Hook。

此模組將包含動作值 logits 的張量處理為其 argmax 分量(即,結果貪婪動作),遵循給定的動作空間(獨熱、二進位制或分類)。它適用於 tensordict 和常規張量。

輸入動作值預計是 log-softmax 操作的結果。

有關分散式的 DQN 的更多詳細資訊,請參閱“從分佈角度看強化學習”,“https://arxiv.org/pdf/1707.06887.pdf

引數:
  • action_space (str, optional) – 動作空間。必須是 "one-hot""mult-one-hot""binary""categorical" 之一。此引數與 spec 互斥,因為 spec 條件化了 action_space。

  • support (torch.Tensor) – 動作值的支援集。

  • action_value_key (strtuple of str, optional) – 表示動作值的輸入鍵。預設為 "action_value"

  • action_mask_key (str or tuple of str, optional) – 表示動作掩碼的輸入鍵。預設為 "None"(相當於沒有掩碼)。

  • out_keys (list of strtuple of str, optional) – 表示動作和動作值的輸出鍵。預設為 ["action", "action_value"]

  • var_nums (int, optional) – 如果 action_space = "mult-one-hot",此值表示每個動作分量的基數。

  • spec (TensorSpec, optional) – 如果提供,則為動作(和/或其他輸出)的 spec。這與 action_space 是互斥的,因為 spec 是 action_space 的條件。

  • safe (bool) – 如果為 True,則輸出值將根據輸入 spec 進行檢查。由於探索策略或數值下溢/溢位問題,可能會發生域外取樣。如果此值超出界限,它將使用 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)
forward(tensordict: Tensor = None) TensorDictBase[原始碼]

定義每次呼叫時執行的計算。

所有子類都應重寫此方法。

注意

儘管前向傳播的實現需要在此函式中定義,但您應該在之後呼叫 Module 例項而不是此函式,因為前者會處理註冊的鉤子,而後者則會靜默忽略它們。

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源