Table of Contents
Shortcuts

# ModuleList

class torch.nn.ModuleList(modules=None)

  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):
              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)

    Appends a given module to the end of the list.

    Parameters:

      module (nn.Module) – module to append

    Return type:

      ModuleList

  extend(modules)

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

    Parameters:

      modules (iterable) – iterable of modules to append

    Return type:

      ModuleList

  insert(index, module)

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

    Parameters:

      - index (int) – index to insert.

      - module (nn.Module) – module to insert

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.