Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsOptimization

Second-order methods

P1.F635 min●●○ IntermediateReviewed Dec 1, 2026

Why this matters for machine learning

Curvature tells you how far to step. Newton's method uses it to take very few, very good steps, which makes it a standard tool for small, smooth problems. It also explains why deep learning, with its millions or billions of parameters, almost always settles for the gradient alone.

  • Logistic regression is a small convex problem, and Newton-type solvers fit it in a handful of iterations. Newton-CG, Newton-Cholesky and L-BFGS are all options of scikit-learn's LogisticRegression. (P3.A7: Logistic regression)
  • On the squared-error loss of linear regression, one Newton step from any starting point lands on the solution of the normal equations. (P3.A2: Linear regression)
  • The memory of the Hessian grows with the square of the number of parameters, which is one main reason (noisy mini-batch estimates and non-convex losses are others) that networks are trained with first-order optimizers such as SGD with momentum and the Adam family. (P4.13: Optimizers)
  • Newton's method solves “the gradient is zero”, so it can be drawn to a saddle or a maximum. This is one reason the shape of the loss surface matters. (P1.F2: Convex vs. non-convex)

Before this: Gradient descent, The Hessian, curvature and Taylor approximation, Inverse and linear systems

After this lesson you can

  • Write Newton's step in one variable and in many, and say what it does to a quadratic
  • Run a few Newton steps by hand and say what quadratic convergence looks like
  • Explain, with an example each, how Newton's method fails (negative curvature, tiny curvature, a far-away start, a singular Hessian)
  • Name the standard repairs and the cheaper approximations (damping, trust regions, adding a multiple of the identity matrix, quasi-Newton, Hessian-vector products)
  • Explain why Newton's method is rarely used for large neural networks, and where it is used

Walk down a hillside in thick fog. Gradient descent feels only the slope under its feet, and then needs to be told how long a stride to take. Too short is slow. Too long overshoots, and past a limit it falls off the other side (P1.F3). The right stride depends on how sharply the ground bends, and gradient descent does not measure that.

Newton’s method measures it. It feels the slope and the bend (the second derivative, P1.C8), builds a small bowl that matches both, and jumps to the bottom of that bowl. If the ground really is a bowl, it arrives in one step. If the ground is not, the bowl can be a poor guide, and this lesson shows how. It also explains why almost no large neural network is trained this way.

From slope and bend to a step

P1.C8 built the quadratic model of a function around the point xx: the value, plus the slope times the step dd, plus half the curvature times d2d^2.

f(x+d)≈f(x)+f′(x) d+12f′′(x) d2.\begin{aligned} f(x+d)\approx{}& f(x)+f'(x)\,d\\ &+\tfrac12 f''(x)\,d^2 . \end{aligned}

This is a parabola in dd. If f′′(x)>0f''(x)>0 it opens upward and has a lowest point. To find it, set the derivative of the right-hand side with respect to dd to zero: f′(x)+f′′(x) d=0f'(x)+f''(x)\,d=0. So the bottom is at d=−f′(x)/f′′(x)d=-f'(x)/f''(x), and Newton’s method repeats

