Calculus and optimisation for ML: derivatives to convexity, Hessians, Newton, Lagrange

Reading: 32 min, plus 11 in closed details. Taylor models as the thread through gradients, Hessians, gradient descent and its step limit, Newton and quasi-Newton, convexity, Lagrange and KKT, stochastic gradients and the edge of stability, with two real runs. A child page of Topic: math. Data as of October 2026.

The idea in one screen

A gradient is a local straight-line model of the loss; a Hessian is a local bowl-shaped model; every optimiser is a rule for how far to trust that model.

Write the loss near where you stand as a Taylor model, L(θ+δ)≈L(θ)+g⊤δ+12δ⊤HδL(\theta + \delta) \approx L(\theta) + g^\top \delta + \tfrac12 \delta^\top H \delta. Every section reads something off it: which way is down (gg), how sharply it curves (HH), how big a step is safe (2/λmax⁡2/\lambda_{\max}), where the bottom is (−H−1g-H^{-1}g), and when the bottom you find is the only one (convexity). Constraints (Lagrange, KKT) and minibatch noise change the model; deep networks show where it stops being enough. Figure 1 draws it on a real loss: logistic regression with two weights on the breast-cancer data (two standardised features, mean radius and mean texture; 569 patients; the Optimisation lab's real landscape).

  1. A real loss over two weights. The surface is the average logistic loss at every weight pair (w1,w2)(w_1, w_2). You stand at w=(0,0)w = (0, 0), loss 0.693; the lowest point is the green optimum.
  2. The gradient: a tilted plane. The first-order model is the tangent plane (orange glass). Its steepest way down is −g-g, with slope ‖g‖\lVert g \rVert = 0.406. A plane has no bottom, so it says which way, never how far.
  3. The Hessian: a bowl. The second-order model adds the curvature (blue mesh): a bowl that touches the loss at ww, curving by 0.171 and 0.333 along its two axes (the eigenvalues of HH).
  4. Two trust rules, two steps. Gradient descent trusts the plane only within a step penalty, η=1/β\eta = 1/\beta with β\beta = 0.333 the largest curvature this loss can have, and lands at (1.06, 0.60). Newton trusts the bowl and jumps to its bottom, (1.28, 0.39), further and lower. The steeper axis curves by exactly β\beta here, so the two steps differ along the flatter one.
  5. Repeat to the optimum. Newton reaches a loss gap under 10−610^{-6} in 5 steps; gradient descent at η=1/β\eta = 1/\beta takes 83 steps. The optimum is (3.55, 0.83). Far from it the bowl can be badly wrong (section 5).

You need: eigenvalues and eigenvectors of a symmetric matrix and what positive definite means (Linear algebra for ML, sections 8 and 9). Elsewhere: how gradients are computed through a network (Jacobians, vector-Jacobian products, Hessian-vector products) is Matrix calculus and backprop; SGD, Adam, Muon and schedules in practice are Optimisers and learning-rate schedulers; floating point and stable softmax are Numerical computing for ML. This page is the mathematics underneath them.

Taylor model: value plus slope term plus curvature term, valid near a point.
Hessian HH: the matrix of second derivatives; its eigenvalues are curvatures.
Condition number κ\kappa: λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min}, how stretched the bowl is.
β\beta-smooth: curvature never above β\beta in any direction.
Convex: chords lie above the graph, so any flat point is the global minimum.
Saddle: a flat point that curves up in some directions and down in others.
Sharpness: the top Hessian eigenvalue during training.
Lagrange multiplier, KKT: the price of a constraint, and the conditions an optimum under constraints satisfies.
Reference: every idea of the page in one table
IdeaIn one lineOne number to rememberWhere ML uses it
Derivativeslope of the curve; the best straight-line fit near a pointslope of x2x^2 at 3 is 6; line error h2h^2every gradient
Gradientall the slopes; points uphill fastesttiny model: ‖g‖=1.153\lVert g \rVert = 1.153the direction of every update
Taylor modelvalue + slope term + curvature termpredicts loss 1.768 for a step that gives 1.773derives every optimiser
Loss gradientscanonical pairs give prediction minus targetCE beats MSE-on-sigmoid by 203x at z=−6z = -6output layers, debugging
Hessianmatrix of curvatures; its eigenvalues classify a flat pointx2−y2x^2 - y^2: eigenvalues 2, −2-2: saddlesharpness, step limits
Step limit and ratediverge above 2/λmax⁡2/\lambda_{\max}; best rate (κ−1)/(κ+1)(\kappa-1)/(\kappa+1)κ=10\kappa = 10: 0.818 per step, 35 steps for 1000xlearning rates, warmup
Momentumremember the last step; rate (κ−1)/(κ+1)(\sqrt\kappa-1)/(\sqrt\kappa+1)κ=10\kappa = 10: 0.519, 11 stepsSGD with momentum, Adam's first moment
Newtonjump to the bottom of the local bowl, −H−1g-H^{-1}g7 steps to 10−610^{-6} on real logistic regression; diverges from a bad startsmall models, XGBoost, the ideal others approximate
Quasi-Newtonlearn the curvature from gradient differencesL-BFGS: 21 steps where gradient descent needs 109scikit-learn's default solver
Convexitychords lie above the curve; any minimum is the minimumlog⁡𝔼[X]≥𝔼[log⁡X]\log \mathbb{E}[X] \ge \mathbb{E}[\log X]: 0.916 against 0.693logistic regression, SVMs, ELBO, KL
Lagrange, KKTat a constrained optimum the gradients line upmultiplier = price of loosening the constraintSVMs, weight decay, softmax, TRPO
Stochastic gradientsnoisy but unbiased; noise falls as 1/B1/Bconstant step stalls at a floor proportional to the stepevery minibatch
Deep landscapessaddles, not bad minima; sharpness rises to 2/η2/\etameasured here: 10.02 against 2/η=102/\eta = 10reading training curves

Worked numbers come from the root page's tiny model (input x=(2,1)x = (2, 1), weight rows cat (1,0)(1, 0), dog (0,1)(0, 1), sat (1,−2)(1, -2), scores z=(2,1,0)z = (2, 1, 0), true next word sat, loss 2.408), from small functions you can check by hand, and from two real runs made for this page: logistic regression on a real medical dataset, and a small neural network whose curvature is measured while it trains. All are recomputed by the scripts in this page's source folder.

The Optimisation lab runs eight methods (gradient descent, momentum, Nesterov, Adam, Newton, damped Newton, saddle-free Newton, BFGS) on a quadratic, Rosenbrock's valley, a saddle and a real logistic loss, with the Hessian, its eigenvalues and the local Taylor model at every step; sections 4, 5 and 11 point to it.

1. The local model: derivative, gradient, Taylor expansion

The derivative is the best straight-line fit

Near xx, a smooth function is a straight line plus an error that shrinks faster than the step:

f(x+h)=f(x)+f′(x)h+r(h),r(h)h→0 as h→0f(x + h) = f(x) + f'(x)\,h + r(h), \qquad \frac{r(h)}{h} \to 0 \text{ as } h \to 0

For x2x^2 at 3 the line is 9+6h9 + 6h and the remainder is exactly h2h^2: 1, 0.01, 0.0001, 0.000001 for h=1,0.1,0.01,0.001h = 1, 0.1, 0.01, 0.001. Halve the step and the error quarters. No other straight line has that property, which is why the tangent is called the best linear approximation: any other slope mm leaves an error of about (6−m)h(6 - m)h, which shrinks only as fast as hh itself. Every gradient-based optimiser rests on this reading (for a small enough step the loss changes by slope times step); gradient checking, comparing the analytic slope with (f(x+h)−f(x−h))/2h(f(x+h) - f(x-h))/2h, rests on the limit definition (the root's Gradient lab, panel 1, runs that check on five losses).

Refresher: the limit definition, the secant picture and the six rules
The secant through (3,9)(3, 9) and (3+h,(3+h)2)(3+h, (3+h)^2) (orange) turns into the tangent (blue) as hh shrinks. Its slope is 6+h6 + h, heading for 6; the gap between the tangent line and the curve at 3+h3 + h is exactly h2h^2.

The derivative is the limit of the secant slope as the gap shrinks to zero, f′(x)=limh→0⁡f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} (also written dfdx\frac{df}{dx}; at h=0h = 0 the fraction would be 0/0, so the limit is the value it settles on as hh gets as close to zero as you like). For f(x)=x2f(x) = x^2 at 3: (3+h)2−9h=6+h\frac{(3+h)^2 - 9}{h} = 6 + h; with h=1,0.1,0.01,0.001h = 1, 0.1, 0.01, 0.001 the secant slopes are 7, 6.1, 6.01, 6.001, and the limit is f′(3)=6f'(3) = 6. In general f′(x)=2xf'(x) = 2x.

RuleFormulaWorked case
Constant, power(c)′=0(c)' = 0, (xn)′=nxn−1(x^n)' = n x^{n-1}(x3)′=3x2(x^3)' = 3x^2; at 2: 12
Sum and constant multiple(af+bg)′=af′+bg′(a f + b g)' = a f' + b g'(3x2+5x)′=6x+5(3x^2 + 5x)' = 6x + 5
Product(fg)′=f′g+fg′(fg)' = f'g + fg'(x2ex)′=(2x+x2)ex(x^2 e^x)' = (2x + x^2)e^x; at 1: 3e=8.1553e = 8.155
Quotient(f/g)′=(f′g−fg′)/g2(f/g)' = (f'g - fg')/g^2(11+e−x)′=e−x(1+e−x)2\big(\frac{1}{1 + e^{-x}}\big)' = \frac{e^{-x}}{(1 + e^{-x})^2}
Exponential, log(ex)′=ex(e^x)' = e^x, (ln⁡x)′=1/x(\ln x)' = 1/x(ln⁡(1+x2))′=2x1+x2(\ln(1 + x^2))' = \frac{2x}{1 + x^2}
Chain(f(g(x)))′=f′(g(x))g′(x)(f(g(x)))' = f'(g(x))\,g'(x)(ln⁡(1+ex))′=ex1+ex=σ(x)(\ln(1 + e^x))' = \frac{e^x}{1 + e^x} = \sigma(x)

