快捷方式

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 開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源