The
torch.nn.Module.named_parameters() method returns an iterator that yields tuples containing both the name and the parameter tensor for each parameter in the module.- Iterator-based: Returns an iterator rather than a list, making it memory-efficient for large models
- Hierarchical naming: Parameter names reflect the module hierarchy using dot notation (e.g.,
layer1.weight)
- Recursive by default: Includes parameters from all submodules unless specified otherwise
import torch import torch.nn as nn class SimpleModel(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(10, 5) self.fc2 = nn.Linear(5, 2) def forward(self, x): x = torch.relu(self.fc1(x)) return self.fc2(x) model = SimpleModel() for name, param in model.named_parameters(): print(f"{name}: {param.shape}") # fc1.weight: torch.Size([5, 10]) # fc1.bias: torch.Size([5]) # fc2.weight: torch.Size([2, 5]) # fc2.bias: torch.Size([2])
Selective Parameter Freezing
for name, param in model.named_parameters(): if 'fc2' not in name: param.requires_grad = False print(f"{name}: requires_grad={param.requires_grad}")
Module — PyTorch 2.2 documentation
Modules can also contain other Modules, allowing to nest them in
a tree structure. You can assign the submodules as regular attributes:
https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.named_parameters

Seonglae Cho