Adaptive & Second-Order Methods
AdaGrad, RMSProp, Adam and Newton’s method.
Gradient descent takes the same step size in every direction, so one steep direction forces tiny steps everywhere else. This lesson fixes that in two ways. Adaptive methods (AdaGrad, RMSProp, Adam) give each parameter its own step size, learned from the gradients seen so far; second-order methods (Newton and quasi-Newton) use curvature to take the right step in every direction at once.
What you’ll learn
- Explain why ill-conditioning slows gradient descent, and how a preconditioner changes the geometry the method sees.
- Write the AdaGrad, RMSProp and Adam updates, derive Adam's bias correction, and contrast an L2 penalty with AdamW's decoupled weight decay.
- Derive Newton's method from a quadratic model, prove its local quadratic convergence for strongly convex functions with a Lipschitz Hessian, and count its cost.
- Describe how quasi-Newton methods such as BFGS and L-BFGS build curvature from gradients, and choose a method for a given problem.
One step size for every direction
Take a quadratic whose Hessian is diagonal, with curvatures . Gradient descent with learning rate multiplies coordinate by at every step. Stability in the steepest direction needs , and at the standard choice the flattest direction shrinks by only per step, so about steps are needed. The condition number , not the dimension, sets the cost.
The cure is a preconditioner, a positive definite matrix that reshapes the step:
In the coordinates this is plain gradient descent on , whose Hessian is . The ideal makes every direction equally curved, and one step with lands exactly on the minimum of a quadratic. Every method in this lesson is a way to approximate a good cheaply: from the history of gradients (adaptive methods) or from second derivatives (Newton and quasi-Newton methods).
AdaGrad and RMSProp: a step size per coordinate
Adaptive methods build a diagonal preconditioner out of the gradients themselves. With the (stochastic) gradient at the current step and every operation applied coordinate by coordinate:
AdaGrad (Duchi, Hazan & Singer, 2011)
s ← s + g ⊙ g # running SUM of squared gradients
x ← x − η · g / (sqrt(s) + ε)
RMSProp (Tieleman & Hinton, 2012)
s ← ρ·s + (1 − ρ)·(g ⊙ g) # running AVERAGE, typically ρ = 0.9
x ← x − η · g / (sqrt(s) + ε)is the elementwise product, and only prevents division by zero.
AdaGrad (Duchi, Hazan & Singer, 2011, with concurrent work by McMahan & Streeter, 2010) was designed for online learning with sparse features, as in text classification or click prediction. A rare feature has a small accumulated sum, so when it does appear it takes a large step; a frequent one takes small steps. For online convex problems this gives a regret bound that adapts to the geometry of the gradients (Adaptive Methods in Depth proves it). The price is that the sum only grows, so when gradients persist the effective step decays roughly like : right for convex problems, but often too fast for deep networks, which must travel far through nonconvex terrain. RMSProp, proposed by Tieleman and Hinton in a 2012 lecture of their online neural networks course and never formally published, replaces the sum with an exponential moving average, so old gradients are forgotten and the steps stop shrinking.
You multiply your training loss by and keep fixed. What happens to the size of RMSProp's steps (ignoring )?
- They become times larger
- They become about times larger
- They stay the same
- They become times smaller
Show the answer
C. They stay the same
Every gradient is multiplied by , and so is , the root of an average of their squares; the ratio is unchanged. Plain gradient descent would take steps times larger and almost certainly diverge.
Adam: momentum meets RMSProp
Adam (Kingma & Ba, 2015) combines a momentum-style average of gradients with RMSProp's average of squared gradients, and corrects both for starting at zero.
ADAM(η = 0.001, β1 = 0.9, β2 = 0.999, ε = 1e-8) # the paper's defaults
m ← 0; v ← 0
for t = 1, 2, ...:
g ← stochastic gradient at x
m ← β1·m + (1 − β1)·g # first moment: momentum
v ← β2·v + (1 − β2)·(g ⊙ g) # second moment, per coordinate
m_hat ← m / (1 − β1^t); v_hat ← v / (1 − β2^t) # bias correction
x ← x − η · m_hat / (sqrt(v_hat) + ε)Unroll the second-moment average from (one coordinate): it is a weighted sum of past squared gradients whose weights add up to less than one.
If the squared gradients have a stationary mean , the average is biased toward zero by exactly that factor, which bias correction divides out. The same holds for with :
Without the correction the early steps are distorted. At the ratio depends only on the two decay rates, and with close to the first steps are much larger than :
With and , how many times is Adam's first step without bias correction (ignoring )?
Show the answer
3.16
. With the correction, and , so the first step is exactly per coordinate, against the sign of . The two versions agree only once has faded, which takes a few thousand steps when .
Weight decay, decoupled. With plain SGD, adding an L2 penalty to the loss and shrinking the weights directly are the same thing: . With Adam they differ. The penalty's gradient is divided by along with everything else, so weights whose gradients have been large are regularized less. AdamW (Loshchilov & Hutter, 2019) applies the weight decay outside the adaptive scaling,
which shrinks every weight at the same relative rate. (In this form, PyTorch's, the decay is also scaled by the learning rate.) As of the mid-2020s AdamW is the most common choice for training transformers.
Newton's method: let curvature choose the step
Approximate near by its second-order Taylor expansion, built from the gradient and the Hessian :
If , the model has a unique minimizer, where its gradient vanishes. Newton's method jumps there:
On a quadratic with the model is exact, so one step lands on from anywhere, whatever the condition number. Newton's method is preconditioned gradient descent with the ideal , re-evaluated at every step.
Write and . Since , the gradient at is an integral of the Hessian along the segment from :
The two Hessians in the bracket are evaluated apart, so they differ by at most , and strong convexity gives :
With the recursion becomes , so the error is doubly exponentially small once :
Newton's method starts with in the recursion . Write . What is ?
Show the answer
8
, , : the exponent doubles at every step. A linearly convergent method that shrinks the error by a factor per step would need about steps to go from to .
The catch is cost and reach. Forming the Hessian of a generalized linear model, , costs and solving the Newton system , with memory: routine for in the thousands, impossible for a network with parameters, whose Hessian would have entries. And the guarantee is local. Far from the quadratic model can mislead: on , convex with minimizer , the pure Newton step is , which diverges from every . On nonconvex functions Newton's method is attracted to any nondegenerate stationary point, saddles and maxima included. Practical Newton methods therefore damp the step with a line search or a trust region. For logistic regression, Newton's method, safeguarded by a line search, is the classical fitting method: iteratively reweighted least squares (Logistic Regression in Depth).
Quasi-Newton methods, and which method when
Quasi-Newton methods get curvature from gradients alone. After a step with gradient change , the Hessian must satisfy , exactly for a quadratic. BFGS, named for Broyden, Fletcher, Goldfarb and Shanno, who proposed it independently in 1970, keeps an approximation of the inverse Hessian and corrects it by a rank-two update so that the new one satisfies this secant condition, :
Each step costs instead of , and near a nondegenerate minimizer BFGS converges superlinearly. L-BFGS (Liu & Nocedal, 1989) keeps only the last pairs , typically to , and applies to a vector in time without ever forming it. It is a strong default for smooth, deterministic problems with up to millions of variables; with noisy mini-batch gradients its curvature pairs are corrupted, one reason it rarely trains deep networks.
import numpy as np
from scipy.optimize import minimize
from scipy.special import expit # the sigmoid
def loss_and_grad(w, X, y, lam): # logistic regression, labels y in {0, 1}
z = X @ w
loss = np.mean(np.logaddexp(0, z) - y * z) + 0.5 * lam * w @ w
grad = X.T @ (expit(z) - y) / len(y) + lam * w
return loss, grad
# X: (n, d) design matrix, y: (n,) array of 0/1 labels
res = minimize(loss_and_grad, np.zeros(X.shape[1]), args=(X, y, 1e-3),
jac=True, method='L-BFGS-B')
w_hat = res.xjac=True tells SciPy that the function returns the loss and its gradient together, and np.logaddexp(0, z) computes without overflow. The small ridge term makes the objective strongly convex, so the minimizer exists and is unique even on separable data.
| Method | Extra memory | Typical use |
|---|---|---|
| SGD with momentum | large or noisy problems; still competitive for many vision models | |
| AdaGrad | convex problems with sparse features | |
| Adam, AdamW | deep networks, especially transformers | |
| Newton (damped) | small and high accuracy: generalized linear models, interior-point solvers | |
| L-BFGS | smooth deterministic problems with up to millions of variables | |
| Approximate curvature (K-FAC, Shampoo) | per-layer matrix factors | research and some large-scale training (Newton & Quasi-Newton Methods) |
Key takeaways
- Gradient descent's speed is set by the condition number , because one step size must suit both the steepest and the flattest directions. A preconditioner reshapes the geometry, and is ideal.
- AdaGrad divides each coordinate's step by the root of its summed squared gradients (strong on sparse convex problems, but steps decay like ); RMSProp uses a moving average instead. Both make steps scale-free, close to sign descent.
- Adam adds momentum and bias correction, stepping by with ; AdamW decouples weight decay from the adaptive scaling. Per-coordinate scaling fixes only axis-aligned ill-conditioning.
- Newton's method minimizes the local quadratic model: one step on a quadratic, and quadratic convergence near a nondegenerate minimizer, but work per step and only local guarantees. BFGS and L-BFGS recover much of its speed from gradients through the secant condition.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why was RMSProp proposed as a replacement for AdaGrad when training neural networks?
- AdaGrad cannot handle sparse gradients
- AdaGrad's sum of squared gradients only grows, so its steps keep shrinking; RMSProp's moving average forgets old gradients
- RMSProp uses second derivatives, which AdaGrad lacks
- AdaGrad's steps grow without bound late in training
Show the answer
B. AdaGrad's sum of squared gradients only grows, so its steps keep shrinking; RMSProp's moving average forgets old gradients
AdaGrad's effective step decays roughly like when gradients persist: fine for convex online problems, too fast for long nonconvex training runs. An exponential moving average keeps the denominator at the scale of recent gradients.
You train with Adam and add an L2 penalty to the loss. Which weights does the penalty pull toward zero most weakly at each step?
- Weights whose gradients stayed small throughout training
- All weights equally, as with SGD
- Weights whose gradients have been large, because the penalty's gradient is divided by
- The bias terms only
Show the answer
C. Weights whose gradients have been large, because the penalty's gradient is divided by
Roughly, coordinate decays at rate , which is small where gradients are large. AdamW applies outside the adaptive scaling, so every weight decays at the same relative rate.
Newton's method with step is applied to a strictly convex quadratic whose condition number is . How many steps does it need, in exact arithmetic?
- One, from any starting point
- About
- About
- It may diverge, because ill-conditioning breaks Newton's method
Show the answer
A. One, from any starting point
The quadratic model is exact, so the Newton step lands on . Rescaling coordinates changes but not Newton's iterates, a property called affine invariance. In floating point a huge does cost accuracy in the linear solve, but not iterations.
What problem does Adam's bias correction fix?
- Mini-batch gradients are biased estimates of the full gradient
- Adam's final solution is biased toward the origin
- Late in training Adam's steps become too small
- The moment estimates start at zero, so early in training they are biased toward zero and distort the first steps
Show the answer
D. The moment estimates start at zero, so early in training they are biased toward zero and distort the first steps
Both moving averages start at , so when the gradient statistics are stationary; dividing by (and by for ) removes the shrinkage. It matters only in the first few thousand steps when .
L-BFGS excels on full-batch logistic regression. Why is it rarely used to train large neural networks with mini-batches?
- It needs the full Hessian at every step
- Noisy mini-batch gradients corrupt its curvature pairs , and its line searches assume consistent function values
- It needs memory
- It only works on quadratic functions
Show the answer
B. Noisy mini-batch gradients corrupt its curvature pairs , and its line searches assume consistent function values
L-BFGS needs only gradients and memory, but its curvature estimate is swamped by noise when the two gradients come from different mini-batches. Stochastic quasi-Newton methods exist, but as of the mid-2020s none has displaced first-order methods in deep learning.
Go deeper
- Newton & Quasi-Newton Methods · Curvature, quadratic convergence, BFGS and L-BFGS.
- Adaptive Methods in Depth · AdaGrad’s regret bound, Adam’s failure cases and AdamW.
- Mirror Descent & Natural Gradient · Optimizing in the right geometry.
Further reading
- J. Duchi, E. Hazan & Y. Singer, "Adaptive subgradient methods for online learning and stochastic optimization," Journal of Machine Learning Research 12, 2121–2159 (2011).
- D. P. Kingma & J. Ba, "Adam: a method for stochastic optimization," ICLR (2015).
- I. Loshchilov & F. Hutter, "Decoupled weight decay regularization," ICLR (2019).
- J. Nocedal & S. J. Wright, Numerical Optimization, 2nd ed. (Springer, 2006), chapters 3, 6 and 7.