MultiAgentMLP¶
- class torchrl.modules.MultiAgentMLP(n_agent_inputs: int | None, n_agent_outputs: int, n_agents: int, *, centralized: ~typing.Optional[bool] = None, share_params: ~typing.Optional[bool] = None, device: ~typing.Optional[~typing.Union[~torch.device, str, int]] = None, depth: ~typing.Optional[int] = None, num_cells: ~typing.Optional[~typing.Union[~typing.Sequence, int]] = None, activation_class: ~typing.Optional[~typing.Type[~torch.nn.modules.module.Module]] = <class 'torch.nn.modules.activation.Tanh'>, use_td_params: bool = True, **kwargs)[source]¶
多智能体 MLP。
这是一个可在多智能体上下文中使用 MLP。例如,作为策略或值函数。有关示例,请参见 examples/multiagent。
它期望的输入形状为 (*B, n_agents, n_agent_inputs),返回的输出形状为 (*B, n_agents, n_agent_outputs)
如果 share_params 为 True,则将使用相同的 MLP 为所有智能体进行前向传递(同构策略)。否则,每个智能体将使用不同的 MLP 来处理其输入(异构策略)。
如果 centralized 为 True,则每个智能体将使用所有智能体的输入来计算其输出(n_agent_inputs * n_agents 将作为一个智能体的输入数量)。否则,每个智能体将仅使用其自身的数据作为输入。
- 参数:
n_agent_inputs (int 或 None) – 每个智能体的输入数量。如果为
None
,则将在首次调用期间延迟实例化输入数量。n_agent_outputs (int) – 每个智能体的输出数量。
n_agents (int) – 智能体数量。
- 关键词参数:
centralized (bool) – 如果 centralized 为 True,则每个智能体将使用所有智能体的输入来计算其输出(n_agent_inputs * n_agents 将作为一个智能体的输入数量)。否则,每个智能体将仅使用其自身的数据作为输入。
share_params (bool) – 如果 share_params 为 True,则将使用相同的 MLP 为所有智能体进行前向传递(同构策略)。否则,每个智能体将使用不同的 MLP 来处理其输入(异构策略)。
device (str 或 toech.device, 可选) – 在其上创建模块的设备。
depth (int, 可选) – 网络的深度。深度为 0 将生成一个具有所需输入和输出大小的单线性层网络。长度为 1 将创建 2 个线性层等。如果未指示深度,则深度信息应包含在 num_cells 参数中(请参见下文)。如果 num_cells 是可迭代对象且指示了深度,则两者应匹配:len(num_cells) 必须等于深度。默认值:3。
num_cells (int 或 Sequence[int], 可选) – 输入和输出之间每层单元的数量。如果提供整数,则每层将具有相同数量的单元。如果提供可迭代对象,则线性层的 out_features 将与 num_cells 的内容匹配。默认值:32。
activation_class (Type[nn.Module]) – 要使用的激活类。默认值:nn.Tanh。
use_td_params (bool, 可选) – 如果
True
,则可以在 self.params 中找到参数,这是一个TensorDictParams
对象(它同时继承自 TensorDict 和 nn.Module)。如果False
,则参数包含在 self._empty_net 中。总而言之,这两种方法应该大致相同,但不可互换:例如,使用use_td_params=True
创建的state_dict
在use_td_params=False
时不能使用。**kwargs – 用于
torchrl.modules.models.MLP
可以传递以自定义 MLP。
注意
要使用 torch.nn.init 模块初始化 MARL 模块参数,请参考
get_stateful_net()
和from_stateful_net()
方法。示例
>>> from torchrl.modules import MultiAgentMLP >>> import torch >>> n_agents = 6 >>> n_agent_inputs=3 >>> n_agent_outputs=2 >>> batch = 64 >>> obs = torch.zeros(batch, n_agents, n_agent_inputs) >>> # instantiate a local network shared by all agents (e.g. a parameter-shared policy) >>> mlp = MultiAgentMLP( ... n_agent_inputs=n_agent_inputs, ... n_agent_outputs=n_agent_outputs, ... n_agents=n_agents, ... centralized=False, ... share_params=True, ... depth=2, ... ) >>> print(mlp) MultiAgentMLP( (agent_networks): ModuleList( (0): MLP( (0): Linear(in_features=3, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=2, bias=True) ) ) ) >>> assert mlp(obs).shape == (batch, n_agents, n_agent_outputs) Now let's instantiate a centralized network shared by all agents (e.g. a centalised value function) >>> mlp = MultiAgentMLP( ... n_agent_inputs=n_agent_inputs, ... n_agent_outputs=n_agent_outputs, ... n_agents=n_agents, ... centralized=True, ... share_params=True, ... depth=2, ... ) >>> print(mlp) MultiAgentMLP( (agent_networks): ModuleList( (0): MLP( (0): Linear(in_features=18, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=2, bias=True) ) ) ) We can see that the input to the first layer is n_agents * n_agent_inputs, this is because in the case the net acts as a centralized mlp (like a single huge agent) >>> assert mlp(obs).shape == (batch, n_agents, n_agent_outputs) Outputs will be identical for all agents. Now we can do both examples just shown but with an independent set of parameters for each agent Let's show the centralized=False case. >>> mlp = MultiAgentMLP( ... n_agent_inputs=n_agent_inputs, ... n_agent_outputs=n_agent_outputs, ... n_agents=n_agents, ... centralized=False, ... share_params=False, ... depth=2, ... ) >>> print(mlp) MultiAgentMLP( (agent_networks): ModuleList( (0-5): 6 x MLP( (0): Linear(in_features=3, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=2, bias=True) ) ) ) We can see that this is the same as in the first example, but now we have 6 MLPs, one per agent! >>> assert mlp(obs).shape == (batch, n_agents, n_agent_outputs)