快捷方式

MultiAgentNetBase

class torchrl.modules.MultiAgentNetBase(*, n_agents: int, centralized: bool | None = None, share_params: bool | None = None, agent_dim: int | None = None, vmap_randomness: str = 'different', use_td_params: bool = True, **kwargs)[源代码]

多智能体网络的基类。

注意

要使用 torch.nn.init 模块初始化 MARL 模块参数,请参考 get_stateful_net()from_stateful_net() 方法。

forward(*inputs: tuple[torch.Tensor]) Tensor[源代码]

定义每次调用时执行的计算。

所有子类都应重写此方法。

注意

尽管前向传播的实现需要在该函数内定义,但之后应调用 Module 实例而不是此函数,因为前者负责运行注册的钩子,而后者会默默地忽略它们。

from_stateful_net(stateful_net: Module)[源代码]

使用网络的状态化版本填充参数。

有关如何收集网络的状态化版本,请参阅 get_stateful_net()

参数:

stateful_net (nn.Module) – 应从中收集参数的状态化网络。

get_stateful_net(copy: bool = True)[源代码]

返回网络的状态化版本。

这可用于初始化参数。

这些网络通常开箱即用,无法调用,需要调用 vmap 才能执行。

参数:

copy (bool, optional) – 如果为 True,则创建网络的深拷贝。默认为 True

如果参数被原地修改(推荐),则无需将参数复制回 MARL 模块。有关如何使用已原地重新初始化的参数重新填充 MARL 模型,请参阅 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)
>>> 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,
... )
>>> snet = mlp.get_stateful_net()
>>> def init(module):
...     if hasattr(module, "weight"):
...         torch.nn.init.kaiming_normal_(module.weight)
>>> snet.apply(init)
>>> # If the module has been updated out-of-place (not the case here) we can reset the params
>>> mlp.from_stateful_net(snet)
reset_parameters()[源代码]

重置模型的参数。

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源