nn.ModuleList is a PyTorch container that holds a list of modules (layers) and properly registers them for parameter tracking and gradient computation. Unlike Python lists, nn.ModuleList ensures PyTorch tracks all parameters for optimization. This enables building networks with arbitrary depths dynamically, where the number of layers is determined at runtime. Using nn.ModuleList in a custom nn.Module allows you to implement forward passes that iterate through layers conditionally, apply different logic per layer, or construct residual connections. This is essential for deep architectures where sequential definition is impractical.
Read the full lesson
Sign up free: one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in