W3cubDocs

/PyTorch 2.9

ModuleList

class torch.nn.modules.container.ModuleList(modules=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.

Parameters

modules (iterable, optional) – an iterable of modules to add

Example:

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
append(module) [source]

Append a given module to the end of the list.

Parameters

module (nn.Module) – module to append

Return type

Self

extend(modules) [source]

Append modules from a Python iterable to the end of the list.

Parameters

modules (iterable) – iterable of modules to append

Return type

Self

insert(index, module) [source]

Insert a given module before a given index in the list.

Parameters
  • index (int) – index to insert.
  • module (nn.Module) – module to insert

© 2025, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://docs.pytorch.org/docs/2.9/generated/torch.nn.modules.container.ModuleList.html