Tag: optimization. A follow-up to the previous post on Newton’s method and self-consistent iteration.

中文版

After the previous post, a natural question is: doesn’t Adam also rescale the step per direction? Isn’t that the same idea as Newton’s method?

Half right. They want the same thing, but they are built from different parts. This post first builds the intuition with one picture, then gives the math. Every number comes from one small two-variable example and was measured, not estimated.

A long, narrow valley

Imagine walking downhill blindfolded, trying to reach the bottom of a valley. All you can feel is which way the ground slopes under your feet, and how steeply.

The simplest strategy is gradient descent: step in the downhill direction, with a fixed step length. The trouble is that valleys are often long and narrow: steep across, nearly flat along. Take big steps and you bounce between the two steep walls; take small steps and the walk along the flat floor takes forever.

The left panel below is such a valley. The curvature along the floor is 0.05 and across it 1.5, a factor of 30. The blue line is gradient descent with the best possible step size. It still bounces for 208 steps before the error falls to a millionth of where it started.

Same valley, two orientations contours of L = ½ xᵀHx with curvatures 0.05 and 1.5 (κ = 30); same start; first steps of each method (a) aligned with the axes × gradient descent: 208 steps Adam: 213 steps Newton: 1 step diagonal scaling by curvature: 1 step (on top of Newton) (b) tilted by 45° × gradient descent: 208 steps Adam: 219 steps Newton: 1 step diagonal scaling by curvature: 208 steps (on top of gradient descent)

Idea one: a separate step size for each direction

The obvious fix: the cross direction is steep, so take small steps there; the along direction is flat, so take large steps there. Concretely, keep one account for east-west and one for north-south, and scale each step by how strongly the ground curves in that direction.

In panel (a) the valley happens to lie along the axes, and the dashed green line does exactly this. East-west is exactly the flat direction and north-south exactly the steep one, so separate scaling is exactly right: one step to the bottom, on top of Newton’s line.

Now tilt the same valley by 45°, panel (b). Its shape has not changed at all; it is only rotated. Walking due east, you now meet both the steep and the flat direction; walking due north, the same. The two accounts come out identical, and scaling both by the same amount is no scaling at all. The dashed green line lies on top of gradient descent’s blue line: 208 steps again.

A separate step size per axis helps only when the valley lies along the axes.

Idea two: learn the shape of the valley

Newton’s method keeps no per-axis accounts. It learns the shape of the whole valley: which way the long axis points, which way the short axis points, and how strongly each one curves. Once you know the shape, you can compute where the bottom is and step straight there.

Rotating the valley changes nothing, because Newton looks at the valley itself, not at the axes. In both panels the black line reaches the bottom in one step.

Where is Adam?

Adam is often described as idea one, implemented for deep learning: each parameter gets its own step size. But it keeps a different account. Idea one records how the slope curves (curvature); Adam records how steep the slope is (the size of the gradient).

Those are quite different. A spot can be steep because the wall there really is steep, or simply because you are still far from the bottom. Adam cannot tell the two apart. And as you approach the bottom the slope flattens, so its account keeps changing.

Measured: the orange Adam line, with the best of 13 learning rates, takes 213 steps in the aligned valley and 219 in the tilted one, both about the same as gradient descent’s 208. In the aligned valley it does not get idea one’s single step.

This does not mean Adam is bad. Two variables and no noise is not the setting Adam was designed for; it was built for training with billions of parameters and noisy gradients. The example shows one thing only: Adam’s “separate step size per parameter” is not scaling by curvature, let alone Newton.

So why doesn’t deep learning use Newton?

Because learning the shape of the valley is too expensive. The shape is a matrix (the Hessian): n parameters give n × n numbers. A large model has billions of parameters, so the matrix cannot even be stored, let alone inverted. And each step sees only a small batch of data, so the feel of the ground underfoot is itself noisy, and so would be any shape computed from it.

Self-consistency problems in physics are the opposite: a few to a few hundred variables, so the shape can be computed; no noise; and the answer must be very precise (residuals of 10⁻¹⁰). Paying to learn the shape is worth it there, which is why Newton is used.

There are compromises in between. In deep learning, K-FAC and Shampoo approximate the matrix layer by layer with Kronecker products, which keeps some of the coupling between directions that a diagonal throws away. In physics, Broyden and Anderson mixing build the matrix up piece by piece from past steps.

The math

One framework

All the methods below can be written as

