Skip to content
mlmentorship

SGD with momentum

Add a moving average of past gradients to the update. Smoother trajectories, faster convergence in narrow valleys, and the foundation of Adam's first moment.

Published · 5 min read ·Core ·Intermediate

Visual quick review

Visual first · depth when needed

Compare raw SGD with momentum on an alternating gradient sequence and explain how momentum amplifies the consistent component while reducing the relative cross-valley oscillation.

Preparing the visual…

Summary

SGD with momentum maintains a velocity (an exponential moving average of past gradients) and updates parameters with instead of with the raw gradient. Typical .

Vanilla SGD bounces around in narrow loss valleys: gradients perpendicular to the valley axis cancel slowly, gradients along the axis are small. Momentum accumulates the consistent along-axis component while perpendicular components average to zero.

Learning objective

What does momentum do to consistent and alternating gradient components?

Raw SGD follows alternating descent directions On a common coordinate scale, four raw SGD updates alternate between the vectors one comma two and one comma negative two. The path zigzags from zero comma zero through one comma two, two comma zero, three comma two, and four comma zero. RAW SGD · SAME SCALE +2 0 −1 0 2 4 6 8 Δθ = (1, +2), (1, −2), … ends at x = 4
Momentum amplifies consistent progress relative to oscillation For beta one half after the momentum buffer reaches its steady alternating regime, the update vectors alternate between two comma four thirds and two comma negative four thirds. On the same scale as raw SGD, four updates trace zero comma zero, two comma four thirds, four comma zero, six comma four thirds, and eight comma zero. MOMENTUM · β=0.5 · WARMED UP +2 0 −1 0 2 4 6 8 Δθ = (2, +4/3), (2, −4/3), … ends at x = 8
Read it this way: use the same axes in both panels. For alternating descent directions dt = −gt = (1, ±2), raw SGD moves forward by 1 while crossing the valley by 2. With ut = 0.5ut−1 + dt after warm-up, the alternating steady-state updates are (2, ±4/3): the consistent component doubles, while the cross-valley-to-forward ratio falls from 2 to 2/3. Momentum makes faster, less zigzagging progress, but the larger forward step is also why its learning rate must be tuned. This original trace was checked against Polyak (1964), Sutskever et al. (2013), and the PyTorch SGD definition.

Empirically, momentum is essential for SGD to be competitive with adaptive optimizers on most problems. SGD without momentum is rarely used in modern training. Adam’s first-moment estimate is essentially momentum, which is why Adam inherits this benefit.

Two formulations

Classical momentum (Polyak, 1964)

Effective LR for a constant gradient: . With , that’s , so reducing effectively reduces the step size.

Nesterov momentum (Nesterov, 1983)

Compute the gradient at the look-ahead position instead of at . Updates:

In practice, only marginally better than classical momentum on most workloads. Used in some vision training (e.g., ResNet original paper).

Picking

Workload
ResNet / CNN training0.9
Reinforcement learning policy nets0.9
Very noisy gradients (RL, contrastive)0.95 or 0.99
Small batch with high noiselower (0.5–0.8) to track recent gradients

controls the effective averaging window: steps. averages over ~10 steps; averages over ~100.

When SGD+momentum vs. Adam

SituationDefault
Vision (CNN, ViT) classification with strong regularizationSGD + momentum + cosine LR
Transformers (NLP, LLM training)Adam / AdamW
Small datasets, fine-tuningAdam (less hyperparameter tuning needed)
Sparse gradients (recsys embeddings)Adam (per-parameter adaptive LR)
Reinforcement learningAdam (default in PPO / DQN implementations)

SGD+momentum often generalizes slightly better than Adam at convergence (sharp vs. flat minima discussion); Adam converges faster initially. For LLMs, Adam wins because the gradient distribution across parameters is highly non-uniform.

Common pitfalls

  • Forgetting that momentum scales the effective LR. Switching from to effectively multiplies the LR by 10.
  • Initializing without bias correction. Early steps have biased velocity; for SGD this is usually fine, but Adam explicitly bias-corrects.
  • Mixing momentum across LR changes. When the LR jumps, momentum carries old-LR-scaled velocities. Some implementations zero momentum at LR transitions.
  • Using SGD without momentum. Almost always strictly worse; pick momentum or use Adam.