ModuleList¶
- class torch.nn.ModuleList(modules=None)[原始碼][原始碼]¶
在列表中保存子模組。
ModuleList
可以像一般的 Python 列表一樣進行索引,但它包含的模組會被正確註冊,並且可以被所有Module
方法看到。- 參數
modules (iterable, optional) – 要新增的模組的可迭代物件(iterable)。
範例
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