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)