x←x−f′(x)f′′(x).x\leftarrow x-\frac{f'(x)}{f''(x)} .

There is a second way to read the same formula. Finding a minimum means solving the equation f′(x)=0f'(x)=0. Draw the tangent line of f′f' at the current xx (its slope is f′′(x)f''(x)) and move to the point where that line crosses zero: that is x−f′(x)/f′′(x)x-f'(x)/f''(x). This is the root-finding method that Newton’s name is attached to, applied to f′f'. Keep this second reading in mind: it looks for the zero of f′f' and nothing more.

Compare with gradient descent, x←x−ηf′(x)x\leftarrow x-\eta f'(x). Newton’s method is gradient descent with the step size η=1/f′′(x)\eta=1/f''(x), worked out again at every point from the local curvature. No step size has to be chosen.

On a quadratic: one step, whatever the curvature

Take f(x)=(x−1)2f(x)=(x-1)^2, which has f′(x)=2(x−1)f'(x)=2(x-1) and f′′(x)=2f''(x)=2. From x=4x=4: f′=6f'=6, f′′=2f''=2, and the step is −6/2=−3-6/2=-3, so Newton’s method lands on x=1x=1, the minimum. The parabola it jumps to is ff itself, because ff is already a parabola.

Gradient descent with η=0.5=1/f′′\eta=0.5=1/f'' gives 4−0.5⋅6=14-0.5\cdot 6=1 as well. With η=0.1\eta=0.1 it moves only to 3.43.4. P1.F3 showed that the error is multiplied by 1−ηf′′1-\eta f'' at every step. With η=1/f′′\eta=1/f'' that factor is exactly 00: the best possible step size is the reciprocal of the curvature, and Newton’s method finds it by itself.

Now make the bowl five times sharper, f(x)=5(x−1)2f(x)=5(x-1)^2. At x=4x=4: f′=30f'=30, f′′=10f''=10, and Newton’s step is still −3-3: it lands on 11. A fixed η=0.5\eta=0.5 would now multiply the error by 1−0.5⋅10=−41-0.5\cdot 10=-4 and blow up, because the safe range shrank to η<2/10=0.2\eta<2/10=0.2. Newton’s step adapts to the curvature, and a fixed step size does not.

A function that is not a quadratic

For f(x)=ex−2xf(x)=\mathrm{e}^x-2x the slope is f′(x)=ex−2f'(x)=\mathrm{e}^x-2 and the curvature is f′′(x)=exf''(x)=\mathrm{e}^x. The minimum is where ex=2\mathrm{e}^x=2, at x∗=ln⁡2=0.693147…x^*=\ln 2=0.693147\ldots, and there f′′=2f''=2. Start at x=0x=0 and use a calculator for the second step.

  • At x=0x=0: f′=−1f'=-1, f′′=1f''=1, step =−(−1)/1=+1=-(-1)/1=+1, so x=1x=1.
  • At x=1x=1: f′=e−2=0.7183f'=\mathrm{e}-2=0.7183, f′′=e=2.7183f''=\mathrm{e}=2.7183, step =−0.2642=-0.2642, so x=0.7358x=0.7358.
  • Next: 0.69400.6940, then 0.6931480.693148, then 0.69314718060.6931471806.

The error ∣x−x∗∣\lvert x-x^*\rvert after each step, for Newton’s method and for gradient descent with η=0.1\eta=0.1 (running the code confirms every entry):

StepNewtonGD, η=0.1\eta=0.1
13.1×10−13.1\times10^{-1}5.9×10−15.9\times10^{-1}
24.3×10−24.3\times10^{-2}5.0×10−15.0\times10^{-1}
39.0×10−49.0\times10^{-4}4.2×10−14.2\times10^{-1}
44.0×10−74.0\times10^{-7}3.6×10−13.6\times10^{-1}
58.0×10−148.0\times10^{-14}3.0×10−13.0\times10^{-1}

Newton’s errors fall like 10−1,10−2,10−4,10−7,10−1410^{-1},10^{-2},10^{-4},10^{-7},10^{-14}. The number of correct decimal places goes roughly 0,1,3,6,130,1,3,6,13, so it roughly doubles at every step. This is quadratic convergence: the new error is about a constant times the square of the old one, ek+1≈C ek2e_{k+1}\approx C\,e_k^2. Here C≈12C\approx\frac12 (the ratios ek+1/ek2e_{k+1}/e_k^2 settle near 0.50.5). Gradient descent has linear convergence: the error is multiplied by about the same factor every step (1−ηf′′(x∗)=0.81-\eta f''(x^*)=0.8 here), so it needs 8383 steps to get below 10−810^{-8}, against 55 for Newton’s method.

Quadratic convergence is a promise with conditions. It holds when f′′(x∗)≠0f''(x^*)\ne0 at the minimum, when ff is smooth enough (its second derivative must not jump around), and when the start is close enough to x∗x^*. The next sections are about what happens when the last condition fails. One honest comparison first: gradient descent with the perfect step size η=1/f′′(x∗)=0.5\eta=1/f''(x^*)=0.5 also needs only 55 steps on this function. The gain of Newton’s method is that it does not need to be told that number.

The widget: Newton against gradient descent

Both methods start at the same xx. Blue dots are gradient descent. Pink squares are Newton’s method, and the dashed pink parabola is the model it jumps to from its current square; the pink diamond is the parabola’s lowest (or highest) point. The chart shows each method’s distance to the minimum on a log scale. Switch functions, drag the start, change η\eta, and turn on damped Newton (explained below) to see the failures and the repair.

eˣ − 2x. f(x) = eˣ − 2x is convex, with its minimum at x = ln 2 ≈ 0.693 where f″ = 2. Far to the left it is almost a straight line.

−3−2−101230510xf(x)
gradient descent (dots) Newton (squares) Newton's parabola at its square its bottom (or top) start (drag it)

Short arrows along the bottom edge: orange is the gradient step, pink the Newton step. Orange segment: the tangent at the blue dot.

Newton (pink) has reached a minimum at x = 0.6931: f′ = 0 and f″ = 2 is positive. Gradient descent (blue), step 12: x = 0.6216, error 7.2e−2.

Vertical axis: log₁₀ |x − x*|, the distance to the minimum on a log scale (lower is better)

024681012−15−10−50

step

Values below 10⁻¹⁶ are drawn on the bottom line. Where a path stops (it left the range of the chart), its line ends.

Positions and errors of both methods at each step shown
stepGD xGD err.Newt. xNewt. err.
006.9e−106.9e−1
10.15.9e−113.1e−1
20.18955.0e−10.73584.3e−2
30.26864.2e−10.6949.0e−4
40.33783.6e−10.69314.0e−7
50.39763.0e−10.69318.0e−14
60.44882.4e−10.69310
70.49212.0e−10.69310
80.52861.6e−10.69310
90.55891.3e−10.69310
100.5841.1e−10.69310
110.60478.8e−20.69310
120.62167.2e−20.69310
Gradient descent, η = 0.1: next step −η·f′(x) = 0.0138
within 1.0e−8: not within 12 steps
steps that raised f: 0
Newton: f′ = 0, f″ = 2, next step −f′/f″ = 0
within 1.0e−8: step 5
steps that raised f: 0
x* is the minimum the error is measured from: 0.6931. “Within” means the error is below 1.0e−8 at that step, counted over all 12 steps.

Play shows one step every two thirds of a second.

Blue dots: gradient descent with step size η. Pink squares: Newton's method. The dashed pink curve is the parabola that Newton's method jumps to from its current point; the pink diamond is that parabola's lowest or highest point. Orange arrows along the bottom edge are the next steps (gradient step in orange, Newton step in pink). Pick a function, drag the start ring or move the Start slider, change η, and press Play or Step. The chart and the table show the distance to the minimum after each step.

Things to try, all checked against the widget’s own rules (“within 10−810^{-8}” means the error is below 10−810^{-8} at that step; the widget runs 12 steps):

  • Smooth and convex, the defaults (start 00, η=0.1\eta=0.1). Newton’s method is within 10−810^{-8} at step 5. Gradient descent has error 7.2×10−27.2\times10^{-2} after 12 steps (it needs 83). Press the button that sets η≈1/f′′\eta\approx1/f'' at the minimum (0.50.5): gradient descent now arrives at step 5 too.
  • Quadratic, start 4. Newton’s method is exact at step 1, for any η\eta. Gradient descent with η=0.5\eta=0.5 is exact at step 1 as well. At η=1\eta=1 (which is exactly 2/f′′2/f'') it bounces 4,−2,4,−2,…4,-2,4,-2,\dots for ever. At η=1.1\eta=1.1 the error grows by a factor 1.21.2 each step: after 12 steps x=27.7x=27.7.
  • Smooth and convex, start −3.4-3.4. There the function is almost a straight line, so f′′=0.033f''=0.033 is tiny and Newton’s first jump is +58.9+58.9, to x=55.5x=55.5. The function is convex and Newton’s method still goes the wrong way for a long time: at x=55x=55, f′′f'' is huge, so it walks back only one unit per step (60 steps in all). Gradient descent with η=0.5\eta=0.5 is within 10−810^{-8} at step 8. Tick damped Newton and Newton’s method arrives at step 6.
  • Two valleys and a hill, start 0.5. Here f′′=−1.25f''=-1.25, so the parabola opens downward and Newton’s step goes to its top. It moves 0.5→0.12→0.2026→0.20430.5\to0.12\to0.2026\to0.2043, the local maximum, and three of its steps raise ff. Gradient descent with η=0.1\eta=0.1 walks toward the right-hand valley. With damped Newton the path ends at the minimum, x=1.301x=1.301, at step 6.
  • Newton cycles, start 0. The step from 00 goes to 11 and the step from 11 goes back to 00, for ever. Damped Newton goes −1,−1.75,−1.7696,…-1,-1.75,-1.7696,\ldots and arrives at step 5. From start 0.30.3 the pure steps jump around (1.121.12, 0.470.47, 1.341.34, 0.830.83, then −9.84-9.84) before settling down toward the minimum near −1.77-1.77.
  • Convex, yet Newton diverges, start 2. This function has f′′=1/(1+x2)>0f''=1/(1+x^2)>0 everywhere. Newton’s steps go 2,−3.54,13.95,−279.3,1.2×1052,-3.54,13.95,-279.3,1.2\times10^5 and the path stops at step 5 when ∣x∣\lvert x\rvert passes one million. From start 1.21.2 it converges at step 5. The dividing line is x≈1.39x\approx1.39: start at 1.351.35 and it converges (step 7), start at 1.41.4 and it escapes (step 9). Gradient descent with η=1\eta=1 reaches the minimum at step 4, so the more elaborate method is not always the better one.

Many variables

For several parameters, the second derivative becomes the Hessian matrix H\mathbf{H} (P1.C8) and the slope becomes the gradient g=∇f\mathbf{g}=\nabla f. The quadratic model is f(p+Δ)≈f(p)+g⋅Δ+12Δ⊤HΔf(\mathbf{p}+\boldsymbol{\Delta})\approx f(\mathbf{p})+\mathbf{g}\cdot\boldsymbol{\Delta}+\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta}, and setting its gradient to zero gives Newton’s step

