PyTorch torch.nn.ModuleList Function

PyTorch torch.nn 参考手册PyTorch torch.nn Reference Manual


torch.nn.ModuleListIt is a container in PyTorch used to store a list of modules.

It is similar to a Python list, but automatically registers all child modules.

Function Definition

torch.nn.ModuleList(modules=None)

Usage Examples

Example 1: Basic Usage

Example

import torch
import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.layers = nn.ModuleList([
            nn.Linear(10, 20),
            nn.Linear(20, 20),
            nn.Linear(20, 5)
        ])

    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x

model = Net()
x = torch.randn(4, 10)
output = model(x)

print("Input:", x.shape)
print("Output:", output.shape)
print("Number of parameters:", sum(p.numel() for p in model.parameters()))

Example 2: Dynamically Building Layers

Example

import torch
import torch.nn as nn

class DynamicNet(nn.Module):
    def __init__(self, num_layers, dim):
        super(DynamicNet, self).__init__()
        self.layers = nn.ModuleList()
        for i in range(num_layers):
            in_dim = dim if i == 0 else dim
            self.layers.append(nn.Linear(in_dim, dim))

    def forward(self, x):
        for i, layer in enumerate(self.layers):
            x = layer(x)
            if i < len(self.layers) - 1:
                x = torch.relu(x)
        return x

model = DynamicNet(num_layers=5, dim=64)
print("Number of layers:", len(model.layers))
print("Total parameters:", sum(p.numel() for p in model.parameters()))

Example 3: Comparison with Sequential

Example

import torch
import torch.nn as nn

# ModuleList - Flexible
mlist = nn.ModuleList([nn.Linear(10, 20), nn.ReLU(), nn.Linear(20, 5)])

# Sequential - Fixed order
seq = nn.Sequential(nn.Linear(10, 20), nn.ReLU(), nn.Linear(20, 5))

print("ModuleList index access:", mlist[0])
print("Sequential index access:", seq[0])
print("nModuleList is iterable but has no forward")
print("Sequential can be called directly")

Use Cases

  • Multi-layer networks: dynamic construction
  • Recurrent structures: shared layers
  • Complex models: require custom control flow

Note: You must use nn.ModuleList instead of a Python list.


PyTorch torch.nn 参考手册PyTorch torch.nn Reference Manual

Other Extensions