r/learnmachinelearning • u/Impossible-Host5340 • 1d ago
Visual intuition for SGD vs. Momentum vs. Adam: 30 noisy runs escaping the same saddle point
When you first learn optimizers, the equations for SGD, Momentum, and Adam look abstract on paper. I built this side-by-side visualization to actually *watch* how they behave on the same saddle point when every run gets identical mini-batch noise.
**Setup**
- **Surface:** `L(x, y) = (x² − 1)² + 3y²` - a saddle point at `(0, 0)` (`L = 1.0`) flanked by two symmetric minima at `(−1, 0)` and `(+1, 0)` (`L = 0`).
- **Start:** all 30 runs begin at `(0, 1)` (`L = 4.0`), right on the unstable ridge, so symmetry breaking comes purely from gradient noise.
- **Identical conditions:** shared learning rate `η = 0.02`, identical per-step Gaussian gradient noise `σ = 0.10` across all three optimizers, 90 steps.
- **Standard update rules:** Momentum uses `γ = 0.90`; Adam uses `β₁ = 0.9, β₂ = 0.999, ε = 1e-8`.
**What jumps out when you run 30 seeds at once**
- **SGD stalls on the saddle plateau.** The steep `y`-slope pulls every run straight down to `(0, 0)`, where `∂L/∂x ≈ 0`. You can see the amber median line flatline at `L = 1.0` for ~50 steps while it waits for random noise to push it off the ridge. By step 90, only **22/30** reach `L < 0.05` (median **82** steps).
- **Momentum is fastest - and wildest.** Carrying vertical velocity (`γ = 0.90`) launches it straight through the saddle up the far wall (`y ≈ −0.65`) and whips it into both valleys first (**30/30** solved, median **31** steps), trading speed for heavy oscillation around the minimum.
- **Adam moves in eerie lockstep.** Dividing each axis by its running RMS gradient turns both the steep `y`-wall and the flat `x`-ridge into roughly equal `±η` steps. That's why all 30 emerald runs slice diagonally into both valleys almost together (**30/30** solved, median **60** steps) with a razor-thin interquartile band.
*(Side note: I built this visualizer in Dart/Flutter as an interactive mini-lab I'm adding to Epoch Idle, an ML-themed simulation game for iOS and Android - https://epoch.loyallogic.org. Happy to answer questions about the math or implementation!)*