Δ=−H−1g.\boldsymbol{\Delta}=-\mathbf{H}^{-1}\mathbf{g}.

In practice the inverse is never formed. The step is the solution of the linear system HΔ=−g\mathbf{H}\boldsymbol{\Delta}=-\mathbf{g}, and np.linalg.solve does that faster and more accurately than inverting (P1.B9).

P1.C8 did this once. On the loss of P0.2 from (1,0)(1,0) the step is Δ=(1.5,−1)\boldsymbol{\Delta}=(1.5,-1), which lands on (2.5,−1)(2.5,-1), the minimum with L=0.5\mathcal{L}=0.5, in one step. Gradient descent on the same loss is held back by the condition number κ=46.1\kappa=46.1 (P1.C8).

Now the ravine of P1.F5, L=x2+κy2\mathcal{L}=x^2+\kappa y^2, with H=diag(2,2κ)\mathbf{H}=\mathrm{diag}(2,2\kappa). From (−3.5,1)(-3.5,1) the gradient is (−7,2κ)(-7,2\kappa) and the Newton step is (3.5,−1)(3.5,-1) for every κ\kappa: it lands exactly on (0,0)(0,0) for κ=10\kappa=10, 100100 and 10001000. Plain gradient descent with η=0.95/κ\eta=0.95/\kappa needs 3333, 186186 and 18701870 steps (P1.F5). Turn the bowl by 45∘45^\circ and the answer does not change: the Hessian for κ=100\kappa=100 becomes (101−99−99101)\begin{pmatrix}101&-99\\-99&101\end{pmatrix}, the step solves the full system, and the minimum is still reached in one step (to rounding error of about 10−1510^{-15}, as the second code cell shows). Dividing each coordinate by its own scale, as adaptive methods do, cannot fix such a tilted valley: for the diagonal of this H\mathbf{H} the condition number stays 100100 (P1.F5). Newton’s method uses the off-diagonal numbers too, and those hold the tilt.

Go deeper Why Newton's step does not care how the axes are chosen

