Skip to content

Adam and SGD Optimizers for Neural Network Training

An optimizer turns gradients into parameter updates: plain SGD scales each gradient by one learning rate, while Adam also divides by a running gradient size. Kingma and Ba, in the Adam paper, describe stochastic gradient descent as "an efficient and effective optimization method that was central in many machine learning success stories, such as recent advances in deep learning." Their method keeps two running averages of the gradient g_t: m_t, an average of the gradients, and v_t, an average of the squared gradients. After correcting both for their start at zero, Algorithm 1 updates the parameters with θ_t ← θ_(t−1) − α · m̂_t / (√v̂_t + ε). The "good default settings for the tested machine learning problems are α = 0.001, β1 = 0.9, β2 = 0.999 and ε = 10⁻⁸."

They state what this buys: "the magnitudes of parameter updates are invariant to rescaling of the gradient, its stepsizes are approximately bounded by the stepsize hyperparameter, it does not require a stationary objective, it works with sparse gradients, and it naturally performs a form of step size annealing." Read those claims for what they say. The update size is tied to the step size α, so the step size is still a choice you make.

Try it by hand. Minimize f1(θ) = θ², with gradient 2θ, and then f2(θ) = 100θ², with gradient 200θ, both starting at θ = 1. Plain SGD uses a learning rate of 0.01. On f1 the first step is 0.01 × 2 = 0.02, giving 0.98, then 0.9604, then 0.9412. On f2 the first step is 0.01 × 200 = 2, which lands at −1, and every step after that jumps back to 1 and then to −1 again, because 1 − 0.01 × 200 = −1. The same learning rate behaves very differently on two objectives that differ only by a constant factor. Now Adam with α = 0.1, β1 = 0.9, β2 = 0.999, and ε = 10⁻⁸. On the first step m̂ = g and √v̂ = |g|, so the step is α · g / (|g| + ε), which is about 0.1 as long as |g| is much larger than ε. For tiny gradients it is not: with ε = 10⁻⁸, a gradient of 10⁻¹⁰ gives a step of about 0.00099, and a gradient of exactly 0 gives no step. The trajectory on both f1 and f2 is 0.9, 0.8004, and 0.7016, identical to the digits shown. Adam is not free of choices, though. Run it on f1 with α = 1.5 and the first step overshoots to −0.5, the next to −0.8995, and the iterates keep swinging across zero for several more steps: −0.539, 0.0441, 0.5133, 0.6476. I computed all of these by running the update rules as written, and the objectives are invented.

The trap: concluding that an adaptive optimizer removes the learning rate or always beats SGD. Adam rescales steps by gradient size, and its step size α still sets how far each step goes, as the α = 1.5 run shows. A caution of my own, not from the paper: the invariance to rescaling the gradient is exact only when ε is zero, and with ε = 10⁻⁸ it is approximate, which is why the two trajectories above agree to the digits shown and not exactly. Also, this example is a toy in one dimension and says nothing about which optimizer trains a real network faster or generalizes better, and implementations can differ in where ε goes and in what state they keep, so the same name can mean slightly different updates in different libraries.

Prof Rod's rule: treat an optimizer as a way to turn gradients into steps, and still choose and check the step size on your own data.

Kingma and Ba, Adam: A Method for Stochastic Optimization, ICLR 2015, Section 1 and Algorithm 1, pp. 1–2