Tap the plot to choose a new starting point. ★ marks the global minimum.
| Optimizer | Steps | Loss | Position | Status |
|---|
Mini demo: gradient descent fits a line
Linear regression learns a slope w and intercept b by minimising mean squared error (MSE). The dashed lines are the errors (residuals) it is trying to shrink.
How to read the plot
The coloured map is a loss surface. Each point is one choice of two parameters, and its colour is how bad that choice is. Dark blue is low loss and yellow is high loss. Each optimizer starts at the same point and follows its own rule downhill. Every dot is one step.
The four optimizers
- SGD: step = −lr × gradient. Simple, but slow in flat directions and zig-zags across narrow valleys.
- Momentum: keeps a running velocity (0.9 × old velocity + gradient), like a ball rolling downhill. It speeds up along consistent directions and can roll through small bumps and local minima.
- RMSProp: divides each step by a running average of recent gradient sizes, so steep and flat directions take similar-sized steps.
- Adam: momentum plus RMSProp, with a bias correction for the first steps. It is the default optimizer for most deep learning.
Experiments to try
- Elongated valley: SGD zig-zags across the narrow direction while crawling along the long one. RMSProp and Adam rescale the steps and head almost straight to the minimum.
- Learning rate too high: on the bowl, push the SGD rate above 1. Each step overshoots further and the status shows diverged. This is why training loss sometimes turns into NaN.
- Many local minima / Double well: plain SGD gets stuck in the nearest dip. Momentum can carry it over the hill into a deeper minimum.
- Rosenbrock banana: finding the curved valley is easy, but following it to the minimum at (1, 1) is slow. This function is a classic stress test for optimizers.
- Fit a line: with learning rate 0.02, the slope is learned quickly but the intercept takes many more steps, because x ranges from 0 to 10 and the two parameters have very different curvature. Rates of 0.03 or more diverge. This is why we scale features before training.
What is gradient descent?
Gradient descent is how almost every machine learning model is trained. The gradient points uphill on the loss surface, so each step moves the parameters a little downhill: θ ← θ − learning rate × gradient. Repeat until the loss stops falling.
Which optimizer should I use?
Adam is the default choice for most deep learning because it adapts its step size per parameter and needs little tuning. SGD with momentum is still popular for computer vision and often generalises slightly better when it is well tuned. RMSProp is common for recurrent networks and reinforcement learning.
Why does a big learning rate make the loss explode?
If a step overshoots the bottom of the valley by more than it started away from it, every step lands further out than the last and the loss grows without limit. On a curve with curvature c, plain gradient descent only converges when the learning rate is below 2 ÷ c.
Is this the real algorithm?
Yes. The optimizers use the standard update rules (momentum 0.9, RMSProp decay 0.9, Adam β₁ = 0.9 and β₂ = 0.999 with bias correction) and the exact gradients of each function.