Describe the same loss in new coordinates x=Ay\mathbf{x}=\mathbf{A}\mathbf{y} with an invertible matrix A\mathbf{A} (a stretch, a turn, or any mix). In the new coordinates the gradient is A⊤g\mathbf{A}^\top\mathbf{g} and the Hessian is A⊤HA\mathbf{A}^\top\mathbf{H}\mathbf{A}. Newton’s step there is −(A⊤HA)−1A⊤g=−A−1H−1g-(\mathbf{A}^\top\mathbf{H}\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{g}=-\mathbf{A}^{-1}\mathbf{H}^{-1}\mathbf{g}, and mapping it back with A\mathbf{A} gives −H−1g-\mathbf{H}^{-1}\mathbf{g}: the same step in the original coordinates. Gradient descent is different: its step in the new coordinates, −ηA⊤g-\eta\mathbf{A}^\top\mathbf{g}, maps back to −ηAA⊤g-\eta\mathbf{A}\mathbf{A}^\top\mathbf{g}, which is not −ηg-\eta\mathbf{g}. So the speed of gradient descent depends on how the problem is written (this is why rescaling the inputs helps, P3.A11), and Newton’s method is immune to that.

Least squares and logistic regression

Linear regression with the loss 12∥Xw−y∥2\tfrac12\lVert\mathbf{X}\mathbf{w}-\mathbf{y}\rVert^2 has gradient X⊤(Xw−y)\mathbf{X}^\top(\mathbf{X}\mathbf{w}-\mathbf{y}) and a constant Hessian X⊤X\mathbf{X}^\top\mathbf{X}. The loss is a quadratic in w\mathbf{w}, so one Newton step from any starting weights lands on the minimum, and that minimum satisfies X⊤X w=X⊤y\mathbf{X}^\top\mathbf{X}\,\mathbf{w}=\mathbf{X}^\top\mathbf{y}: the normal equations of P3.A2. Solving the normal equations is one Newton step. (Averaging the loss over the NN examples divides both the gradient and the Hessian by NN, so the step does not change.) The step needs X⊤X\mathbf{X}^\top\mathbf{X} to be invertible, which fails when two features are copies of each other; the fix of adding μI\mu\mathbf{I} comes up below.

Logistic regression (P3.A7) has no closed form, but its loss is convex and the number of weights is small. Newton’s method on it is classical, and it is the idea behind the iteratively reweighted least squares that statistical software uses for it. The third code cell below counts the steps on a small data set: Newton’s method needs 7 steps, and gradient descent with the standard safe step size 1/L1/L (where LL bounds the curvature) needs 302, both counted until the gradient norm is below 10−810^{-8}.

Where Newton’s method goes wrong

Newton’s method trusts the parabola. Every failure is a case where the parabola is a bad copy of ff, or where it has no bottom to jump to.

1. The curve bends the wrong way. On the two-valley curve f(x)=x44−x2+0.4xf(x)=\frac{x^4}{4}-x^2+0.4x of P1.F3, at x=0.5x=0.5 the slope is f′=−0.475f'=-0.475 and the curvature is f′′=−1.25f''=-1.25. The parabola opens downward. Its stationary point is a top, and Newton’s step −f′/f′′=−0.38-f'/f''=-0.38 goes there, to x=0.12x=0.12. The function value rises from −0.0344-0.0344 to 0.03370.0337. The next steps are 0.20260.2026 and 0.20430.2043, which is the local maximum of ff. Gradient descent from the same point with η=0.1\eta=0.1 slides downhill into the valley at 1.301.30.

This is the core weakness. Newton’s method solves f′(x)=0f'(x)=0 (second reading above), and a maximum and a saddle solve it as well as a minimum does. Near a maximum it converges as fast as near a minimum. In many variables the same happens when H\mathbf{H} has a negative eigenvalue (P1.B12, P1.C8). For f=x2−y2f=x^2-y^2, a saddle (P1.F2), the Hessian is diag(2,−2)\mathrm{diag}(2,-2), and from (1,0.01)(1,0.01) Newton’s step goes straight to (0,0)(0,0), the saddle, in one step. Gradient descent with η=0.1\eta=0.1 multiplies xx by 0.80.8 and yy by 1.21.2 at every step, so after 30 steps it has left the saddle along yy (at (0.001,2.37)(0.001,2.37)). From almost every start, gradient descent is pushed away from a saddle, slowly (started exactly on y=0y=0 it would converge to the saddle). Newton’s method is pulled in.

2. Almost no curvature. On ex−2x\mathrm{e}^x-2x from x=−3.4x=-3.4 the curvature is 0.0330.033 and the slope is −1.97-1.97, so the step −f′/f′′-f'/f'' is +58.9+58.9. In one dimension a tiny f′′f'' means “the parabola is very wide, so its bottom is very far away”. When f′′=0f''=0 exactly there is no bottom at all and the step is undefined.

3. Far from the minimum. The parabola is only a local model. The function xarctan⁡x−12ln⁡(1+x2)x\arctan x-\tfrac12\ln(1+x^2) has f′′=1/(1+x2)>0f''=1/(1+x^2)>0 everywhere, so it is convex, yet from x=2x=2 the Newton path is 2,−3.54,13.95,−279.3,…2,-3.54,13.95,-279.3,\ldots: each jump is bigger than the last. The curve x44−x2+2x\frac{x^4}{4}-x^2+2x makes Newton’s method alternate 0→1→0→10\to1\to0\to1 for ever. Even for a convex function, quadratic convergence is a promise about the start being close.

4. A singular Hessian. If H\mathbf{H} has an eigenvalue 00, the system HΔ=−g\mathbf{H}\boldsymbol{\Delta}=-\mathbf{g} has no solution or infinitely many. For f=x2+yf=x^2+y the Hessian is diag(2,0)\mathrm{diag}(2,0) and the gradient is (2x,1)(2x,1): the second equation reads 0⋅Δy=−10\cdot\Delta_y=-1, and np.linalg.solve raises an error. If the eigenvalue is tiny but not zero, the step is enormous: with diag(2,10−12)\mathrm{diag}(2,10^{-12}) the step along yy is −1012-10^{12}. This is the many-variable version of failure 2.

Repairs

Each of these is a family of methods with its own lesson in a numerical-optimization course. Here is the idea of each, in a few lines.

  • Damping and line search. Use the Newton direction but do not trust the full length: halve the step until ff really drops. The widget’s damped Newton does this. It also uses ∣f′′∣\lvert f''\rvert in place of f′′f'', with a floor of 0.10.1, so that a negative curvature no longer turns the step uphill and a tiny one does not blow it up. From x=0.5x=0.5 on the two-valley curve it ends at the minimum 1.3011.301 (step 6), from −3.4-3.4 on ex−2x\mathrm{e}^x-2x it arrives at step 6, and on the cycling curve it reaches the minimum at step 5. Near a minimum it takes the full Newton step, so the fast convergence stays.
  • Trust regions. Decide how far the parabola can be trusted (a radius). Take the best step inside that radius. If the parabola predicted the drop well, enlarge the radius; if it did not, shrink it.
  • Adding a multiple of the identity. Use (H+μI)Δ=−g(\mathbf{H}+\mu\mathbf{I})\boldsymbol{\Delta}=-\mathbf{g}. The eigenvalues of H+μI\mathbf{H}+\mu\mathbf{I} are the old ones plus μ\mu, so if μ\mu is bigger than the size of the most negative eigenvalue (μ>−λmin⁡\mu>-\lambda_{\min}) they are all positive, and the matrix is invertible. For μ=0\mu=0 this is Newton’s step. For a large μ\mu it is −g/μ-\mathbf{g}/\mu, a small gradient step with η=1/μ\eta=1/\mu. On ex−2x\mathrm{e}^x-2x at x=−3x=-3 (where f′=−1.95f'=-1.95 and f′′=0.0498f''=0.0498) the step is 39.239.2 for μ=0\mu=0, 1.861.86 for μ=1\mu=1 and 0.1940.194 for μ=10\mu=10. On the two-valley curve at x=0.5x=0.5, μ=2\mu=2 turns the curvature −1.25-1.25 into +0.75+0.75 and the step becomes +0.633+0.633, downhill, to x=1.133x=1.133 with f=−0.4187f=-0.4187. This is the Levenberg–Marquardt idea (Levenberg 1944, Marquardt 1963, for least-squares fitting); ridge regression adds the same kind of term (P3.A6).
  • Quasi-Newton methods replace H\mathbf{H} by an estimate built from the gradients seen so far. Hessian-vector products avoid ever writing H\mathbf{H} down. Both are in boxes below.
Go deeper Gauss–Newton and Levenberg–Marquardt for least squares

For a loss 12∑iri(θ)2\tfrac12\sum_i r_i(\boldsymbol{\theta})^2 made of residuals rir_i (prediction minus target), let J\mathbf{J} be the matrix of derivatives of the residuals (the Jacobian, P1.C7). The gradient is J⊤r\mathbf{J}^\top\mathbf{r}, and the Hessian is J⊤J+∑iri∇2ri\mathbf{J}^\top\mathbf{J}+\sum_i r_i\nabla^2 r_i. Gauss–Newton keeps only the first part: it solves J⊤J Δ=−J⊤r\mathbf{J}^\top\mathbf{J}\,\boldsymbol{\Delta}=-\mathbf{J}^\top\mathbf{r}. The matrix J⊤J\mathbf{J}^\top\mathbf{J} is positive semi-definite (it has no negative curvature), and it needs no second derivatives. It can still be singular, which is why the Levenberg–Marquardt fix below is needed. It is exact when the residuals are linear in θ\boldsymbol{\theta}, which is linear regression and its normal equations. Levenberg–Marquardt solves (J⊤J+μI)Δ=−J⊤r(\mathbf{J}^\top\mathbf{J}+\mu\mathbf{I})\boldsymbol{\Delta}=-\mathbf{J}^\top\mathbf{r} and adjusts μ\mu during the run. These are standard tools for fitting curves to data, for example in SciPy’s least_squares.

Go deeper Quasi-Newton methods: learning the curvature from gradients

Newton’s method needs f′′f''. But f′′f'' is how fast f′f' changes, and two slopes at two points show that. In one variable, replace f′′(xk)f''(x_k) by f′(xk)−f′(xk−1)xk−xk−1\dfrac{f'(x_k)-f'(x_{k-1})}{x_k-x_{k-1}}. This is the secant method applied to f′f'. On ex−2x\mathrm{e}^x-2x with x0=0x_0=0 and x1=1x_1=1 the errors are 3.1×10−13.1\times10^{-1}, 1.1×10−11.1\times10^{-1}, 1.6×10−21.6\times10^{-2}, 9.3×10−49.3\times10^{-4}, 7.7×10−67.7\times10^{-6}, 3.6×10−93.6\times10^{-9}, 1.4×10−141.4\times10^{-14}. It is a little slower than Newton’s method (its order of convergence is about 1.61.6, not 22) and it never computes a second derivative.

In many variables the same idea is the requirement that the new Hessian estimate B\mathbf{B} satisfy B (θk−θk−1)=gk−gk−1\mathbf{B}\,(\boldsymbol{\theta}_{k}-\boldsymbol{\theta}_{k-1})=\mathbf{g}_k-\mathbf{g}_{k-1}. BFGS (found independently by Broyden, Fletcher, Goldfarb and Shanno, all in 1970) updates B\mathbf{B} or its inverse by a small correction at every step. It still stores an n×nn\times n matrix, so for large nn it has the same memory problem. L-BFGS (Liu and Nocedal, 1989) stores only the last mm pairs of vectors (the parameter change and the gradient change), often mm between 5 and 20, which costs about 2mn2mn numbers, and computes the step from them without ever forming a matrix. It is a common choice for medium-sized, full-batch problems and is the default solver of scikit-learn’s LogisticRegression. The difficulty with noisy mini-batch gradients is that the gradient differences it relies on are then mostly noise, and making quasi-Newton methods work in that setting is a research topic.

Go deeper Hessian-free methods: Newton's step from Hessian-vector products only

The step solves HΔ=−g\mathbf{H}\boldsymbol{\Delta}=-\mathbf{g}. The conjugate gradient method solves such a system (for positive definite H\mathbf{H}) using only products Hv\mathbf{H}\mathbf{v} for a sequence of vectors v\mathbf{v}: in exact arithmetic it finishes in at most nn iterations, and stopping after a few gives an approximate step. A Hessian-vector product is cheap without the Hessian: it is the change of the gradient along v\mathbf{v}, Hv≈(∇f(p+εv)−∇f(p))/ε\mathbf{H}\mathbf{v}\approx\bigl(\nabla f(\mathbf{p}+\varepsilon\mathbf{v})-\nabla f(\mathbf{p})\bigr)/\varepsilon, or computed exactly by differentiating the gradient (automatic differentiation, P1.C10), at a cost of a small multiple of one gradient. So a Newton-like step needs memory of order nn and no n×nn\times n matrix. This is the idea of Newton-CG and of the Hessian-free optimization that Martens popularized for neural networks around 2010; it is why the second-order methods that are used for large models work with products and not with matrices. A damping term μI\mu\mathbf{I} is normally added, and each conjugate-gradient iteration costs about as much as one more pass over the data.

Go deeper Newton's method when the second derivative is zero at the minimum

The function f(x)=x4f(x)=x^4 has its minimum at 00 with f′′(0)=0f''(0)=0, so a condition of quadratic convergence fails. Newton’s step is x−4x3/(12x2)=23xx-4x^3/(12x^2)=\tfrac23x: the error is multiplied by 23\tfrac23 at every step, which is linear convergence: from x=1x=1 it goes 0.667,0.444,0.296,0.198,0.1320.667,0.444,0.296,0.198,0.132. That is still much better than gradient descent here (with η=0.1\eta=0.1 it is at 0.3910.391 after 5 steps and, as P1.F3 found, at 0.110.11 after 100), because (2/3)100≈2.5×10−18(2/3)^{100}\approx2.5\times10^{-18}. The general rule: when f′′f'' vanishes at the minimum, Newton’s method slows to linear convergence, and when it is near zero the parabola is almost flat and the steps are large and unreliable.

Why it is rarely used at scale

The case for Newton’s method is the number of steps: five instead of eighty-three, seven instead of three hundred. The case against is the price of each step and a few more things that go wrong for neural networks.

  • Memory. A model with nn parameters has an n×nn\times n Hessian. For n=106n=10^6 that is 101210^{12} entries, about 44 terabytes in 32-bit numbers (P1.C8). For n=109n=10^9 it is 101810^{18} entries, 4×10184\times10^{18} bytes. For a model with 1.75×10111.75\times10^{11} parameters (the size reported for GPT-3) it is about 3×10223\times10^{22} entries, around 102310^{23} bytes.
  • Time. A dense solve of HΔ=−g\mathbf{H}\boldsymbol{\Delta}=-\mathbf{g} costs of order n3n^3 operations: about n3/3n^3/3 with a Cholesky factorization (for a positive definite H\mathbf{H}) and about 2n3/32n^3/3 with the LU method that np.linalg.solve uses. Taking n3/3n^3/3 and n=109n=10^9 gives 3×10263\times10^{26} for a single step: at an assumed 101710^{17} operations per second that is about a hundred years (about twice as long with LU). A gradient costs about as much as a pass over the data.
  • Noise. The training loss is an average over millions of examples, and a mini-batch (P1.F4) gives a noisy estimate of its gradient and of its Hessian. Newton’s method divides by the curvature, so directions with small curvature get large steps, and an error in a small curvature is amplified. If the true curvature is 0.10.1 and the gradient is 11, the step is 1010; a mini-batch estimate of 0.050.05 or 0.150.15 gives 2020 or 6.76.7, and an estimate at or below 00 gives a huge or wrong-way step.
  • Non-convexity. The loss of a network has saddles and regions of negative curvature, where plain Newton’s method is drawn to the wrong kind of point (above). The repairs help, and cost more per step.
  • Cost against steps. For logistic regression with dd weights and NN examples, a gradient costs about NdN d operations. A Newton step builds the Hessian (about Nd2N d^2) and solves (order d3d^3). When NN is at least about dd, that is roughly dd times more per step (if NN is smaller, the solve dominates). With d=3d=3 and 302/7≈43302/7\approx43 times fewer steps, Newton’s method wins easily. With d=106d=10^6 weights, gradient descent would have to need about 10610^6 times more steps for Newton’s method to break even, and the 101210^{12}-entry Hessian could not even be stored. Newton’s method wins when the number of parameters is small, and the advantage disappears as it grows.

Where second-order ideas do appear:

  • Small and medium problems. Newton-type solvers and L-BFGS fit logistic regression and many other classical models, and are standard in statistics software. L-BFGS is also used for full-batch problems of moderate size.
  • Least squares. Gauss–Newton and Levenberg–Marquardt, above.
  • Curvature as a research tool for networks. Estimating the largest curvature λmax⁡\lambda_{\max} with Hessian-vector products (P1.C8), natural-gradient methods and cheaper preconditioners such as K-FAC and Shampoo (proposed around 2015 and 2018) are studied as ways to get some of the benefit of Newton’s method at a price like that of a gradient. How well they compare with Adam-style methods depends on the model and the setting, and it is still an active research question.

What does train most deep networks is first-order: SGD with momentum, and Adam and its relatives (P4.13). They use only gradients. A rescaling by the size of recent gradients is not curvature (P1.F5), so these methods are not second-order methods, however much their step may look like a division.

The recipe in one place

Intuition

Newton’s method draws a bowl that matches the ground at your feet (height, slope and bend) and walks to the bottom of that bowl. If the ground is a bowl, you arrive at once. Near the bottom of a smooth valley the bowl is a very good copy, so each step roughly doubles the number of correct digits. Far away the bowl can be a bad copy: if the ground bends the wrong way you walk to a hilltop, if it is almost flat the bowl’s bottom is miles away, and if the bowl does not exist (no curvature in some direction) there is nowhere to go. The repairs shorten the walk, or bend the bowl upward, or only trust it nearby.

The price is that you must measure the bend in every direction at once, and for a model with a million parameters that is a million-by-million table.

Math

One variable: xk+1=xk−f′(xk)f′′(xk)x_{k+1}=x_k-\dfrac{f'(x_k)}{f''(x_k)}. This is gradient descent with ηk=1/f′′(xk)\eta_k=1/f''(x_k), and Newton’s root-finding method applied to f′f'.

Many variables: Δ=−H−1g\boldsymbol{\Delta}=-\mathbf{H}^{-1}\mathbf{g}, computed by solving HΔ=−g\mathbf{H}\boldsymbol{\Delta}=-\mathbf{g}.

Convergence: if f′′(x∗)≠0f''(x^*)\ne0, f′′′f''' is continuous and x0x_0 is close enough to x∗x^*, then

ek+1≈f′′′(x∗)2f′′(x∗) ek2,ek=xk−x∗.\begin{aligned} e_{k+1}&\approx\frac{f'''(x^*)}{2f''(x^*)}\,e_k^2,\\ e_k&=x_k-x^*. \end{aligned}

For gradient descent on a quadratic, ek+1=(1−ηf′′) eke_{k+1}=(1-\eta f'')\,e_k, which is 00 for η=1/f′′\eta=1/f''.

Quadratic: f(p+Δ)f(\mathbf{p}+\boldsymbol{\Delta}) equals its own model, so one step reaches the minimum when H\mathbf{H} is positive definite.

Failures: f′′<0f''<0 (or H\mathbf{H} with a negative eigenvalue): the stationary point of the model is a top or a saddle. f′′→0f''\to0: ∣Δ∣→∞\lvert\Delta\rvert\to\infty. H\mathbf{H} singular: no unique solution.

Repairs: (H+μI)Δ=−g(\mathbf{H}+\mu\mathbf{I})\boldsymbol{\Delta}=-\mathbf{g} with eigenvalues λi+μ\lambda_i+\mu; step halving until f(p+tΔ)≤f(p)+c t g⋅Δf(\mathbf{p}+t\boldsymbol{\Delta})\le f(\mathbf{p})+c\,t\,\mathbf{g}\cdot\boldsymbol{\Delta} with c=10−4c=10^{-4} (the widget’s rule); trust regions; BFGS, L-BFGS; conjugate gradient with Hv\mathbf{H}\mathbf{v} products.

Cost: memory n2n^2, a dense solve of order n3n^3 operations (n3/3n^3/3 by Cholesky for positive definite H\mathbf{H}, about 2n3/32n^3/3 by LU).

Code

import numpy as np

def newton(grad, hess, theta, steps):
    for _ in range(steps):
        g, H = grad(theta), hess(theta)
        theta = theta + np.linalg.solve(H, -g)   # solve H d = -g, never inv(H)
    return theta

For a model with many parameters you would not call hess. PyTorch can compute a Hessian-vector product by differentiating the gradient a second time (torch.autograd.grad with create_graph=True), and scipy.optimize.minimize(method="L-BFGS-B") or "Newton-CG" give ready-made solvers for medium problems.

Try it: Newton against gradient descent in one variable, and two ways to fail

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

The second cell is the many-variable version: the one-step landings, a saddle, a singular Hessian, and least squares.

Try it: Newton's step in many variables

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The third cell counts the steps on a small logistic regression, with the data made by a formula so that every run gives the same numbers.

Try it: logistic regression with Newton's method and with gradient descent

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Check your understanding

  1. For f(x)=3(x−2)2+1f(x)=3(x-2)^2+1, where does one Newton step from x=10x=10 land?

  2. At the current point f′=−0.475f'=-0.475 and f′′=−1.25f''=-1.25. What does Newton's step do?

  3. Newton's method has quadratic convergence and the error is now about 10−310^{-3}, with the constant CC near 1. What error should the next two steps give, roughly?

  4. Why is a dense Newton step impractical for a network with 10910^9 parameters?

  5. A Hessian has eigenvalues −2-2 and 55. Which change makes the step solve a system whose matrix has only positive eigenvalues?

Exercises

  1. Newton’s method on f(x)=2x2−8x+3f(x)=2x^2-8x+3 from x=10x=10: where does it land?
    Show answerf′(10)=4⋅10−8=32f'(10)=4\cdot10-8=32 and f′′=4f''=4, so the step is −32/4=−8-32/4=-8 and the landing point is x=2x=2, which is where f′=0f'=0 (4x−8=04x-8=0). One step, because ff is a quadratic.
  2. Run one more Newton step on f(x)=ex−2xf(x)=\mathrm{e}^x-2x from x=1x=1.
    Show answerf′(1)=e−2=0.7183f'(1)=\mathrm{e}-2=0.7183 and f′′(1)=e=2.7183f''(1)=\mathrm{e}=2.7183, so the step is −0.7183/2.7183=−0.2642-0.7183/2.7183=-0.2642 and x=0.7358x=0.7358. The minimum is 0.69310.6931, so the error went from 0.3070.307 to 0.0430.043.
  3. For f(x)=x3−3xf(x)=x^3-3x (a local minimum at x=1x=1, a local maximum at x=−1x=-1) take one Newton step from x=0.5x=0.5 and one from x=−0.5x=-0.5. Where does each go?
    Show answerf′=3x2−3f'=3x^2-3 and f′′=6xf''=6x. From 0.50.5: f′=−2.25f'=-2.25, f′′=3f''=3, step +0.75+0.75, to 1.251.25, toward the minimum at 11. From −0.5-0.5: f′=−2.25f'=-2.25, f′′=−3f''=-3, step −0.75-0.75, to −1.25-1.25, toward the maximum at −1-1. The same formula goes to a minimum on one side and a maximum on the other, depending on the sign of f′′f''.
  4. A model has n=2×105n=2\times10^5 parameters. How much memory does its Hessian need in 32-bit numbers?
    Show answern2=4×1010n^2=4\times10^{10} entries, times 44 bytes, is 1.6×10111.6\times10^{11} bytes, about 160160 gigabytes, for one matrix.
  5. f(x,y)=2x2+12y2f(x,y)=2x^2+\tfrac12y^2. What is the Newton step from (4,4)(4,4), and where does it land?
    Show answerg=(4x,y)=(16,4)\mathbf{g}=(4x,y)=(16,4) and H=diag(4,1)\mathbf{H}=\mathrm{diag}(4,1), so Δ=−(16/4,4/1)=(−4,−4)\boldsymbol{\Delta}=-(16/4,4/1)=(-4,-4) and the landing point is (0,0)(0,0), the minimum, in one step.
  6. On ex−2x\mathrm{e}^x-2x at x=−3x=-3 the slope is −1.95-1.95 and the curvature is 0.04980.0498. What are the plain Newton step and the step with μ=1\mu=1 added to the curvature?
    Show answerPlain: 1.95/0.0498=39.21.95/0.0498=39.2. With μ=1\mu=1: 1.95/(0.0498+1)=1.861.95/(0.0498+1)=1.86. The added μ\mu keeps the step from exploding when the curvature is tiny.

Next: P1.F7 adds constraints: how to optimize when the answer must stay on a curve or inside a region. P4.13 returns to the optimizers used in practice, which are first-order, with full training runs.

Key takeaways

  • Newton's method fits a parabola to the function at the current point and jumps to the parabola's bottom: x ← x − f′(x)/f″(x). It is the same as gradient descent with η = 1/f″(x), recomputed at every point.
  • On a quadratic the parabola is the function, so one step reaches the minimum whatever the curvature. In many variables the step solves HΔ = −g (use solve, not an inverse), and on x² + κy² it lands on the minimum in one step for every κ, also when the bowl is tilted.
  • Near a minimum with f″ ≠ 0 the error is roughly squared at each step (quadratic convergence): on eˣ − 2x from 0 the errors are 3.1e−1, 4.3e−2, 9.0e−4, 4.0e−7, 8.0e−14. Gradient descent with η = 0.1 needs 83 steps to reach 1e−8.
  • Newton's method solves f′ = 0, so it does not know a minimum from a maximum or a saddle. With f″ < 0 it moves to the top of the parabola; with f″ near 0 it takes an enormous step; far from a minimum it can cycle or diverge even when f is convex; with a singular H there is no step.
  • Repairs: shorten the step until f drops (damping or line search), trust the parabola only nearby (trust region), use H + μI so that all curvatures are positive (Levenberg–Marquardt style), approximate H from gradient differences (BFGS, L-BFGS), or work with Hessian-vector products only.
  • For n parameters the Hessian has n² entries (about 4 terabytes in fp32 for n = 10⁶) and a dense solve costs about n³ operations. Mini-batch Hessians are noisy and the loss of a network is not convex, so first-order methods dominate deep learning.
  • Second-order ideas remain useful for small and medium problems (logistic regression, L-BFGS on full batches, least squares) and as research on cheaper preconditioners for networks.
Found a mistake? Tell us