What Optimizers Do After Backpropagation

By the time backpropogation finishes, the model’s weights haven’t changed. The model has made predictions, the loss function has scored them against the training objective, and backpropagation has calculated how sensitive that loss is to each parameter. The optimizer uses those gradients to decide what to change and by how much.

That last step helps explain why two training runs can behave differently even when they start with the same model, use the same examples, and calculate loss the same way. One optimizer may work directly with the latest gradients. Another may also use what it has seen over hundreds of earlier steps.

What a Gradient Can Tell You

A gradient describes what happens near the model’s current parameter values. If the gradient for a weight is positive, nudging that weight upward would increase the loss, assuming the other parameters stay fixed and the change is small enough. For a step that aims to reduce the loss, that gives you a reason to move the weight downward. It leaves an important question open: how far should it move?

In a PyTorch training loop, that distinction shows up in two calls. loss.backward() calculates the gradients, and optimizer.step() uses them to update the parameters. If the loop stops after the backward pass, the usual weight update hasn’t happened yet.

How SGD and Momentum Use Gradients

Plain stochastic gradient descent, or SGD, subtracts the gradient multiplied by the learning rate from each parameter. In deep learning, that gradient typically comes from a mini-batch. If a weight starts at 2, its gradient is 0.5, and the learning rate is 0.1, SGD subtracts 0.05 and leaves the weight at 1.95. That’s the update with no momentum or weight decay.

The next batch may produce a different gradient simply because it contains different examples. With momentum, each step carries forward some influence from earlier gradients. If several batches point in roughly the same direction, those contributions can build. If they keep pointing in opposite directions, they can partly cancel out. PyTorch's SGD optimizer keeps that history in a momentum buffer when momentum is enabled. How much it helps still depends on the learning rate and the problem.

What Adam Keeps Track Of

Adam keeps two running averages for each parameter: one of its gradients and one of its squared gradients. It gives more weight to recent values and corrects for the fact that both averages start at zero. The first average helps determine the direction of the step. The second helps scale the step for each parameter, and a small constant keeps the denominator from getting too close to zero.

Adam still has a base learning rate. A poorly chosen value can make training unstable or slow, and keeping those running averages uses extra memory. When comparing Adam with SGD, look at how well each model performs on validation data, how long it takes to get there, and how much memory and tuning time it needs. The optimizer that brings training loss down fastest may not give you the best model for the task.

Where the Training Loop Can Go Wrong

PyTorch adds new gradients to any already stored on a parameter. For independent updates, you normally clear the old gradients before calculating new ones. You can also use that behavior deliberately: accumulate gradients across several mini-batches, then take one optimizer step. The same attention to timing applies to gradient clipping, which changes gradients before the update, and to a learning rate schedule, which changes the rate according to the training plan.

Optimizer history also matters when you resume a run. A checkpoint that saves only the model’s weights can restore its predictions at that point, but it leaves out the momentum buffer or Adam’s running averages. If you want the next update to pick up where the last one left off, save the optimizer state along with the model and record the settings that governed the run.

Back to Main   |  Share