σ(x)=1/(1+e−x)\sigma(x) = 1/(1 + e^{-x}) is the sigmoid and ln⁡(1+ex)\ln(1 + e^x) the softplus: the last line says softplus's slope is the sigmoid. Every line was checked symbolically (sympy) by the page's script. Why the chain rule multiplies slopes: a step hh moves u=g(x)u = g(x) by about g′(x)hg'(x)\,h, which moves ff by about f′(u)g′(x)hf'(u)\,g'(x)\,h; both approximations become exact in the limit. If uu moves 3 units per unit of xx and ff moves 2 per unit of uu, ff moves 6 per unit of xx.

The sigmoid's slope: σ′(x)=e−x(1+e−x)2=σ(x)(1−σ(x))\sigma'(x) = \frac{e^{-x}}{(1 + e^{-x})^2} = \sigma(x)\,\big(1 - \sigma(x)\big), because e−x1+e−x=1−σ(x)\frac{e^{-x}}{1 + e^{-x}} = 1 - \sigma(x). At x=0x = 0 it is 0.250.25, its largest value; far from zero it falls towards 0, which is why a sigmoid output trained with squared error stalls (section 2). The same steps give tanh′⁡(x)=1−tanh2⁡(x)\tanh'(x) = 1 - \tanh^2(x). Derivatives of functions of vectors and matrices, the two layout conventions and the chain rule as a product of Jacobians: Matrix calculus and backprop (sections 1 to 4).

Add the curvature: the Taylor expansion

The second-order model matches the value, the slope and the bend (the second derivative f′′f'', the slope of the slope):

f(a+h)≈f(a)+f′(a)h+12f′′(a)h2f(a + h) \approx f(a) + f'(a)\,h + \tfrac12 f''(a)\,h^2

The 12\tfrac12 makes the parabola's second derivative equal f′′(a)f''(a). The error of the order-kk model shrinks like hk+1h^{k+1} when ff is smooth enough (Taylor's theorem; Nocedal and Wright, Theorem 2.1, states the multivariate version optimisers use).

The parabola (blue) hugs exe^x (grey) over a much wider range than the tangent (orange). At a=0a = 0, h=0.5h = 0.5: order 1 gives 1.5, order 2 gives 1.625, the truth is 1.6487.

Many inputs: the gradient points up the steepest slope

A partial derivative is the derivative along one input with the others frozen: for f(x,y)=x2+3y2f(x, y) = x^2 + 3y^2, ∂f/∂x=2x\partial f/\partial x = 2x and ∂f/∂y=6y\partial f/\partial y = 6y. The gradient ∇f\nabla f (often gg) stacks them into a vector the shape of the input. Applying the linear approximation in every coordinate at once, the slope along a unit direction uu is

Duf=∇f⋅u=‖∇f‖cos⁡ϕD_u f = \nabla f \cdot u = \lVert \nabla f \rVert \cos \phi

where ϕ\phi is the angle between uu and the gradient. So the gradient is the direction of steepest ascent, its length is that steepest slope, −∇f-\nabla f is steepest descent, and the gradient is perpendicular to the contour. The tiny model: the gradient of its loss in the three scores is g=(0.665,0.245,−0.910)g = (0.665, 0.245, -0.910), length 1.153. Moving the sat score alone has slope −0.910-0.910; the best unit move, −g/‖g‖=(−0.577,−0.212,0.789)-g/\lVert g \rVert = (-0.577, -0.212, 0.789), lowers the loss at rate 1.153, faster than any single score could. "Steepest" depends on how length is measured: with a per-coordinate scale (Adam) or a curvature-weighted norm (Newton, natural gradient) the steepest direction changes, a clean way to see every optimiser (Boyd and Vandenberghe, section 9.4). A vector function has one gradient per output; stacked as rows they form the Jacobian (Matrix calculus and backprop, section 2).

Turn on the spot at (1,1)(1, 1) on f=x2+3y2f = x^2 + 3y^2: the slope you feel is largest (6.325) facing along the gradient (2,6)(2, 6) (blue), zero along the contour, most negative facing away. Facing east it is 2; north-east, (2+6)/2=5.657(2 + 6)/\sqrt2 = 5.657.

The Taylor model in many dimensions

With many inputs the bend becomes the Hessian, Hij=∂2L/∂θi∂θjH_{ij} = \partial^2 L / \partial \theta_i \partial \theta_j, how the slope in direction ii changes as you move in direction jj:

L(θ+δ)≈L(θ)+g⊤δ+12δ⊤HδL(\theta + \delta) \approx L(\theta) + g^\top \delta + \tfrac12\, \delta^\top H \delta

g⊤δg^\top \delta is the slope term; δ⊤Hδ=∑i,jδiHijδj\delta^\top H \delta = \sum_{i,j} \delta_i H_{ij} \delta_j is the curvature felt along the step δ\delta, one number.

The numbers: the tiny model's first step
The root's step with learning rate 0.1 moves the scores by δ=−0.5g=(−0.333,−0.122,0.455)\delta = -0.5\,g = (-0.333, -0.122, 0.455) and the true loss falls from 2.408 to 1.773. The first-order model predicts 2.408−0.5×1.330=1.7422.408 - 0.5 \times 1.330 = 1.742: it overshoots the drop, because the slope flattens as you go. Adding the curvature term with H=diag⁡(p)−pp⊤H = \operatorname{diag}(p) - pp^\top (section 3) predicts 1.768, within 0.005 of the truth. For a nudge of 0.1 to the sat score alone the three values are 2.3166 (order 1), 2.3170 (order 2) and 2.3170 (true): the missing 0.0004 is the curvature.
Every optimiser is a Taylor model plus a trust rule
MethodModel of the lossHow far it trusts it
Gradient descentorder 1: L+g⊤δL + g^\top\deltaa step-size penalty 12η‖δ‖2\frac{1}{2\eta}\lVert\delta\rVert^2 (section 4)
Newtonorder 2 with the true HHfully: jump to the model's minimum (section 5)
Damped Newton, trust regionorder 2only inside a radius, or with H+λIH + \lambda I
Quasi-Newton (BFGS, L-BFGS)order 2 with HH estimated from gradientswith a line search (section 6)
Adam, Adagradorder 1, measured in a per-coordinate scalea step of about η\eta per coordinate
Natural gradient, TRPOorder 1, measured in KL divergencea KL budget (section 9)
Where else ML uses the two terms

The first-order term is behind influence functions and gradient-based saliency; the second-order term is behind sharpness, pruning scores (Optimal Brain Damage) and the learning-rate limit (section 4).

2. The gradient of every standard loss: prediction minus target

Backprop starts from ∂L/∂z\partial L / \partial z, the gradient of the loss in the logit zz (the raw score before any squashing). For every standard pairing of final activation and loss it comes out as prediction minus target, yˆ−y\hat y - y, p−yp - y, p−etp - e_t or ez−ye^z - y; the result, the table of pairings and what it means for training (why squared error on a sigmoid goes silent when the model is most wrong) are owned by Loss functions, and why each loss is a negative log-likelihood by Probability for ML, section 7; this section owns the derivation steps (closed below). The step that makes them all short: in the binary case the slope of the loss in pp, p−yp(1−p)\frac{p - y}{p(1 - p)}, blows up as pp saturates, and the slope of pp in zz is p(1−p)p(1 - p): they cancel exactly.

