🏁 Optimizer Race: Gradient Descent, Momentum, Nesterov and Adam on Tricky Landscapes
The science
Gradient descent: x <- x - lr * grad f. On a quadratic f = (x^2 + k y^2)/2 the y-coordinate shrinks by a factor (1 - lr k) every step, so it converges iff lr < 2/k; the readout shows this theoretical ratio.
Heavy-ball momentum: v <- beta v + grad f, x <- x - lr v. Nesterov evaluates the gradient at the look-ahead point x - lr beta v. Adam keeps bias-corrected moving averages of the gradient and its square (beta1 = 0.9, beta2 = 0.999) and takes steps of about lr per coordinate.
Independent checks (run on load): every analytic gradient is compared with central finite differences, and 30 GD steps on the bowl are compared with the closed form y0 (1 - lr k)^n.
Simplifications: 2D toy surfaces and fixed learning rates; real machine-learning loss surfaces have millions of dimensions, noisy mini-batch gradients and learning-rate schedules. Rastrigin shows that all four methods are local: they stop in whichever minimum they fall into.
Controls
Raise the learning rate until plain GD oscillates, then diverges (on the bowl it diverges when lr * k > 2). Momentum and Adam cope better with the narrow Rosenbrock valley.