x ← x − P ∇L(x)

and differ only in the preconditioner P:

method P based on
gradient descent η I nothing: one step size for all directions
diagonal scaling by curvature η diag(H)⁻¹ the curvature along each coordinate
Adam η diag(1/(√v̂ + ε)), v̂ a running average of squared gradients the size of the gradient along each coordinate
Newton H⁻¹ the full curvature matrix

The example

The loss is a quadratic L(x) = ½ xᵀ H x with its minimum at x = 0, and

H = Q(θ) diag(0.05, 1.5) Q(θ)ᵀ,     condition number κ = 1.5 / 0.05 = 30

where Q(θ) is a rotation by θ. Panel (a) is θ = 0, panel (b) θ = 45°. Both start at x₀ = (1, 0.5), and “converged” means |x| has fallen to 10⁻⁶ |x₀|.

Gradient descent. Along eigendirection i the error is multiplied by 1 − η λᵢ each step. The best choice makes the two factors equal in size: η = 2/(λ_min + λ_max) = 2/1.55 ≈ 1.29, with factor (κ − 1)/(κ + 1) = 29/31 ≈ 0.935, so about ln 10⁻⁶ / ln 0.935 ≈ 208 steps. This depends only on the eigenvalues of H, not on θ, so both orientations take 208 steps.

Diagonal scaling. At θ = 0, diag(H) = H, so P = H⁻¹: this is Newton, one step. At θ = 45°,

H = [[ 0.775, −0.725],
     [−0.725,  0.775]]

Both diagonal entries are 0.775, so diag(H)⁻¹ is the identity times a constant: gradient descent with a different learning rate, still 208 steps. The off-diagonal −0.725 is what carries the tilt, and diagonal scaling throws it away.

Adam. Each step

m ← β₁ m + (1 − β₁) g          (running average of the gradient)
v ← β₂ v + (1 − β₂) g²         (running average of the squared gradient, per coordinate)
x ← x − η m̂ / (√v̂ + ε)          (m̂, v̂: bias-corrected)

The denominator √v̂ measures the size of the gradient. In a quadratic, the gradient along coordinate i is roughly the curvature times the distance from the minimum along that direction, so Adam’s scaling mixes curvature with distance, and the distance keeps changing. (This is an explanation of the result, not something measured separately.) β₁ = 0.9, β₂ = 0.999, ε = 10⁻⁸; only the learning rate was tuned, over 13 log-spaced values from 10⁻³ to 1. The best was 0.1 in the aligned valley (213 steps) and 0.316 in the tilted one (219 steps).

Gradient descent with momentum (heavy ball). Adam’s m is a form of momentum, so here is momentum on its own:

x_{k+1} = x_k − α ∇L(x_k) + β (x_k − x_{k−1})

With Polyak’s optimal parameters α = 4/(√λ_max + √λ_min)² and β = ((√κ − 1)/(√κ + 1))², the asymptotic factor improves from (κ − 1)/(κ + 1) to (√κ − 1)/(√κ + 1) ≈ 0.691. Measured: 48 steps aligned, 47 tilted (not drawn in the figure). Momentum in effect takes the square root of the condition number, 208 steps down to about 48. But like gradient descent it uses one step size for all directions, so the orientation of the valley makes no difference.

Newton. P = H⁻¹ reaches the minimum of a quadratic in one step, whatever θ is.

Measured step counts

method aligned (θ = 0) tilted (θ = 45°)
gradient descent, η = 1.29 208 208
diagonal scaling by curvature 1 208
heavy ball (Polyak parameters) 48 47
Adam (best learning rate) 213 219
Newton 1 1

Gradient descent, heavy ball and Newton do not depend on the axes; their step counts depend only on the shape of the valley, so rotating it changes nothing. Diagonal scaling and Adam do depend on the axes, so rotation changes their counts. The difference: diagonal scaling goes from 1 step to 208, while Adam never had the advantage in the first place and takes about 210 steps either way.

Limits

  • Two variables, no noise, an exact quadratic, one starting point.
  • For Adam only the learning rate was tuned; β₁ and β₂ are the defaults.
  • This shows what each preconditioner is based on. It says nothing about how well Adam trains neural networks.

In one sentence

Adam and Newton both “rescale the step per direction”. But Adam looks at how steep the slope is, one axis at a time; Newton looks at the full shape of the valley. When the valley is tilted, only the method that sees the shape helps.