Two more consequences. Stability: frameworks fuse the softmax into the loss (CrossEntropyLoss and BCEWithLogitsLoss take logits) and compute ln⁡∑jezj=m+ln⁡∑jezj−m\ln \sum_j e^{z_j} = m + \ln \sum_j e^{z_j - m} with m=maxj⁡zjm = \max_j z_j; for logits (1000,1001)(1000, 1001), e1001e^{1001} overflows even in float64, while the shifted form gives 1001.313. Cost: the K×KK \times K softmax Jacobian is never built; p−etp - e_t costs KK operations, with KK the vocabulary for a language model. From here the gradient flows back through the network: Matrix calculus and backprop (sections 4, 5 and 7, including the old page's worked logistic unit, z=0.1z = 0.1, p=0.525p = 0.525, loss 0.644, one step to p=0.595p = 0.595).

Derivations and worked numbers for each loss

Mean squared error and the factor of a half. L=12(yˆ−y)2L = \tfrac12 (\hat y - y)^2 with yˆ=z\hat y = z gives ∂L/∂z=yˆ−y\partial L/\partial z = \hat y - y. The half is a convention. Without it the gradient is 2(yˆ−y)2(\hat y - y); averaged over a batch, L=1n∑i(yˆi−yi)2L = \frac1n \sum_i (\hat y_i - y_i)^2 gives each example 2n(yˆi−yi)\frac{2}{n}(\hat y_i - y_i) (the old page's "2/n2/n", which the root's Gradient lab corrected by stating both conventions). PyTorch's MSELoss has no half; the constant only rescales the learning rate. Example: z=1.5z = 1.5, y=1y = 1: with the half, L=0.125L = 0.125 and gradient 0.5; without it, L=0.25L = 0.25 and gradient 1.0.

Linear regression. With yˆ=Xw\hat y = Xw and the batch-mean loss without the half, ∇wL=2nX⊤(Xw−y)\nabla_w L = \frac{2}{n} X^\top (Xw - y); setting it to zero gives the normal equations X⊤Xw=X⊤yX^\top X w = X^\top y: at the optimum the residual is perpendicular to every column of XX. Worked example (old page, recomputed): points (1,1),(2,3),(3,4)(1, 1), (2, 3), (3, 4) with an intercept column give X⊤X=(36614)X^\top X = \begin{pmatrix} 3 & 6 \\ 6 & 14 \end{pmatrix}, X⊤y=(8,19)X^\top y = (8, 19), determinant 6, so w=(−1/3,3/2)w = (-1/3, 3/2): the line yˆ=−0.333+1.5x\hat y = -0.333 + 1.5x. The residuals (0.167,−0.333,0.167)(0.167, -0.333, 0.167) sum to 0 and 1(0.167)+2(−0.333)+3(0.167)=01(0.167) + 2(-0.333) + 3(0.167) = 0. Solving it stably (QR rather than the normal equations): Linear algebra for ML, section 7.

Binary cross-entropy. L=−[yln⁡p+(1−y)ln⁡(1−p)]L = -[y \ln p + (1 - y) \ln (1 - p)], p=σ(z)p = \sigma(z): ∂L∂z=p−yp(1−p)⋅p(1−p)=p−y\frac{\partial L}{\partial z} = \frac{p - y}{p(1 - p)} \cdot p(1 - p) = p - y. Computed stably on the logit as max⁡(z,0)−yz+ln⁡(1+e−|z|)\max(z, 0) - yz + \ln(1 + e^{-|z|}), which never exponentiates a large positive number. Examples (old page, recomputed): z=−6z = -6, y=1y = 1: p=0.0025p = 0.0025, loss 6.0025, gradient −0.9975-0.9975, nearly the largest push possible. z=2z = 2, y=1y = 1: p=0.8808p = 0.8808, loss 0.1269, gradient −0.1192-0.1192.

Softmax cross-entropy. Write L=−zt+ln⁡∑jezjL = -z_t + \ln \sum_j e^{z_j}: the slope of −zt-z_t in zkz_k is −𝟙[k=t]-\mathbb{1}[k = t], and the slope of the log-sum-exp is pkp_k, so ∂L/∂zk=pk−𝟙[k=t]\partial L/\partial z_k = p_k - \mathbb{1}[k = t]. Going through the softmax Jacobian ∂pk/∂zj=pk(δkj−pj)\partial p_k / \partial z_j = p_k(\delta_{kj} - p_j) (as a matrix diag⁡(p)−pp⊤\operatorname{diag}(p) - pp^\top) gives the same answer. With soft targets qq (label smoothing, distillation) ∇zL=p−q\nabla_z L = p - q because the qkq_k sum to 1. Old page example: z=(2,1,0.1)z = (2, 1, 0.1), class 1 true: ez=(7.389,2.718,1.105)e^z = (7.389, 2.718, 1.105), sum 11.2125, which rounds to 11.213 (the old page printed 11.212), so p=(0.659,0.242,0.099)p = (0.659, 0.242, 0.099), loss 0.417, gradient (−0.341,0.242,0.099)(-0.341, 0.242, 0.099). The components sum to 0, because adding a constant to every logit leaves the softmax unchanged. The tiny model, true word sat: p−esat=(0.665,0.245,−0.910)p - e_{\text{sat}} = (0.665, 0.245, -0.910); with soft target q=(0.9,0.05,0.05)q = (0.9, 0.05, 0.05): p−q=(−0.235,0.195,0.040)p - q = (-0.235, 0.195, 0.040).

Poisson. Rate μ=ez\mu = e^z, loss ez−yz+ln⁡y!e^z - yz + \ln y!, gradient ez−ye^z - y; with y=3y = 3 and z=ln⁡2z = \ln 2, the loss is 1.712 and the gradient −1-1.

3. Critical points and the Hessian: minimum, maximum or saddle

Where the gradient is zero the ground is flat in every direction: a critical point. A bowl, an upside-down bowl, a saddle and a flat-floored trough all share that flatness; only the second derivatives tell them apart.

  1. A bowl, x2+y2x^2 + y^2. H=diag⁡(2,2)H = \operatorname{diag}(2, 2): both eigenvalues positive, so the surface curves up along both arrows (blue; each arrow's length is proportional to its eigenvalue). A local minimum.
  2. A dome, −x2−y2-x^2 - y^2. Both eigenvalues are −2-2: it curves down along both (orange). A local maximum.
  3. A saddle, x2−y2x^2 - y^2. Eigenvalues 2 and −2-2: up along one direction, down along the other. Gradient descent near it leaves along the direction that curves down (the flowing dots), slowly, because the slope is tiny next to a flat point (section 11).
  4. A flat floor, x2+y4x^2 + y^4. H=diag⁡(2,0)H = \operatorname{diag}(2, 0): the zero eigenvalue (grey) says nothing about that direction. Here the y4y^4 term makes it a minimum.
  5. The same Hessian, a saddle: x2−y4x^2 - y^4. At the origin its Hessian is the same diag⁡(2,0)\operatorname{diag}(2, 0), yet it is a saddle. When an eigenvalue is zero the test is inconclusive.

The Hessian is symmetric when the second derivatives are continuous (Schwarz's theorem; f=x2yf = x^2 y has both mixed derivatives equal to 2x2x), so it has real eigenvalues λi\lambda_i and perpendicular eigenvectors uiu_i (the spectral theorem; Linear algebra for ML, sections 8 and 9). Along an eigenvector the Taylor model at a critical point is a plain parabola, L(θ∗+tui)≈L(θ∗)+12λit2L(\theta^* + t\,u_i) \approx L(\theta^*) + \tfrac12 \lambda_i\, t^2: each eigenvalue is the curvature along its direction, and the second-derivative test follows.

Eigenvalues of HH at g=0g = 0NamePointExample, HH at the origin
all >0\gt 0positive definite, H≻0H \succ 0local minimumx2+y2x^2 + y^2: diag(2, 2)
all <0\lt 0negative definite, H≺0H \prec 0local maximum−x2−y2-x^2 - y^2: diag(−2,−2-2, -2)
both signsindefinitesaddlex2−y2x^2 - y^2: diag(2, −2-2)
some zero, rest one signsemidefinitetest inconclusivex2+y4x^2 + y^4: diag(2, 0), a minimum; x2−y4x^2 - y^4, same HH, a saddle

The fraction of negative eigenvalues is the critical point's index (section 11 uses it). For softmax cross-entropy, differentiating p−etp - e_t once more gives

∇z2L=diag⁡(p)−pp⊤\nabla_z^2 L = \operatorname{diag}(p) - p\,p^\top

The label has disappeared: the curvature does not depend on the target. For one sigmoid output it is the number p(1−p)p(1 - p) (the root's Gradient lab, panel 4, shows why it equals the Fisher information). For logistic regression the chain rule through zi=w⊤xiz_i = w^\top x_i gives ∇w2L=1nX⊤SX\nabla_w^2 L = \frac1n X^\top S X with S=diag⁡(pi(1−pi))S = \operatorname{diag}(p_i(1-p_i)).

The numbers
Tiny model, in the scores: at p=(0.665,0.245,0.090)p = (0.665, 0.245, 0.090) the eigenvalues of diag⁡(p)−pp⊤\operatorname{diag}(p) - pp^\top are 0, 0.119 and 0.371 (the zero is the direction (1,1,1)(1, 1, 1): shifting every score equally changes nothing). In the weights each is multiplied by ‖x‖2=5\lVert x \rVert^2 = 5, so λmax⁡=1.855\lambda_{\max} = 1.855. Real logistic regression (569 breast-cancer patients, 30 features plus an intercept, section 6): at the start the Hessian's eigenvalues run from 0.0018 to 3.32, a condition number of 1,855; at the optimum from 0.0018 to 0.150, a condition number of 85.9.
More: the Hessian in the input, and where else ML uses HH

In the input xx (from the sibling page): W⊤(diag⁡(p)−pp⊤)W=(0.1848−0.2289−0.22890.6007)W^\top (\operatorname{diag}(p) - pp^\top) W = \begin{pmatrix} 0.1848 & -0.2289 \\ -0.2289 & 0.6007 \end{pmatrix}, eigenvalues 0.084 and 0.702.

Sharpness (the top eigenvalue) and the learning-rate limit (section 4); classifying where training stalls (section 11); Newton and its approximations (sections 5 and 6); Laplace approximations, influence functions H−1∇LH^{-1}\nabla L, Optimal Brain Damage pruning (score 12Hiiwi2\tfrac12 H_{ii} w_i^2). Computing HvHv without building HH (two backward passes) is on Matrix calculus and backprop, in its closed Depth section.

4. Gradient descent, its step limit 2/λmax⁡2/\lambda_{\max} and its rate (κ−1)/(κ+1)(\kappa - 1)/(\kappa + 1)

Gradient descent is the first-order model plus a step penalty

The plane L(θ)+g⊤δL(\theta) + g^\top \delta has no bottom, so add a penalty for stepping far from where it was measured:

δ∗=arg⁡minδ⁡[L(θ)+g⊤δ+12η‖δ‖2]=−ηg⇒θk+1=θk−η∇L(θk)\delta^* = \arg\min_\delta \Big[ L(\theta) + g^\top \delta + \frac{1}{2\eta} \lVert \delta \rVert^2 \Big] = -\eta\, g \quad\Rightarrow\quad \theta_{k+1} = \theta_k - \eta\, \nabla L(\theta_k)

The learning rate η\eta is how far you trust a straight-line model. Replace ‖δ‖2\lVert \delta \rVert^2 by another distance and you get another optimiser: a per-coordinate scaling gives Adagrad and Adam's shape, δ⊤Hδ\delta^\top H \delta gives Newton, a KL divergence gives natural gradient.

If the curvature never exceeds β\beta in any direction (H⪯βIH \preceq \beta I: the loss is β\beta-smooth), the loss is guaranteed to fall whenever η<2/β\eta \lt 2/\beta, and the guaranteed fall is largest at η=1/β\eta = 1/\beta (the descent lemma). Real networks are not globally smooth in a useful way, so practice measures the local curvature instead (section 11); in the Optimisation lab you can watch the guarantee fail on Rosenbrock's valley when η\eta is too large.

Derivation: the descent lemma

With H⪯βIH \preceq \beta I (βI−H\beta I - H has no negative eigenvalue) the second-order term is at most β2‖δ‖2\frac{\beta}{2}\lVert\delta\rVert^2, so for the step δ=−ηg\delta = -\eta g:

L(θ−ηg)≤L(θ)−η‖g‖2+βη22‖g‖2=L(θ)−η(1−βη2)‖g‖2L(\theta - \eta g) \le L(\theta) - \eta \lVert g \rVert^2 + \frac{\beta \eta^2}{2} \lVert g \rVert^2 = L(\theta) - \eta\Big(1 - \frac{\beta\eta}{2}\Big)\lVert g \rVert^2

The bracket is positive whenever η<2/β\eta \lt 2/\beta. At η=1/β\eta = 1/\beta the guaranteed fall is ‖g‖2/(2β)\lVert g \rVert^2 / (2\beta) (Boyd and Vandenberghe, equation 9.17). Example: f=x2+3y2f = x^2 + 3y^2 has β=6\beta = 6; from (1,1)(1, 1), where ‖g‖2=40\lVert g \rVert^2 = 40, the step η=1/6\eta = 1/6 must lower ff by at least 40/12=3.3340/12 = 3.33 (it lowers it from 4 to 0.444, a fall of 3.56).

On a quadratic every eigen-direction is its own one-dimensional problem

Near a minimum every smooth loss looks like L(θ)=12θ⊤HθL(\theta) = \tfrac12 \theta^\top H \theta, whose gradient is HθH\theta, so one step is θk+1=(I−ηH)θk\theta_{k+1} = (I - \eta H)\,\theta_k. Along each eigenvector of HH this multiplies the coordinate by the single number 1−ηλi1 - \eta\lambda_i; the directions do not interact. A coordinate shrinks only if |1−ηλi|<1|1 - \eta\lambda_i| \lt 1 (with ηλi\eta\lambda_i between 1 and 2 it flips sign each step but shrinks; above 2 it flips and grows), and the steepest direction is the hardest, so

η<2λmax⁡,η∗=2λmax⁡+λmin⁡,r(η∗)=κ−1κ+1,κ=λmax⁡λmin⁡\eta \lt \frac{2}{\lambda_{\max}}, \qquad \eta^* = \frac{2}{\lambda_{\max} + \lambda_{\min}}, \qquad r(\eta^*) = \frac{\kappa - 1}{\kappa + 1}, \quad \kappa = \frac{\lambda_{\max}}{\lambda_{\min}}
  1. A stretched bowl. L=12(x2+10y2)L = \tfrac12(x^2 + 10y^2), so H=diag⁡(1,10)H = \operatorname{diag}(1, 10): the flat direction curves by 1, the steep one by 10, κ=10\kappa = 10. Every run starts at (1,1)(1, 1) and stops when both coordinates are under 10−310^{-3}, a thousandfold cut.
  2. The safe step, η=1/λmax⁡=0.1\eta = 1/\lambda_{\max} = 0.1. The steep coordinate is multiplied by 1−0.1×10=01 - 0.1 \times 10 = 0 and is gone in one step; the flat one only by 0.9 per step, so the walk down the valley floor takes 66 steps.
  3. The best fixed step, η∗=2/11=0.182\eta^* = 2/11 = 0.182. Both factors now have size 9/11=0.8189/11 = 0.818; the steep one is negative, so that coordinate flips sign every step and the path zig-zags across the valley while it shrinks: 35 steps.
  4. Just over the limit, η=0.21>2/λmax⁡=0.2\eta = 0.21 \gt 2/\lambda_{\max} = 0.2 (an illustrative step). The steep factor is 1−2.1=−1.11 - 2.1 = -1.1: the zig-zag grows by a tenth each step and the iterate climbs out of the bowl, while the flat coordinate still shrinks.
  5. Momentum. Heavy ball with the best settings for this bowl (step 0.231, momentum 0.270; below) builds speed along the floor and damps the zig-zag: 16 steps. Its rate 0.519 is approached only slowly at first, so it takes longer than the 11 steps the rate alone gives, still about half of the best plain step's 35.
The numbers (old page and root, recomputed)
H=diag⁡(1,10)H = \operatorname{diag}(1, 10), κ=10\kappa = 10: divergence above η=0.2\eta = 0.2; best step 2/11=0.1822/11 = 0.182, contraction 9/11=0.8189/11 = 0.818 per step, so a thousandfold cut takes ln⁡1000/ln⁡(11/9)=34.4\ln 1000 / \ln(11/9) = 34.4, that is 35 steps (the script runs them: from (1,1)(1, 1) the largest coordinate after 35 steps is 0.00089). With the safe textbook step η=1/λmax⁡=0.1\eta = 1/\lambda_{\max} = 0.1 the factor is 1−1/κ=0.91 - 1/\kappa = 0.9 and it takes 66 steps. The tiny model at its start has λmax⁡=1.855\lambda_{\max} = 1.855 in the weights, so its local limit is η≈1.08\eta \approx 1.08 (the root's step with η=1\eta = 1 was just inside it).

The rate is the worse of the two ends because |1−ηλ||1 - \eta\lambda| is V-shaped in λ\lambda; setting 1−ηλmin⁡=ηλmax⁡−11 - \eta\lambda_{\min} = \eta\lambda_{\max} - 1 gives η∗\eta^*. This is the rate Linear algebra for ML (section 11) quotes, and its correction stands: the rate holds only at this best fixed step; at any other step it is slower.

Checked on real data
Logistic regression on the breast-cancer data (section 6) has, at its optimum, λmax⁡=0.150\lambda_{\max} = 0.150, λmin⁡=0.00175\lambda_{\min} = 0.00175, κ=85.9\kappa = 85.9. The theory predicts that gradient descent with η∗=13.1\eta^* = 13.1 contracts the error by (κ−1)/(κ+1)=0.977(\kappa - 1)/(\kappa + 1) = 0.977 per step near the optimum. Measured over the iterations where the loss gap falls from 10−810^{-8} to 10−1210^{-12}: 0.974 per step, within the drift of the Hessian along the path. The same step is far above the limit at the start, where λmax⁡=3.32\lambda_{\max} = 3.32 gives 2/λmax⁡=0.602/\lambda_{\max} = 0.60; it survives only because the logistic loss's gradient is bounded, which is exactly why practice uses warmup or a line search.

What it means in training: the largest usable learning rate is set by the sharpest direction and the speed by the flattest. That is why normalisation layers and good initialisation, which make the bowl rounder, speed training (Normalisation and initialisation), and why learning-rate warmup exists (curvature is often high early). The root's Gradient lab (panel 3) animates the bowl; for a strongly convex, smooth function (μI⪯H⪯βI\mu I \preceq H \preceq \beta I) the same reasoning bounds the loss gap: at η=1/β\eta = 1/\beta it shrinks by at least 1−μ/β=1−1/κ1 - \mu/\beta = 1 - 1/\kappa per step (Boyd and Vandenberghe, equation 9.18).

The flat direction wants a big step, the steep one a small step; the best step balances them
Predict first: at κ=100\kappa = 100, how much slower is the best rate than at κ=10\kappa = 10? Then drag. Orange: the flat direction's factor |1−ηλmin⁡||1 - \eta\lambda_{\min}|; blue: the steep one's |1−ηλmax⁡||1 - \eta\lambda_{\max}|; the rate is the higher of the two (thick), lowest where they cross (green, η∗\eta^*). The step axis is a fraction of the limit 2/λmax⁡2/\lambda_{\max}; moving κ\kappa resets the step to η∗\eta^*.

Momentum: the condition number enters through its square root

Heavy-ball momentum (Polyak 1964) keeps a running velocity: steps that agree from one iteration to the next (along the valley floor) add up, to about 1/(1−β)1/(1 - \beta) times a single step, and steps that flip sign (across the walls) cancel. On a quadratic with the best settings the rate becomes κ−1κ+1\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1} (Goh 2017, "Why Momentum Really Works"). On diag(1, 10) that is 0.519 per step, about 11 steps for a thousandfold cut against plain gradient descent's 35; for κ=100\kappa = 100, 0.818 against 0.980, about 34 steps against 345. Nesterov's accelerated gradient evaluates the gradient where momentum is about to carry you, and on smooth convex functions (not just quadratics) provably reaches a loss gap of order 1/k21/k^2 after kk steps, against 1/k1/k for gradient descent; no method using only gradients can do better in the worst case. How momentum, Nesterov and Adam behave on real networks: Optimisers and learning-rate schedulers (sections 2 to 6, and its Optimiser race tab with thirteen update rules).

Momentum and Nesterov: the update rules, the best settings and a real-data check

In Goh's form: zk+1=βzk+∇L(θk)z_{k+1} = \beta z_k + \nabla L(\theta_k), θk+1=θk−αzk+1\theta_{k+1} = \theta_k - \alpha\, z_{k+1}, with step α\alpha and momentum β∈[0,1)\beta \in [0, 1). On a quadratic each eigen-coordinate carries position and velocity, updated by a fixed 2x2 matrix whose eigenvalues set the rate. Goh's analysis (section "The Dynamics of Momentum") gives three results:

  • Stability: 0<αλi<2+2β0 \lt \alpha\lambda_i \lt 2 + 2\beta. With β=0.9\beta = 0.9 the step can be almost twice as large as plain gradient descent's limit: on diag(1, 10), up to 0.38 rather than 0.2.
  • Best settings: α=(2λmin⁡+λmax⁡)2\alpha = \Big(\frac{2}{\sqrt{\lambda_{\min}} + \sqrt{\lambda_{\max}}}\Big)^2, β=(λmax⁡−λmin⁡λmax⁡+λmin⁡)2\beta = \Big(\frac{\sqrt{\lambda_{\max}} - \sqrt{\lambda_{\min}}}{\sqrt{\lambda_{\max}} + \sqrt{\lambda_{\min}}}\Big)^2.
  • Best rate: κ−1κ+1\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}.

diag(1, 10): α=(2/(1+3.162))2=0.231\alpha = (2/(1 + 3.162))^2 = 0.231, β=(2.162/4.162)2=0.270\beta = (2.162/4.162)^2 = 0.270, rate (3.162−1)/(3.162+1)=0.519(3.162 - 1)/(3.162 + 1) = 0.519, so a thousandfold reduction takes ln⁡1000/ln⁡(1/0.519)=10.5\ln 1000/\ln(1/0.519) = 10.5 steps. Running those settings in the page's script measures 0.524 per step (the rate is approached, with a polynomial factor, because the 2x2 matrices at the optimum have repeated eigenvalues).

Nesterov (Nesterov 1983, in the form of Sutskever et al. 2013): vk+1=μvk−η∇L(θk+μvk)v_{k+1} = \mu v_k - \eta \nabla L(\theta_k + \mu v_k), θk+1=θk+vk+1\theta_{k+1} = \theta_k + v_{k+1}. The look-ahead corrects an overshoot one step earlier. The 1/k21/k^2 bound and its optimality are in Nesterov, Introductory Lectures on Convex Optimization, 2004, sections 2.1 and 2.2; heavy ball has no such guarantee beyond quadratics.

On real data: on the breast-cancer logistic regression, Nesterov with the textbook constants (η=1/β\eta = 1/\beta with the global smoothness bound β=3.32\beta = 3.32, momentum (κ−1)/(κ+1)=0.955(\sqrt{\kappa} - 1)/(\sqrt{\kappa} + 1) = 0.955) reaches a loss gap of 10−610^{-6} in 274 iterations, where plain gradient descent at the same step does not get there in 500 (section 6 has the full race). Momentum's 2x2 analysis drawn interactively: Goh 2017; momentum against gradient descent on a bowl is the root's Gradient lab, panel 3, and this page's Optimisation lab.

Same loss, same start: Newton races in from w = 0 and flies off from w = 8

5. Newton's method: jump to the bottom of the local bowl

Minimise the second-order model m(δ)=L+g⊤δ+12δ⊤Hδm(\delta) = L + g^\top \delta + \tfrac12 \delta^\top H \delta: its gradient g+Hδg + H\delta is zero at

Hδ=−g⇒δNewton=−H−1gH \delta = -g \quad\Rightarrow\quad \delta_{\text{Newton}} = -H^{-1} g

This is a minimum of the model only if H≻0H \succ 0. In one dimension it is −L′/L′′-L'/L'', slope over curvature. In eigen-coordinates it divides each direction's gradient by that direction's curvature, so flat directions get big steps and steep ones small steps at once: the condition number no longer matters.

The before and after: one gradient step against one Newton step

Both methods on the same real function: the logistic-regression loss of one weight ww on one breast-cancer feature (the "worst concave points" measurement, standardised; label 1 is malignant), with the small L2 penalty of section 6. At each point gradient descent trusts the tangent line under a step penalty with η=1/β=3.97\eta = 1/\beta = 3.97, from the largest curvature this function can have, β=0.252\beta = 0.252 (its model is a parabola of curvature β\beta); Newton trusts the parabola with the true curvature. Predict before step 5: from w=8w = 8, where the loss is almost flat, where does Newton go?

  1. Start at w=0w = 0: the two models agree. Loss 0.693, slope −0.384-0.384, curvature 0.252, which here equals the bound β\beta. So gradient descent's parabola (orange) and Newton's (blue, dashed over it) are the same, and both land at 1.524.
  2. Second step: the curvature has fallen. At 1.524 the true curvature is below β\beta, so Newton's parabola is wider and its bottom further on: Newton lands at 2.539, gradient descent, still using β\beta, at 1.934.
  3. Newton converges. 3.262, 3.526, 3.551: the error is roughly squared each step. In the same steps gradient descent reaches 2.204, 2.404, and so on.
  4. Gradient descent crawls. At the optimum w∗=3.551w^* = 3.551 the curvature is only 0.0228, so each step removes about 0.0228/0.2520.0228/0.252 of the remaining distance (it contracts by 1−0.0228/0.252=0.911 - 0.0228/0.252 = 0.91): after 30 steps it is still at 3.483.
  5. Start at w=8w = 8: the bowl lies. The curvature there is only 0.0039, so Newton's parabola is nearly flat and its bottom far away: Newton jumps to −2.58-2.58. Gradient descent moves safely to 7.84.
  6. Newton diverges. Then 14.2, −13.3-13.3, 366: each jump lands where the parabola is a worse model of the loss. Gradient descent creeps downhill (7.84, 7.68, ...).

Fast near the answer, dangerous far from it

  • Quadratic convergence. Near a minimum with H≻0H \succ 0 and a smooth Hessian the error is roughly squared each step: the correct digits double (Boyd and Vandenberghe, section 9.5.3). On the 31-parameter real logistic regression the loss gap after Newton steps 5 to 8 is 1.6×10−31.6 \times 10^{-3}, 6.1×10−56.1 \times 10^{-5}, 1.3×10−71.3 \times 10^{-7}, 6.4×10−136.4 \times 10^{-13}.
  • Affine invariance. Rescaling or rotating the parameters (θ=Aϕ\theta = A\phi) changes Newton's steps in exactly the matching way, so units do not matter; gradient descent's steps change shape (section 9.5.1).
  • Far from the minimum the model lies (the w=8w = 8 start). The fix is damping: a backtracking line search on the Newton direction, a trust region (minimise the model only within a radius), or Levenberg-Marquardt (H+λI)−1g(H + \lambda I)^{-1} g, which slides between Newton (λ=0\lambda = 0) and a short gradient step (large λ\lambda).
  • Saddles attract it. With a negative eigenvalue, −H−1g-H^{-1}g moves uphill along that direction, towards the saddle: "the saddle point θ∗\theta^* becomes an attractor for the Newton method" (Dauphin et al., 2014, section 3). Their saddle-free Newton divides by |λi||\lambda_i| instead (the Optimisation lab has it).

Why it is too expensive at scale

For nn parameters, HH has n2n^2 entries and solving Hδ=−gH\delta = -g costs about n3n^3 operations. At n=109n = 10^9: 101810^{18} entries, 2 exabytes in 16-bit numbers, before any solve. Minibatch noise makes the curvature estimate itself noisy, and the many saddles make an undamped step unsafe. So practice uses stand-ins that need only gradients or Hessian-vector products: Gauss-Newton and Fisher matrices, Hessian-free conjugate gradient, K-FAC, Shampoo, SOAP and Muon, diagonal scalings such as Adam, and quasi-Newton (next section).

The approximations to Newton, one line each
  • Gauss-Newton and the Fisher matrix: positive semidefinite stand-ins for HH (for the canonical losses of section 2, the Fisher equals the Gauss-Newton part of the Hessian; the root's Gradient lab, panel 4, and Martens 2020, section 9). Natural gradient preconditions by the inverse Fisher.
  • Hessian-free (Martens 2010): solve Hδ=−gH\delta = -g approximately by conjugate gradient, using only Hessian-vector products.
  • K-FAC (Martens and Grosse 2015): approximate each layer's block of the Fisher by a Kronecker product of two small matrices.
  • Shampoo, SOAP, Muon: per-layer matrix preconditioners in practice; owned by Optimisers and learning-rate schedulers (section 4).
  • Diagonal scalings: Adam divides by the square root of an exponential moving average of squared gradients; its authors relate that average to the diagonal of the Fisher (Kingma and Ba 2014, section 5). It is a crude per-coordinate scale, not a Newton step (no inverse curvature, and a square root).

6. Quasi-Newton: learn the curvature from gradients, and a race on real data

After a step sk=θk+1−θks_k = \theta_{k+1} - \theta_k the gradient changes by yk=gk+1−gky_k = g_{k+1} - g_k. For a quadratic yk=Hsky_k = H s_k exactly, so ask the curvature estimate to satisfy the secant equation Bk+1sk=ykB_{k+1} s_k = y_k. BFGS (Broyden, Fletcher, Goldfarb, Shanno, 1970) picks the solution closest to the previous estimate and keeps its inverse directly; the step is −Mkgk-M_k g_k followed by a line search. It still stores n2n^2 numbers. L-BFGS (Liu and Nocedal 1989) keeps only the last mm pairs (sk,yk)(s_k, y_k), typically 5 to 20, and applies the inverse to the gradient with a "two-loop recursion" of 4mn4mn multiplications: memory 2mn2mn numbers instead of n2n^2. It is scikit-learn's default solver for logistic regression. It needs full-batch, deterministic gradients: with minibatch noise yky_k is dominated by sampling noise rather than curvature and the estimate degrades.

Newton, with the exact curvature, needs 7 iterations, each solving a 31x31 system (cheap here, impossible at 10910^9). L-BFGS learns enough curvature from 10 gradient pairs to need 21 iterations and 23 gradient evaluations. First-order methods pay for the condition number (1,855 at the start, 85.9 at the optimum): plain gradient descent with the safe global step is still 2×10−32 \times 10^{-3} from the optimum after 500 iterations. Adam does well here, but at a learning rate of 1.0 that no default would choose.

The BFGS update and the curvature condition
Mk+1=(I−ρkskyk⊤)Mk(I−ρkyksk⊤)+ρksksk⊤,ρk=1yk⊤skM_{k+1} = (I - \rho_k s_k y_k^\top)\, M_k\, (I - \rho_k y_k s_k^\top) + \rho_k s_k s_k^\top, \qquad \rho_k = \frac{1}{y_k^\top s_k}

Mk=Bk−1M_k = B_k^{-1}. The update needs yk⊤sk>0y_k^\top s_k \gt 0 (the curvature condition; the Wolfe line-search conditions guarantee it), which keeps MM positive definite (Nocedal and Wright, Numerical Optimization, 2nd edition, chapter 6). Each update is a rank-two correction costing n2n^2. The L-BFGS two-loop recursion is in chapter 7.

The ranking follows how much curvature each method uses
L2-regularised logistic regression on the breast-cancer data bundled with scikit-learn (569 patients, 30 standardised features plus an unpenalised intercept, penalty λ=1/569\lambda = 1/569, the same problem as scikit-learn's LogisticRegression(C=1), whose answer the page's script matches to 1.1×10−61.1 \times 10^{-6}). Vertical axis: loss minus the optimum, log scale. Every method starts at w=0w = 0. Settings: gradient descent at η=1/β\eta = 1/\beta with the global bound β=3.32\beta = 3.32; gradient descent with a backtracking (Armijo) line search; Nesterov with η=1/β\eta = 1/\beta and momentum 0.955; Adam at its best learning rate from a grid of 0.001 to 3 (it was 1.0); L-BFGS with m=10m = 10 (SciPy); Newton with the exact Hessian.
MethodIterations to a gap of 10−610^{-6}Cost per iteration

7. XGBoost's leaf weights are Newton steps

XGBoost adds one regression tree per round to the current prediction and takes a second-order Taylor expansion of each example's loss around it (Chen and Guestrin 2016, equation 3), with per-example slope gig_i and curvature hih_i, plus a penalty γT+12λ‖w‖2\gamma T + \tfrac12 \lambda \lVert w \rVert^2 on a tree of TT leaves with values wjw_j. Every example in leaf jj gets the same value, so with Gj=∑i∈IjgiG_j = \sum_{i \in I_j} g_i and Hj=∑i∈IjhiH_j = \sum_{i \in I_j} h_i each leaf's objective is a parabola Gjwj+12(Hj+λ)wj2G_j w_j + \tfrac12 (H_j + \lambda) w_j^2, minimised at

wj∗=−GjHj+λ,Gain=12[GL2HL+λ+GR2HR+λ−(GL+GR)2HL+HR+λ]−γw_j^* = -\frac{G_j}{H_j + \lambda}, \qquad \text{Gain} = \frac12 \Big[ \frac{G_L^2}{H_L + \lambda} + \frac{G_R^2}{H_R + \lambda} - \frac{(G_L + G_R)^2}{H_L + H_R + \lambda} \Big] - \gamma

A Newton step −G/H-G/H for the leaf, damped by λ\lambda; a split is worth the drop in the objective (their equations 5 to 7). That is why XGBoost needs losses with a second derivative, and why its custom-objective API asks for both the gradient and the Hessian of each example.

Derivation of the leaf weight and the gain

The round-tt objective (their equation 2 and 3) is ℒ~(t)=∑i[gift(xi)+12hift(xi)2]+Ω(ft)\tilde{\mathcal L}^{(t)} = \sum_{i} [ g_i f_t(x_i) + \tfrac12 h_i f_t(x_i)^2 ] + \Omega(f_t), with gi=∂l/∂yˆg_i = \partial l/\partial \hat y and hi=∂2l/∂yˆ2h_i = \partial^2 l/\partial \hat y^2 at the current prediction yˆi(t−1)\hat y_i^{(t-1)}. Grouped by leaves it is ∑j[Gjwj+12(Hj+λ)wj2]+γT\sum_{j} [ G_j w_j + \tfrac12 (H_j + \lambda) w_j^2 ] + \gamma T; each bracket's slope Gj+(Hj+λ)wjG_j + (H_j + \lambda) w_j is zero at wj∗w_j^*, and substituting back gives ℒ~∗=−12∑jGj2Hj+λ+γT\tilde{\mathcal L}^* = -\frac12 \sum_j \frac{G_j^2}{H_j + \lambda} + \gamma T. The gain is the parent's value minus the two children's. Worked: GL=−1.5G_L = -1.5, HL=0.75H_L = 0.75, GR=0.5G_R = 0.5, HR=0.25H_R = 0.25, Gain =12[2.25/1.75+0.25/1.25−1/2]−γ= \tfrac12[2.25/1.75 + 0.25/1.25 - 1/2] - \gamma. Boosting in context, and whether boosted trees still win on tables: Classical ML (sections 3 and 4).

Logistic loss, current prediction 0 (so p=0.5p = 0.5, g=p−yg = p - y, h=p(1−p)=0.25h = p(1-p) = 0.25 for everyone), λ=1\lambda = 1. Click a label to flip it; click "split here" positions to move the split.
Labels 1, 1, 1, 0 (old page, recomputed): one leaf has G=−1G = -1, H=1H = 1, w∗=1/(1+1)=0.5w^* = 1/(1 + 1) = 0.5, half a unit of log-odds towards the majority (the undamped Newton step would be 1.0). Splitting the three positives from the negative: Gain =0.493−γ= 0.493 - \gamma; the children get wL=0.857w_L = 0.857 and wR=−0.4w_R = -0.4.

8. Convexity: when every minimum is the minimum

A function is convex if the chord between any two points on its graph never dips below the graph: a bowl, not a wave.

  1. Chord (the definition): f(tx+(1−t)y)≤tf(x)+(1−t)f(y)f(t x + (1-t) y) \le t f(x) + (1-t) f(y) for all x,yx, y and t∈[0,1]t \in [0, 1]. For x2x^2, x=−1x = -1, y=3y = 3, t=0.5t = 0.5: f(1)=1≤12(1+9)=5f(1) = 1 \le \tfrac12(1 + 9) = 5.
  2. Tangent (if differentiable): f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top (y - x): the function lies above every tangent plane. So if ∇f(x)=0\nabla f(x) = 0, then f(y)≥f(x)f(y) \ge f(x) for every yy: a flat point is a global minimum (Boyd and Vandenberghe, section 3.1.3).
  3. Curvature (if twice differentiable): H(x)⪰0H(x) \succeq 0 everywhere (section 3.1.4). This is the practical test; strict convexity (H≻0H \succ 0) makes the minimiser unique.
ProblemConvex in its parameters?Why
Least squares, ridgeyesHessian 2nX⊤X⪰0\frac{2}{n} X^\top X \succeq 0 (plus 2λI2\lambda I)
Logistic and softmax regressionyeslog-sum-exp minus a linear term, of an affine map of ww; Hessian 1nX⊤SX⪰0\frac1n X^\top S X \succeq 0
Support vector machineyeshinge max⁡(0,1−yw⊤x)\max(0, 1 - y\, w^\top x) is a maximum of affine functions, plus ‖w‖2\lVert w \rVert^2
Lassoyes (not smooth)‖w‖1\lVert w \rVert_1 is a sum of |wi||w_i|
Cross-entropy in the logitsyesHessian diag⁡(p)−pp⊤⪰0\operatorname{diag}(p) - pp^\top \succeq 0 (section 3)
Any network with a hidden layer, in its weightsnothe permutation argument below
Between a=−1a = -1 and b=3b = 3: green dot, the function at the mixed point f(ta+(1−t)b)f(ta + (1-t)b); orange dot, the chord at the same place, tf(a)+(1−t)f(b)t f(a) + (1-t) f(b). For a convex function green is never above orange. Pick the last function and find a tt where the chord passes below the graph.

Convex against non-convex, in two weights

  1. A convex loss: the chord lies above. The real two-weight logistic loss of Figure 1. Between two weight vectors the straight chord (orange) stays above the loss along the segment (green): at the midpoint 0.582 against 0.290.
  2. Tangent planes lie below; flat means global. Every tangent plane (orange glass) stays under the surface, so where the tangent plane is flat (green glass, at the optimum) nothing anywhere is lower.
  3. A non-convex loss: the chord dips below. The lab's x2+y4/4−y2/2x^2 + y^4/4 - y^2/2 (illustrative) has two minima of value −0.25-0.25 at (0,±1)(0, \pm1); the chord between them is at −0.25-0.25, but the surface at the midpoint is 0, above it.
  4. Why networks look like this. Read the two minima as one network and the same network with two hidden units swapped: equal loss. Their average is the saddle between them, worse; gradient descent from there runs to either copy (the flowing dots).

Why deep networks are not convex. Swap two hidden units of a layer (their incoming and outgoing weights together): the network computes exactly the same function, so the loss is the same at two different points. If the loss were convex the midpoint would be at least as good, but the midpoint averages the two units into identical copies, usually a worse network. So there are many separate minima of equal value, and convexity in the logits says nothing about earlier layers' weights. What convexity buys: no bad local minima, a certificate of optimality (∇f=0\nabla f = 0, or the KKT conditions with constraints), and rates that depend only on the condition number: with μI⪯H⪯βI\mu I \preceq H \preceq \beta I, gradient descent converges linearly at 1−μ/β1 - \mu/\beta per step (section 4), Nesterov at about 1−μ/β1 - \sqrt{\mu/\beta}, and Newton quadratically near the end.

Jensen's inequality

The chord definition over a whole distribution is Jensen's inequality (Boyd and Vandenberghe, section 3.1.8): for convex ff, f(𝔼[X])≤𝔼[f(X)]f(\mathbb{E}[X]) \le \mathbb{E}[f(X)], and for a concave ff such as ln⁡\ln it reverses. Example: XX is 1 or 4 with equal chance: ln⁡𝔼[X]=ln⁡2.5=0.916\ln \mathbb{E}[X] = \ln 2.5 = 0.916 against 𝔼[ln⁡X]=12(0+1.386)=0.693\mathbb{E}[\ln X] = \tfrac12(0 + 1.386) = 0.693. It is why KL divergence is never negative (Information theory for ML):

−KL(p‖q)=𝔼p[ln⁡(q/p)]≤ln⁡𝔼p[q/p]=ln⁡∑xq(x)=0-\mathrm{KL}(p \Vert q) = \mathbb{E}_p[\ln(q/p)] \le \ln \mathbb{E}_p[q/p] = \ln \sum_x q(x) = 0

and it gives the evidence lower bound (ELBO) of VAEs and diffusion models from ln⁡𝔼q[p(x,z)/q(z)]\ln \mathbb{E}_q[p(x, z)/q(z)] (Variational inference and generative-model maths); EM is coordinate ascent on that bound.

Building convex functions

Boyd and Vandenberghe, section 3.2: non-negative weighted sums of convex functions are convex; a convex function of an affine map Ax+bAx + b is convex; the pointwise maximum of convex functions is convex; ln⁡∑jezj\ln \sum_j e^{z_j} (log-sum-exp) is convex. These rules give every "yes" row of the table.

9. Constraints: Lagrange multipliers, KKT, and where ML uses them

Sometimes the parameters must satisfy a condition: probabilities sum to 1, a weight vector stays inside a ball, a policy stays close to the old one. Walk along the constraint curve g(x)=0g(x) = 0 (here gg is a constraint function, not a gradient): where ff stops changing, every direction you may move is perpendicular to ∇f\nabla f, and the curve's own normal is ∇g\nabla g, so at the optimum the two gradients are parallel.

∇f(x∗)=λ∇g(x∗),ℒ(x,λ)=f(x)−λg(x)\nabla f(x^*) = \lambda\, \nabla g(x^*), \qquad \mathcal{L}(x, \lambda) = f(x) - \lambda\, g(x)

λ\lambda is the Lagrange multiplier and ℒ\mathcal L the Lagrangian: its gradient in xx gives the parallel condition and its derivative in λ\lambda gives back the constraint, so one unconstrained system replaces the constrained problem. The multiplier is a price: with the constraint g(x)=cg(x) = c, λ=df∗/dc\lambda = df^*/dc, how much the best value improves per unit the constraint is loosened. In the figure, (1,1)=λ(2x,2y)(1, 1) = \lambda(2x, 2y) and the constraint give x=y=0.707x = y = 0.707, λ=0.707\lambda = 0.707, f∗=1.414f^* = 1.414; on the circle x2+y2=cx^2 + y^2 = c the optimum is f∗=2cf^* = \sqrt{2c}, whose derivative 1/2c1/\sqrt{2c} is 0.707 at c=1c = 1, equal to λ\lambda.

  1. The objective is a tilted plane, the constraint a circle. Maximise f=x+yf = x + y (the plane; its contour lines are drawn on the floor) on the unit circle x2+y2=1x^2 + y^2 = 1 (orange, dashed on the floor and lifted onto the plane). Only points on the lifted curve are allowed.
  2. Walk round the circle. From 10° towards 45° the value climbs. At the floor point, ∇f=(1,1)\nabla f = (1, 1) is the same everywhere (blue) and ∇g=(2x,2y)\nabla g = (2x, 2y) points straight out of the circle (orange); the angle between them closes.
  3. At the top the gradients are parallel. At 45°, x=y=0.707x = y = 0.707: (1,1)=λ(1.414,1.414)(1, 1) = \lambda (1.414, 1.414) with λ=0.707\lambda = 0.707, and f∗=2=1.414f^* = \sqrt2 = 1.414, the highest point of the curve.
  4. The multiplier is a price. Loosen the constraint to x2+y2=cx^2 + y^2 = c: the circle grows and the top rises as f∗=2cf^* = \sqrt{2c}, to 2 at c=2c = 2. Its slope 1/2c1/\sqrt{2c} at c=1c = 1 is 0.707, the multiplier.

Inequality constraints: the KKT conditions

To minimise f(x)f(x) subject to hi(x)≤0h_i(x) \le 0, the Karush-Kuhn-Tucker conditions at a solution with multipliers μi\mu_i are (Boyd and Vandenberghe, section 5.5.3): stationarity ∇f(x∗)+∑iμi∇hi(x∗)=0\nabla f(x^*) + \sum_i \mu_i \nabla h_i(x^*) = 0; primal feasibility hi(x∗)≤0h_i(x^*) \le 0; dual feasibility μi≥0\mu_i \ge 0 (a constraint can only push, not pull); complementary slackness μihi(x∗)=0\mu_i h_i(x^*) = 0: either the constraint is tight or its multiplier is zero. For a convex problem with a strictly feasible point (Slater's condition) they are necessary and sufficient. Example: minimise (x−2)2(x - 2)^2 subject to x≤1x \le 1: the unconstrained answer 2 is outside, so the constraint binds, x∗=1x^* = 1, and 2(x∗−2)+μ=02(x^* - 2) + \mu = 0 gives μ=2≥0\mu = 2 \ge 0. With x≤3x \le 3 instead, the constraint is slack and μ=0\mu = 0.

Where the multipliers show up in models you train

  • Weight decay is a constraint in disguise. Minimising the loss subject to ‖w‖2≤t\lVert w \rVert^2 \le t has, by KKT, the same solution as minimising the loss plus μ‖w‖2\mu \lVert w \rVert^2 for a matching μ≥0\mu \ge 0: the decay strength is the multiplier of a norm ball (a smaller ball, a larger multiplier). Practice: Regularisation.
  • Support vectors are complementary slackness. The SVM minimises 12‖w‖2\tfrac12\lVert w \rVert^2 subject to yi(w⊤xi+b)≥1y_i(w^\top x_i + b) \ge 1; each example's multiplier αi\alpha_i is zero unless it sits on the margin (or violates it, soft margin), so w=∑iαiyixiw = \sum_i \alpha_i y_i x_i depends only on those examples (Classical ML, section 5).
  • Softmax is the maximum-entropy distribution with a fixed average score: pk=eβsk/∑jeβsjp_k = e^{\beta s_k}/\sum_j e^{\beta s_j}, the inverse temperature β\beta being the multiplier of the average-score constraint (Jaynes 1957). With the tiny model's scores (2,1,0)(2, 1, 0) and β=1\beta = 1 it is the root's (0.665,0.245,0.090)(0.665, 0.245, 0.090).
  • TRPO's KL-constrained step is the natural gradient. Maximise the surrogate's first-order model g⊤Δg^\top \Delta subject to the second-order KL 12Δ⊤FΔ≤δ\tfrac12 \Delta^\top F \Delta \le \delta (FF, the Hessian of the KL, is the Fisher information): the Lagrangian is stationary at Δ∝F−1g\Delta \propto F^{-1} g, the natural gradient direction, with the length fixed by the budget (Schulman et al. 2015, appendix C).
Δ=2δg⊤F−1gF−1g\Delta = \sqrt{\frac{2\delta}{g^\top F^{-1} g}}\; F^{-1} g
The numbers: one Bernoulli policy
A policy that takes an action with probability p=σ(θ)p = \sigma(\theta) has Fisher information F=p(1−p)F = p(1 - p) in the logit (the root's Bernoulli curvature). With budget δ=0.01\delta = 0.01 the allowed logit step is 2δ/F\sqrt{2\delta/F}: at p=0.5p = 0.5, F=0.25F = 0.25, step 0.283 (to p=0.570p = 0.570, true KL 0.0100); at p=0.9p = 0.9, F=0.09F = 0.09, step 0.471 (to p=0.935p = 0.935, true KL 0.0088). The same budget allows a bigger parameter step where the distribution is less sensitive: the whole point of measuring steps in KL rather than in parameters.
Derivations: softmax as maximum entropy, TRPO's step, and duality

Softmax. Among distributions pp over KK outcomes with scores sks_k, maximise the entropy −∑kpkln⁡pk-\sum_k p_k \ln p_k with average score fixed at s‾\bar s:

ℒ=−∑kpkln⁡pk+β(∑kpksk−s‾)+ν(∑kpk−1)\mathcal L = -\sum_k p_k \ln p_k + \beta\Big(\sum_k p_k s_k - \bar s\Big) + \nu\Big(\sum_k p_k - 1\Big)

Differentiating in pkp_k: −ln⁡pk−1+βsk+ν=0-\ln p_k - 1 + \beta s_k + \nu = 0, so pk=eν−1eβskp_k = e^{\nu - 1} e^{\beta s_k}, and the sum-to-one constraint fixes eν−1=1/∑jeβsje^{\nu - 1} = 1/\sum_j e^{\beta s_j} (Boyd and Vandenberghe, section 7.2, solve the same problem as a convex program).

TRPO. The paper (equation 55 in appendix C) maximises L(θ)L(\theta) "subject to DKL(θold,θ)≤δD_{\mathrm{KL}}(\theta_{\text{old}}, \theta) \le \delta". The Lagrangian g⊤Δ−λ2(Δ⊤FΔ−2δ)g^\top \Delta - \tfrac{\lambda}{2}(\Delta^\top F \Delta - 2\delta) gives Δ∝F−1g\Delta \propto F^{-1}g; the paper's "β=2δ/s⊤As\beta = \sqrt{2\delta / s^\top A s}" is the length above with s=F−1gs = F^{-1}g (they write AA for the Fisher), solved by conjugate gradient with Fisher-vector products and followed by a line search. TRPO and the clipped objective of PPO that replaced it: Policy gradients and actor-critic; the KL penalty in RLHF and GRPO: RL for LLMs.

Duality, in brief. Minimise the Lagrangian over xx for fixed multipliers: d(μ)=minx⁡[f(x)+∑iμihi(x)]d(\mu) = \min_x [f(x) + \sum_i \mu_i h_i(x)], the dual function. For any μ≥0\mu \ge 0 it is a lower bound on the best feasible value (weak duality), because on feasible points the added terms are ≤0\le 0. The dual problem maximises that bound; for convex problems under Slater's condition the best bound equals the answer (strong duality; Boyd and Vandenberghe, sections 5.2.2 and 5.2.3). The SVM's dual has one multiplier per training example and only inner products xi⊤xjx_i^\top x_j, which is what lets kernels replace them. Convex optimisation in full: Boyd and Vandenberghe, chapters 2 to 5.

10. Stochastic gradients: noise, batch size, and why SGD still converges

The full loss is an average over nn examples, so a minibatch of BB random examples gives gˆ=1B∑i∈batch∇ℓi\hat g = \frac1B \sum_{i \in \text{batch}} \nabla \ell_i. It is unbiased, 𝔼[gˆ]=∇L\mathbb{E}[\hat g] = \nabla L, and its variance falls as 1/B1/B:

𝔼‖gˆ−∇L‖2=σ2B⋅n−Bn−1\mathbb{E}\lVert \hat g - \nabla L \rVert^2 = \frac{\sigma^2}{B} \cdot \frac{n - B}{n - 1}

where σ2\sigma^2 is the average squared distance of one example's gradient from the mean, and the last factor corrects for sampling without replacement (it is 0 at B=nB = n).

Measured: the squared error of the minibatch gradient of the breast-cancer logistic loss at w=0w = 0, averaged over 4,000 random batches per size (dots), against the formula (line). Log-log axes; a slope of −1-1 is the 1/B1/B law. Here σ2=5.74\sigma^2 = 5.74 against a squared gradient length of 2.01: one example's gradient is mostly noise.

Repeat the descent lemma of section 4 with gˆ\hat g: the noise adds a term βη22⋅\frac{\beta\eta^2}{2}\cdot (variance) that does not shrink as the gradient does. So with a constant step SGD does not converge to the minimum; it settles in a region whose size is proportional to η\eta (for a strongly convex, smooth loss, Bottou, Curtis and Nocedal 2018, Theorem 4.6: a geometrically decaying term plus a floor proportional to the step). With a decreasing step satisfying the Robbins-Monro conditions ∑kηk=∞\sum_k \eta_k = \infty, ∑kηk2<∞\sum_k \eta_k^2 \lt \infty (large enough in total to get anywhere, small enough that the noise they add stays finite) the gap goes to zero; with ηk=β/(γ+k)\eta_k = \beta/(\gamma + k) it falls like 1/k1/k (Theorem 4.7, after Robbins and Monro 1951).

This is why schedules decay the learning rate at the end of training; why a larger batch tolerates a larger step (less noise per step: the linear-scaling heuristic of Goyal et al. 2017) until the curvature limit of section 4 takes over; and why L-BFGS (section 6) does not carry over to minibatches. The noise also helps: it pushes the iterate off saddles and plateaus. In practice: Optimisers and learning-rate schedulers (sections 1 and 7).

A constant step stalls at a floor set by the step; a decaying step keeps falling
SGD with batches of 8 on the same logistic regression (71 steps per epoch, geometric mean of 3 seeds), loss gap per epoch. Constant steps 2 and 0.5 stall at floors near 10−210^{-2} and 10−310^{-3}, roughly proportional to the step; step 0.1 is still falling after 150 epochs; the decaying step 2/(1+k/500)2/(1 + k/500) starts fast and keeps falling.

11. The landscape of a deep network: saddles, sharpness and the edge of stability

Saddles, not bad local minima. Dauphin et al. 2014 gather arguments from statistical physics and random-matrix theory that for typical high-dimensional error functions "the ratio of the number of saddle points to local minima increases exponentially with the dimensionality": a minimum needs all nn Hessian eigenvalues positive, and if signs were anything like independent that would be exponentially rare. Critical points with high error tend to have a large index (many negative eigenvalues), so the minima that exist tend to have low error; they check it on small networks trained on down-sampled MNIST and CIFAR-10. Saddles are slow because plateaus surround them: gradient descent escapes along negative curvature, slowly, and Newton is attracted to them (section 5). The Optimisation lab's saddle function shows all three.

The edge of stability. Section 4 says gradient descent with step η\eta is unstable once the top Hessian eigenvalue, the sharpness, exceeds 2/η2/\eta. Cohen et al. 2021 measured the sharpness during full-batch training of real networks and found progressive sharpening (below 2/η2/\eta the sharpness "tends to continually rise") and then the edge of stability: "the maximum eigenvalue of the training loss Hessian hovers just above the value 2/(step size), and the training loss behaves non-monotonically over short timescales, yet consistently decreases over long timescales". The quadratic theory predicts divergence; the network instead adjusts its own curvature. Reproduced for this page on a small network (64 inputs, two hidden layers of 64 tanh units, 10 outputs; 8,970 parameters) trained by full-batch gradient descent on 1,000 of scikit-learn's bundled 8x8 handwritten digits, with squared error on one-hot targets (one of the two losses Cohen et al. study). Predict before choosing a step: at η=0.3\eta = 0.3, whose limit 6.67 is below the starting sharpness of 8.6, does training diverge?

What it shows
At η=0.02,0.05,0.1\eta = 0.02, 0.05, 0.1 (limits 100, 40, 20) the sharpness falls briefly and then rises steadily (progressive sharpening, to 9.5, 10.7 and 11.7 by step 6,000) and the loss falls at every single step. At η=0.2\eta = 0.2 (limit 10) the sharpness reaches 10 at step 825 and then hovers: its mean over the second half of training is 10.02, and the loss rises on 1,790 of 6,000 steps while still falling overall, to 0.0069. At η=0.3\eta = 0.3 (limit 6.67) the start is already too sharp; the sharpness drops to the limit within about 125 steps and stays there (mean 6.70; the loss rises on 2,411 steps). Two more seeds give second-half means of 10.01 and 10.02 (η=0.2\eta = 0.2) and 6.71 and 6.71 (η=0.3\eta = 0.3). The phenomenon reproduces on this small network.

What it means. The step-size limit is real, but in a network it is not a fixed property of the problem: training drives the curvature to whatever the learning rate allows, and a higher learning rate selects flatter regions. This is one reason the learning rate acts as a regulariser, and why sharpness-aware minimisation (SAM; Foret et al. 2021), which minimises the worst loss in a small ball around the weights, looks for flat minima on purpose. Whether flatness causes better generalisation is still debated (sharpness changes under reparameterisation: Dinh et al. 2017).

Top: sharpness (top Hessian eigenvalue, measured every 25 steps) against step, with the line 2/η2/\eta for each learning rate. Bottom: the training loss (log scale); the strip under it shows every step of the η=0.2\eta = 0.2 run over the 100-step stretch with the most up-and-down changes (steps 3,260 to 3,360), where the short-timescale oscillation is visible. All runs start from the same initialisation (sharpness 8.6).
How the sharpness was measured, and other second-order quantities in papers

Every 25 steps the sharpness is measured by power iteration with Hessian-vector products: the vector vv is repeatedly replaced by Hv/‖Hv‖Hv/\lVert Hv \rVert, and the Rayleigh quotient v⊤Hvv^\top H v converges to the top eigenvalue; checked once against SciPy's Lanczos solver: 10.106 against 10.107. Cohen et al. report both cross-entropy and squared error; this run uses squared error.

  • Influence functions estimate how the model would change without one training example, −H−1∇ℓi-H^{-1}\nabla \ell_i (Koh and Liang 2017).
  • Optimal Brain Damage prunes the weights with the smallest 12Hiiwi2\tfrac12 H_{ii} w_i^2, the second-order estimate of the loss increase when wiw_i is set to zero (LeCun et al. 1989).
  • Hessian spectra of trained networks: a bulk near zero plus a few large outliers, roughly one per class (Sagun et al. 2017, Ghorbani et al. 2019).

12. Which method when

nn is the number of parameters; mm the number of stored L-BFGS pairs.

MethodUsesCost per stepWhen
Gradient descent, SGDgradientone gradient; nn memorythe default for deep networks; slow when κ\kappa is large
Momentum, Nesterov, Adamgradient plus nn numbers of state per momentone gradientdeep learning in practice: momentum tolerates ill-conditioning, Adam adds per-coordinate scaling
Newton (damped)gradient and full Hessiann2n^2 memory, about n3n^3 to solvesmall smooth problems; few steps near a minimum; IRLS for GLMs
L-BFGSgradient plus mm gradient differencesabout 4mn4mn plus a line searchfull-batch deterministic problems of moderate size (logistic regression, CRFs, physics fits); not for noisy minibatches
Hessian-free, K-FAC, ShampooHessian- or Fisher-vector products, or per-layer factorsa few gradients per stepwhen curvature matters and a full Hessian is out of reach
XGBoost leavesper-example gig_i, hih_itwo numbers per exampleboosted trees, one Newton step per leaf
Lagrange, KKT, dualityconstraint gradientsproblem-dependentconstraints: simplex, norm balls, KL budgets, margins

For derivatives themselves (reverse mode for one loss and many parameters, forward mode for few inputs and many outputs, Hessian-vector products for curvature without the matrix) see Matrix calculus and backprop, section 7.

Common mistakes

Treating g=0g = 0 as a minimum
In high dimensions a stationary point is far more likely to be a saddle (section 11). Check the curvature: the smallest Hessian eigenvalue, estimated by power iteration on H−cIH - cI or by Lanczos.
"Blame the data" for a learning-rate blow-up
A loss that oscillates and then grows right after the learning rate was raised is the 2/λmax⁡2/\lambda_{\max} limit (section 4). In full-batch training the network may instead sit at the edge of stability, with a loss that wobbles but falls (section 11).
Quoting (κ−1)/(κ+1)(\kappa - 1)/(\kappa + 1) as gradient descent's rate at any step
It holds only at the best fixed step 2/(λmax⁡+λmin⁡)2/(\lambda_{\max} + \lambda_{\min}); at the safe step 1/λmax⁡1/\lambda_{\max} the rate is 1−1/κ1 - 1/\kappa, and anything above 2/λmax⁡2/\lambda_{\max} diverges on a quadratic.
Running Newton with an indefinite Hessian or from far away
Undamped, it steps towards maxima and saddles, and from a flat region it jumps far (the w=8w = 8 start in section 5 diverges). Use a line search, a trust region, H+λIH + \lambda I, or |H||H|.
"Adam is a second-order method"
It divides by the square root of a moving average of squared gradients, a diagonal quantity related to the Fisher, not the Hessian, and not inverted (section 5).
MSE for classification, or squashing twice
On a sigmoid output MSE's gradient vanishes exactly when the model is confidently wrong (section 2). Passing probabilities to BCEWithLogitsLoss or CrossEntropyLoss, which expect logits, squashes twice and shrinks gradients. Computing log(softmax(z)) in two steps underflows; use the fused log-softmax.
Forgetting which MSE convention is in use
12(yˆ−y)2\tfrac12(\hat y - y)^2 has gradient yˆ−y\hat y - y; (yˆ−y)2(\hat y - y)^2 has 2(yˆ−y)2(\hat y - y); the batch mean divides by nn. Each changes the effective learning rate by a constant (section 2).
Assuming the whole network is convex because cross-entropy is
Cross-entropy is convex in the logits; the loss in earlier layers' weights is not (the permutation argument, section 8).
Expecting a constant SGD step to converge
It stalls at a noise floor proportional to the step (section 10); decay the step, or grow the batch.
Mixing up the lambdas
On this page λ\lambda is an eigenvalue (sections 3 and 4), a Lagrange multiplier (section 9) and an L2 penalty (sections 6 and 7). The KKT view links the last two: a penalty strength is a multiplier.
Reading sharpness as a fixed property of the problem
In a network the curvature moves with training and is driven to 2/η2/\eta by the learning rate itself (section 11).