Gradient LabStart the lesson

Lessons › Chapter 8: Neural networks

Optimizers: SGD, momentum and Adam

With momentum, each step keeps some of the previous one, like a heavy ball rolling downhill. The side-to-side bounces cancel out and the forward motion builds up.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Training takes days

Training is slow and the error zigzags. Leo switches to a smarter optimizer.

Problem

Plain gradient descent zigzags

In a long, narrow valley, plain stochastic gradient descent (SGD) bounces from wall to wall because the steep sides dominate the slope. It gets there, slowly.

Key idea

Momentum: a rolling ball

With momentum, each step keeps some of the previous one, like a heavy ball rolling downhill. The side-to-side bounces cancel out and the forward motion builds up.

Build

Adam: a step size per weight

adam adds one more idea: each weight gets its own step size, small where slopes are steep and bigger where they’re gentle. It heads almost straight for the bottom and is the default for most deep learning.

Real life

In real life: rolling through the bumps

A skateboarder with speed rolls straight over small bumps instead of stopping at each one. That’s momentum. It’s why training doesn’t get stuck on every little dip, and why optimizers like Adam train large models much faster.

Quick check: What does momentum add to gradient descent?

Momentum carries part of the last step forward, so oscillations cancel and progress builds up.

Pseudocode

# plain SGD: step = −lr × gradient
w = w − lr × gradient

# momentum: keep some of the last step, like a rolling ball
velocity = 0.9 × velocity − lr × gradient
w = w + velocity

# Adam: momentum + its own step size for every weight

Key terms

momentum
Carrying some of the previous step forward, so training rolls smoothly instead of zigzagging.
adam
A popular optimizer that combines momentum with a separate step size for every weight.

Build it: A neural network that reads handwriting (35 min, runs in your browser)

See the project