Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Module

model.parameters

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.

How it works

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.

Syntax

Module.parameters(recurse=True)

Parameters

NameTypeDescription
recurseboolWhether to recursively include parameters from child modules.

Returns

An iterator over the module's parameters.

Key points

  • The method produces an iterator rather than a materialized list, so it can be consumed directly by APIs such as optimizers.
  • A standard use is passing the returned iterator to an optimizer, which uses the registered tensors during gradient-based training.
  • The parameter set covers both the module's own registered parameters and those belonging to nested submodules.
  • Use named_parameters() instead when parameter names are needed alongside the parameter objects.

Common mistakes

  • Do not write code that depends on a particular parameter ordering; use names or explicit parameter groups when order matters.

Example: Optimize a 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.

When to use it

Create optimizer input

Training a neural network with gradient descent.

Inspect model parameters

Collecting all learnable tensors in a nested model.

Control recursive traversal

Restricting iteration to a module's own parameters.

Interview questions

QWhat does Module.parameters() return, and why is it useful?

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.

QDoes parameters() include parameters from nested modules?

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.

QCan code rely on the order of parameters()?

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_parameters
  • torch.nn.Parameter
  • torch.optim.Optimizer
  • torch.nn.Module.state_dict

Source: Module — PyTorch 2.14 documentation. Checked 2026-10-16.