Pytorch Torch Nn Modulelist
# PyTorch torch.nn.ModuleList Function
[ PyTorch torch.nn Reference Manual](https://example.com/pytorch/pytorch-torch-nn-ref.html)
* * *
`torch.nn.ModuleList` is a container in PyTorch used to store a list of modules.
It is similar to a Python list, but automatically registers all submodules.
### 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("Parameter count:",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("Layer count:",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)
print("Sequential index access:", seq)
print("nModuleList iterable but no forw
YouTip