QMixer¶
- class torchrl.modules.QMixer(state_shape: tuple[int, ...] | torch.Size, mixing_embed_dim: int, n_agents: int, device: DEVICE_TYPING)[源代码]¶
QMix mixer。
通过一个单调的超网络将代理的局部 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 (tuple 或 torch.Size) – 状态的形状(不包括可能的前导批次维度)。
mixing_embed_dim (int) – 混合嵌入维度的尺寸。
n_agents (int) – 代理数量。
device (str 或 torch.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)