ModuleList#
- class torch.nn.ModuleList(modules=None)[source]#
以列表形式儲存子模組。
ModuleList可以像普通 Python 列表一樣進行索引,但它包含的模組會被正確註冊,並且對所有Module方法可見。- 引數
modules (iterable, optional) – 要新增的模組的可迭代物件
示例
class MyModule(nn.Module): def __init__(self) -> None: super().__init__() self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)]) def forward(self, x): # ModuleList can act as an iterable, or be indexed using ints for i, l in enumerate(self.linears): x = self.linears[i // 2](x) + l(x) return x