Create optimizer input
Training a neural network with gradient descent.
PyTorch · Module
Module.parameters() returns an iterator over a module's registered learnable parameters, including parameters in child modules by default. It is commonly passed to an optimizer for training.
Calling parameters() returns an iterator over the module's registered learnable parameters, including those belonging to nested child modules. The iterator is commonly passed to an optimizer so gradient-based updates can modify the model's weights and biases.
Module.parameters(recurse=True)| Name | Type | Description |
|---|---|---|
recurse | bool | Whether to recursively include parameters from child modules. |
An iterator over the module's parameters.
import torch
model = torch.nn.Sequential(
torch.nn.Linear(4, 3),
torch.nn.ReLU(),
torch.nn.Linear(3, 1),
)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
x = torch.randn(2, 4)
y = model(x)
loss = y.square().mean()
loss.backward()
optimizer.step()The iterator returned by parameters() supplies the model's registered learnable tensors to the optimizer.
Training a neural network with gradient descent.
Collecting all learnable tensors in a nested model.
Restricting iteration to a module's own parameters.
parameters() returns an iterator over the module's registered parameters and recursively includes parameters from child modules. It is commonly supplied directly to an optimizer for training.
Follow-up: How can parameter names be included? Use named_parameters(), which yields name-parameter pairs instead of tensors alone.
A module's direct parameters and the parameters of its submodules are included when recursion is enabled. This lets one optimizer receive the complete parameter set for a nested network.
Follow-up: How would you exclude descendants? Use the recurse=False argument to restrict iteration to the module itself.
Repeated calls on an unchanged module return parameters in the same order, but the API does not promise a particular order in general. Therefore, parameter order should not be treated as a stable semantic identifier.
Follow-up: What should be used when distinct optimization behavior is needed? Build explicit optimizer parameter groups, selecting parameters intentionally rather than relying on iteration order.
torch.nn.Module.named_parameterstorch.nn.Parametertorch.optim.Optimizertorch.nn.Module.state_dict