快捷方式

QMixer

class torchrl.modules.QMixer(state_shape: tuple[int, ...] | torch.Size, mixing_embed_dim: int, n_agents: int, device: DEVICE_TYPING)[原始碼]

QMix 混合器。

透過單調超網路將智慧體的區域性 Q 值混合成全域性 Q 值,該超網路的引數來自全域性狀態。摘自論文 https://arxiv.org/abs/1803.11485

它將每個智慧體選擇動作的區域性值(形狀為 (*B, self.n_agents, 1))轉換為一個全域性值(形狀為 (*B, 1))。與 torchrl.objectives.QMixerLoss 一起使用。有關示例,請參閱 examples/multiagent/qmix_vdn.py

引數:
  • state_shape (tupletorch.Size) – 狀態的形狀(不包括可能的領先批次維度)。

  • mixing_embed_dim (int) – 混合嵌入維度的尺寸。

  • n_agents (int) – 代理數量。

  • device (strtorch.Device) – 網路所用的 torch 裝置。

示例

>>> import torch
>>> from tensordict import TensorDict
>>> from tensordict.nn import TensorDictModule
>>> from torchrl.modules.models.multiagent import QMixer
>>> n_agents = 4
>>> qmix = TensorDictModule(
...     module=QMixer(
...         state_shape=(64, 64, 3),
...         mixing_embed_dim=32,
...         n_agents=n_agents,
...         device="cpu",
...     ),
...     in_keys=[("agents", "chosen_action_value"), "state"],
...     out_keys=["chosen_action_value"],
... )
>>> td = TensorDict({"agents": TensorDict({"chosen_action_value": torch.zeros(32, n_agents, 1)}, [32, n_agents]), "state": torch.zeros(32, 64, 64, 3)}, [32])
>>> td
TensorDict(
    fields={
        agents: TensorDict(
            fields={
                chosen_action_value: Tensor(shape=torch.Size([32, 4, 1]), device=cpu, dtype=torch.float32, is_shared=False)},
            batch_size=torch.Size([32, 4]),
            device=None,
            is_shared=False),
        state: Tensor(shape=torch.Size([32, 64, 64, 3]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([32]),
    device=None,
    is_shared=False)
>>> vdn(td)
TensorDict(
    fields={
        agents: TensorDict(
            fields={
                chosen_action_value: Tensor(shape=torch.Size([32, 4, 1]), device=cpu, dtype=torch.float32, is_shared=False)},
            batch_size=torch.Size([32, 4]),
            device=None,
            is_shared=False),
        chosen_action_value: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.float32, is_shared=False),
        state: Tensor(shape=torch.Size([32, 64, 64, 3]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([32]),
    device=None,
    is_shared=False)
mix(chosen_action_value: Tensor, state: Tensor)[原始碼]

混合器的前向傳播。

引數:

chosen_action_value – 形狀為 [*B, n_agents] 的張量

返回:

形狀為 [*B] 的張量

返回型別:

chosen_action_value

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源