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

PyTorch · Module

optimizer.step

optimizer.step() applies one optimizer update after gradients are computed. Most optimizers use it without arguments; closure-based optimizers can reevaluate the model and loss.

How it works

After gradients have been computed by backpropagation, calling step applies the optimizer's update rule to its managed parameters. Most optimizers use the no-argument form; algorithms that must reevaluate the objective can receive a closure that clears gradients, recomputes the loss, performs backpropagation, and returns that loss.

Syntax

optimizer.step()
optimizer.step(closure)

Parameters

NameTypeDescription
closureCallable[[], float]A callable that reevaluates the model, computes the loss, and returns it; optional for most optimizers.

Returns

None for the usual form; a loss value for the closure form, as specified by the optimizer API.

Key points

  • Calling step() without arguments is the normal form for most optimization algorithms.
  • A closure used by reevaluating optimizers should reset gradients, calculate the loss, and return that loss.
  • Each call represents one optimizer update, although a closure-based algorithm may evaluate the objective repeatedly inside it.

Common mistakes

  • Call step only after the current gradients have been computed, typically with backward().
  • Algorithms that reevaluate the objective, such as LBFGS, require a closure rather than only the no-argument form.

Example: Basic optimization step

import torch

model = torch.nn.Linear(4, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
inputs = torch.randn(8, 4)
targets = torch.randn(8, 1)

optimizer.zero_grad()
loss = torch.nn.functional.mse_loss(model(inputs), targets)
loss.backward()
optimizer.step()

Clears accumulated gradients, computes a loss, backpropagates its gradients, and updates the model parameters.

When to use it

Update parameters from gradients

After a standard backward pass in a minibatch training loop.

Closure-driven optimization

When using an optimizer that needs multiple objective evaluations, such as LBFGS.

Apply a training-step update

At each iteration after accumulating the gradients intended for the update.

Interview questions

QWhat does optimizer.step() do, and where does it go in a training loop?

step performs one optimization update on the parameters managed by the optimizer. In the usual training loop, gradients are computed first with backward(), then step applies the optimizer's rule.

Follow-up: Why is zero_grad typically called before backward()? Gradients accumulate by default, so clearing them prevents contributions from previous iterations from being reused unintentionally.

QWhen is optimizer.step() called relative to backward()?

The no-argument form is the simplified interface supported by most optimizers. It is called after gradients have been computed and updates the parameters using those gradients.

Follow-up: What if an optimizer must reevaluate the model? Pass a closure that clears gradients, recomputes the output and loss, backpropagates, and returns the loss.

QWhy does optimizer.step sometimes take a closure?

A closure is a callable that reevaluates the model and returns the loss. Optimizers such as LBFGS can invoke it multiple times during one optimization step.

Follow-up: Why must the closure clear gradients? Repeated reevaluation would otherwise accumulate gradients across closure calls instead of computing each trial update from a fresh gradient.

  • torch.optim.Optimizer
  • Optimizer.zero_grad
  • Optimizer.add_param_group
  • torch.optim.LBFGS
  • torch.optim.SGD

Source: How To Adjust Learning Rate. Checked 2026-10-16.