class torch.nn.ModuleList(modules: Optional[Iterable[torch.nn.modules.module.Module]] = None) [source]
Holds submodules in a list.
ModuleList can be indexed like a regular Python list, but modules it contains are properly registered, and will be visible by all Module methods.
modules (iterable, optional) – an iterable of modules to add
Example:
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__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
append(module: torch.nn.modules.module.Module) → T [source]
Appends a given module to the end of the list.
module (nn.Module) – module to append
extend(modules: Iterable[torch.nn.modules.module.Module]) → T [source]
Appends modules from a Python iterable to the end of the list.
modules (iterable) – iterable of modules to append
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.7.0/generated/torch.nn.ModuleList.html