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 FoundationsCalculus

The Hessian, curvature and Taylor approximation

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

Why this matters for machine learning

The gradient says which way is downhill, and the second derivatives say how the ground bends. That tells a bowl from a saddle, limits how big a step can be, and gives a better local model of the loss than a tilted plane.

  • Gradient descent on a bowl-shaped loss is stable only when the learning rate is below 2/λmax⁡2/\lambda_{\max}, where λmax⁡\lambda_{\max} is the largest curvature. (P1.F3: Gradient descent)
  • Newton's method steps to the minimum of the local quadratic model, −H−1∇f-\mathbf{H}^{-1}\nabla f, which is exact in one step for a quadratic. (P1.F6: Second-order methods, coming soon)
  • Telling a minimum from a saddle in a loss landscape takes the signs of the Hessian's eigenvalues. (P1.F2: Convex vs. non-convex, coming soon)
  • Curvature that differs a lot between directions makes gradient descent zig-zag, which momentum and adaptive step sizes try to fix. (P1.F5: Momentum and adaptive steps (preview), coming soon)
  • Second-order methods get curvature information for large models from Hessian-vector products, without ever writing down the Hessian. (P1.F6: Second-order methods, coming soon)

Before this: Derivative rules, The gradient, Directional derivatives, Eigenvalues and eigenvectors

After this lesson you can

  • Read the second derivative f′′f'' as the rate of change of the slope (concave up or down, inflection), and use the second-derivative test, including the cases where it says nothing (x4x^4 and x3x^3 at 00)
  • Build the Taylor polynomial of ex\mathrm{e}^x, sin⁡x\sin x, ln⁡(1+x)\ln(1+x) and the sigmoid term by term, explain why its error falls like a power of the step, and say where it stops working (the radius of convergence)
  • Write the Hessian matrix H\mathbf{H} of a function of several inputs (for the P0.2 loss, (28/3442)\begin{pmatrix}28/3&4\\4&2\end{pmatrix}), use f(p+Δ)≈f(p)+∇f⋅Δ+12Δ⊤HΔf(\mathbf{p}+\boldsymbol{\Delta})\approx f(\mathbf{p})+\nabla f\cdot\boldsymbol{\Delta}+\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta}, and compute the curvature u⊤Hu\mathbf{u}^\top\mathbf{H}\mathbf{u} along a direction
  • Use the eigenvalues of H\mathbf{H} to classify a point where the gradient is zero (bowl, hill, saddle or undecided), and connect the largest eigenvalue to the learning-rate limit η<2/λmax⁡\eta<2/\lambda_{\max} of gradient descent and to Newton's step

P1.C5 found that halving a step cut the error of the tangent-plane estimate from 0.01610.0161 to 0.00400.0040, a factor of about four. Why four? And can the error be predicted instead of only measured? It can. A tangent plane is flat, so what it misses is how the ground bends, and bending is measured by second derivatives. This lesson collects them, first for one input and then for many.

Curvature in one variable

The derivative f′f' is the slope of ff. It is itself a function, so it has a slope too. The second derivative f′′f'' is the derivative of the derivative: how fast the slope changes.

For f(x)=x2f(x)=x^2 the slope is f′(x)=2xf'(x)=2x, and the slope of the slope is f′′(x)=2f''(x)=2. Check it with numbers: at x=3x=3 the slope is 66, and at x=3.1x=3.1 it is 6.26.2. The slope grew by 0.20.2 over a step of 0.10.1, which is f′′⋅0.1f''\cdot0.1.

functionf′f'f′′f''what the graph does
x2x^22x2x22bends upward everywhere
−x2-x^2−2x-2x−2-2bends downward everywhere
x3x^33x23x^26x6xbends downward for x<0x<0, upward for x>0x>0
x4x^44x34x^312x212x^2bends upward, but very flat near 00

The sign of f′′f'' tells which way the graph bends. Where f′′>0f''>0 the slope is increasing and the graph bends upward, like a bowl: it is concave up. Where f′′<0f''<0 the slope is decreasing and the graph bends downward, like a hill: concave down. The size of f′′f'' tells how sharp the bend is. A point where the bend changes sign, such as x=0x=0 for x3x^3, is an inflection point.

The second-derivative test

At a flat point (f′(a)=0f'(a)=0) the sign of f′′(a)f''(a) says what kind of point it is, when f′′f'' is continuous near aa:

  • f′′(a)>0f''(a)>0: the graph bends upward around the flat spot, so aa is a local minimum.
  • f′′(a)<0f''(a)<0: a local maximum.
  • f′′(a)=0f''(a)=0: no conclusion.

The last case is real. The three functions x4x^4, −x4-x^4 and x3x^3 all have f′(0)=0f'(0)=0 and f′′(0)=0f''(0)=0, yet 00 is a minimum, a maximum and neither, respectively. When f′′(0)=0f''(0)=0, look at the next derivatives: for functions with enough derivatives, the first one that is not zero decides. If it is of even order, the point is a minimum when that derivative is positive and a maximum when it is negative. If it is of odd order, the point is neither.

Taylor polynomials: matching a function with powers

Close to a point aa a smooth function can be copied by a polynomial, one term at a time. Write h=x−ah=x-a for the distance from the centre aa.

  1. Degree 0, the constant. f(x)≈f(a)f(x)\approx f(a). It matches the height at aa.
  2. Degree 1, the tangent line (P1.C1, P1.C5). f(x)≈f(a)+f′(a) hf(x)\approx f(a)+f'(a)\,h. It also matches the slope.
  3. Degree 2, add the bend. f(x)≈f(a)+f′(a) h+12f′′(a) h2f(x)\approx f(a)+f'(a)\,h+\tfrac12f''(a)\,h^2.

Why the 12\tfrac12? Over a step of size hh the slope changes from f′(a)f'(a) to about f′(a)+f′′(a)hf'(a)+f''(a)h. The average slope over the step is the middle value, f′(a)+12f′′(a)hf'(a)+\tfrac12f''(a)h, and the height gained is the average slope times hh. That gives f′(a)h+12f′′(a)h2f'(a)h+\tfrac12f''(a)h^2.

The pattern continues. The Taylor polynomial of degree nn at the centre aa is

Pn(x)=∑k=0nf(k)(a)k! (x−a)k=f(a)+f′(a) h+f′′(a)2h2+⋯+f(n)(a)n!hn.\begin{aligned} P_n(x)&=\sum_{k=0}^{n}\frac{f^{(k)}(a)}{k!}\,(x-a)^k\\[4pt] &=f(a)+f'(a)\,h+\frac{f''(a)}{2}h^2\\ &\quad+\dots+\frac{f^{(n)}(a)}{n!}h^n. \end{aligned}

The symbol f(k)f^{(k)} means the kk-th derivative, and k!=1⋅2⋯kk!=1\cdot2\cdots k is a factorial. The factorials are there because differentiating hkh^k a total of kk times gives k!k!: to make the kk-th derivative of the polynomial equal f(k)(a)f^{(k)}(a), the coefficient must be f(k)(a)/k!f^{(k)}(a)/k!. So PnP_n is the polynomial of degree nn that matches ff and its first nn derivatives at the centre.

One example done by hand: ex\mathrm{e}^x

Every derivative of ex\mathrm{e}^x is ex\mathrm{e}^x, and e0=1\mathrm{e}^0=1, so at the centre a=0a=0 every coefficient is 1/k!1/k!:

ex≈1+x+x22+x36+x424+…\mathrm{e}^x\approx1+x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}+\dots

At x=0.5x=0.5 the true value is e0.5=1.64872\mathrm{e}^{0.5}=1.64872. The polynomials of degree 0,1,2,3,40,1,2,3,4 give 1,  1.5,  1.625,  1.64583,  1.648441,\;1.5,\;1.625,\;1.64583,\;1.64844. Each new term is smaller than the one before, and the polynomials close in on the true value.

The widget: add one term at a time

Pick a function, a centre aa and a point xx, then press “+ term”. The blue curve is ff and the pink curve is the polynomial. The red bar at xx is the error.

Add Taylor terms one at a time

f(x) = eˣ. Every derivative of eˣ is eˣ, so at the centre a every coefficient is eᵃ/k!.

−3−2−1123−2246810x
coefficients ck = f(k)(a)/k!: c₀ = 1, c₁ = 1, c₂ = 0.5
P₂(x) = 1 + 1·h + 0.5·h² with h = x − a = 1
terms ckhᵏ: 1 + 1 + 0.5
P₂(x) = 2.5
f(x) = 2.71828
error f(x) − P₂(x) = 0.218
error one degree lower = 0.718
radius of convergence at this centre: no limit

The error at x went from 0.718 (degree 1) to 0.218 (degree 2): the new term helped. This series has no radius limit: for every x the error goes to 0 as terms are added. Far from the centre it only needs more terms than the 8 available here.

Degree n uses the terms up to hⁿ, so it has n + 1 terms (degree 0 is just the constant). h = x − a is the distance from the centre. Tap the plot to move x. The polynomial is only a local picture: it matches f perfectly at the centre and drifts away from it.

Taylor polynomials one term at a time. The blue curve is the function, the pink curve is its Taylor polynomial of degree n at the centre a (the dashed vertical line), and the fainter pink curves are the lower degrees. The solid vertical line marks the point x where the error is read, and the red bar is the gap between f and the polynomial there. Below the plot are the coefficients, the polynomial with numbers, the error, and a sentence on whether x is inside the radius of convergence. Tap the plot to move x.

Things to try:

  1. With ex\mathrm{e}^x at centre 00 and x=1x=1, add terms from degree 00 to 88. The polynomial hugs the curve near the centre first and spreads outward as terms are added. At degree 33 the error at x=1x=1 is 0.05160.0516.
  2. Choose sin⁡x\sin x at centre 00. The degree-22 polynomial is the same as the degree-11 one, because only odd powers appear (the status says so). Move xx to 33: far from the centre the polynomials swing noticeably (degrees 1, 3, 5 give 3, −1.5, 0.533,\,-1.5,\,0.53 against the true 0.140.14), and it takes many terms to settle.
  3. Choose ln⁡(1+x)\ln(1+x) at centre 00. With x=0.5x=0.5 the error shrinks as terms are added. Move xx to 1.51.5 and the status turns red: the polynomials get worse as the degree rises.
  4. Keep ln⁡(1+x)\ln(1+x) and x=1.5x=1.5, but move the centre to a=1a=1. Now xx is 0.50.5 from the centre, inside the new radius, and the polynomials work.
  5. Choose the sigmoid at centre 00. At x=2x=2 it works; at x=4x=4 it fails, although the curve itself is smooth.

How fast the error shrinks

For a smooth function the error of the degree-nn polynomial is about a constant times ∣h∣n+1|h|^{n+1}, provided the next derivative f(n+1)(a)f^{(n+1)}(a) is not 00. The next term that was left out, of size hn+1h^{n+1}, is what dominates. So halving the step divides the error by about 2n+12^{n+1}. When that derivative is 00 the error falls faster: for sin⁡x\sin x at centre 00 the tangent line (sin⁡x≈x\sin x\approx x) has f′′(0)=0f''(0)=0, so its error is about x3/6x^3/6 and falls eight-fold, not four-fold, when the step halves. The sigmoid’s tangent line at 00 does the same. For ex\mathrm{e}^x at centre 00, the table gives the error (in thousandths) of the degree-nn polynomial for the steps h=0.4, 0.2, 0.1h=0.4,\,0.2,\,0.1, and the ratio of the last two errors, where hh halves. The degree 11 is the tangent line, 22 the quadratic and 33 the cubic.

nn0.40.40.20.20.10.1ratio
191.891.821.421.45.175.17≈4\approx4
211.811.81.401.400.1710.171≈8\approx8
31.161.160.06940.06940.004250.00425≈16\approx16

The ratios settle toward 44, 88 and 1616 as the steps get smaller. This explains P1.C5: a tangent plane is a degree-1 model, so halving the step divides its error by about four. For a degree-2 model the error falls about eight-fold, which is a good reason to include the bend.

Where Taylor polynomials stop working

More terms do not always mean a better fit. Try ln⁡(1+x)\ln(1+x) at the centre 00. Its polynomial is x−x22+x33−…x-\tfrac{x^2}{2}+\tfrac{x^3}{3}-\dots. The sums of degrees 11 to 88 at two points are:

  • At x=0.5x=0.5 the true value is 0.40550.4055 and the polynomials give 0.5, 0.375, 0.4167, 0.401, 0.4073, 0.4047, 0.4058, 0.4053.
  • At x=1.5x=1.5 the true value is 0.91630.9163 and the polynomials give 1.5, 0.375, 1.5, 0.2344, 1.7531, −0.1453, 2.2955, −0.9081.

At x=0.5x=0.5 the numbers settle on the true value. At x=1.5x=1.5 they swing farther and farther away. The same happens for 11−x=1+x+x2+…\frac{1}{1-x}=1+x+x^2+\dots (the geometric sum): at x=0.5x=0.5 the sums approach 22, and at x=1.5x=1.5 they grow without bound while the true value is −2-2.

The dividing line is a distance, the radius of convergence RR. Inside it (∣x−a∣<R|x-a|<R) the error goes to 00 as the degree grows. Outside it (∣x−a∣>R|x-a|>R) the error grows without bound. The radius is the distance from the centre to the nearest trouble spot of the function:

  • ln⁡(1+x)\ln(1+x) is undefined at x=−1x=-1, so from the centre 00 the radius is 11. For 11−x\frac{1}{1-x} the trouble spot is x=1x=1, and the radius is 11 as well.
  • ex\mathrm{e}^x and sin⁡x\sin x have no trouble spots, and their polynomials work at any distance. Far from the centre they only need more terms.
  • The sigmoid σ(x)=11+e−x\sigma(x)=\frac{1}{1+\mathrm{e}^{-x}} is smooth at every real xx, yet its series at 00 has radius π≈3.14\pi\approx3.14. The trouble spots are not on the real line: they are at x=±iπx=\pm\mathrm{i}\pi, where e−x=−1\mathrm{e}^{-x}=-1 (complex numbers are beyond this lesson), and they still limit the series.
  • A new centre moves the radius. For ln⁡(1+x)\ln(1+x) at the centre aa it is 1+a1+a, so at a=1a=1 it is 22, and x=2.5x=2.5 is now inside.

At the edge itself (∣x−a∣=R|x-a|=R) the answer depends on the function. For ln⁡(1+x)\ln(1+x) at the right-hand edge x=1x=1 the sums still converge, very slowly. For 11−x\frac{1}{1-x} they do not settle.

The lesson for machine learning is that a Taylor polynomial is a local tool. It is a good copy of the function near the centre and says little far away. Gradient descent relies on exactly that: it trusts a local model for one small step and then rebuilds it (P1.F3).

Two ML functions near zero

The Taylor polynomials of the sigmoid and its relative, the softplus ln⁡(1+ex)\ln(1+\mathrm{e}^x) (a smooth version of the ReLU of P4.4), are short:

σ(x)≈12+x4−x348,ln⁡(1+ex)≈ln⁡2+x2+x28.\begin{aligned} \sigma(x)&\approx\frac12+\frac{x}{4}-\frac{x^3}{48},\\[4pt] \ln(1+\mathrm{e}^x)&\approx\ln2+\frac{x}{2}+\frac{x^2}{8}. \end{aligned}

The sigmoid has no x2x^2 term because σ′′(0)=0\sigma''(0)=0: its slope is σ′=σ(1−σ)\sigma'=\sigma(1-\sigma), which is largest, 14\tfrac14, at 00. At x=0.5x=0.5 the true σ(0.5)=0.62246\sigma(0.5)=0.62246. The line 12+x4\tfrac12+\tfrac x4 gives 0.6250.625, off by 0.00250.0025, and the cubic gives 0.622400.62240, off by 0.000060.00006. For softplus the true value at 0.50.5 is 0.974080.97408 and the quadratic gives 0.974400.97440. A neuron whose input is small is therefore almost linear.

Several inputs: the Hessian

With two inputs, differentiate the partial derivatives again. The second partial derivatives of f(x,y)f(x,y) are

fxx=∂2f∂x2,fxy=∂2f∂y ∂x,fyx=∂2f∂x ∂y,fyy=∂2f∂y2.\begin{aligned} f_{xx}&=\frac{\partial^2 f}{\partial x^2}, & f_{xy}&=\frac{\partial^2 f}{\partial y\,\partial x},\\[6pt] f_{yx}&=\frac{\partial^2 f}{\partial x\,\partial y}, & f_{yy}&=\frac{\partial^2 f}{\partial y^2}. \end{aligned}

The mixed ones differentiate in two different directions, one after the other. For f(x,y)=x2+3xyf(x,y)=x^2+3xy (P1.C4) we have fx=2x+3yf_x=2x+3y and fy=3xf_y=3x, so

fxx=2,fxy=3,fyx=3,fyy=0.\begin{aligned} f_{xx}&=2, & f_{xy}&=3,\\ f_{yx}&=3, & f_{yy}&=0. \end{aligned}

The two mixed partials agree, as P1.C4 promised. This holds whenever the second partials are continuous near the point (Schwarz’s theorem, often called Clairaut’s), which covers the smooth functions of this course. The Hessian matrix collects all second partials:

H=(fxxfxyfyxfyy),Hij=∂2f∂xi ∂xj.\begin{gathered} \mathbf{H}=\begin{pmatrix}f_{xx}&f_{xy}\\f_{yx}&f_{yy}\end{pmatrix}, \\ H_{ij}=\frac{\partial^2 f}{\partial x_i\,\partial x_j}. \end{gathered}

It is also written ∇2f\nabla^2 f. Because the mixed partials agree it is a symmetric matrix, so P1.B12 applies: it has real eigenvalues and perpendicular eigenvectors. For x2+3xyx^2+3xy it is (2330)\begin{pmatrix}2&3\\3&0\end{pmatrix}. In one phrase, the Hessian is the Jacobian (P1.C7) of the gradient: the matrix of slopes of the gradient’s entries.

Two Hessians matter for the rest of the course.

The P0.2 loss. It expands to

L(w,b)=13(14w2+12wb+3b2−58w−24b+62),\begin{aligned} \mathcal{L}(w,b)=\tfrac13\big(&14w^2+12wb+3b^2\\ &-58w-24b+62\big), \end{aligned}

with ∂L∂w=28w+12b−583\frac{\partial\mathcal{L}}{\partial w}=\frac{28w+12b-58}{3} and ∂L∂b=12w+6b−243\frac{\partial\mathcal{L}}{\partial b}=\frac{12w+6b-24}{3} (P1.C4). Differentiating once more gives

H=(28/3442)=23(14663).\mathbf{H}=\begin{pmatrix}28/3&4\\4&2\end{pmatrix}=\frac23\begin{pmatrix}14&6\\6&3\end{pmatrix}.

The loss is a quadratic polynomial, so its second derivatives are constants: this Hessian is the same at every point.

The stretched bowl f=x2+10y2f=x^2+10y^2 of P1.F3. Here fxx=2f_{xx}=2, fyy=20f_{yy}=20 and fxy=0f_{xy}=0, so H=(20020)\mathbf{H}=\begin{pmatrix}2&0\\0&20\end{pmatrix}. This is the matrix that P1.B12 used to explain the factors 0.810.81 and −0.9-0.9 of P1.F3.

For nn inputs the Hessian is an n×nn\times n matrix, with n2n^2 entries (about half of them distinct, by symmetry). Keep that number in mind: it is the reason the Hessian is a luxury for large models.

The quadratic model

Add the bend to the tangent plane of P1.C5. The second-order (quadratic) approximation of ff near p\mathbf{p}, for a step Δ\boldsymbol{\Delta}, is

f(p+Δ)≈f(p)+∇f(p)⋅Δ+12 Δ⊤H(p) Δ.\begin{aligned} f(\mathbf{p}+\boldsymbol{\Delta})&\approx f(\mathbf{p})+\nabla f(\mathbf{p})\cdot\boldsymbol{\Delta}\\ &\quad+\tfrac12\,\boldsymbol{\Delta}^\top\mathbf{H}(\mathbf{p})\,\boldsymbol{\Delta}. \end{aligned}

With one input this is f(a)+f′(a)h+12f′′(a)h2f(a)+f'(a)h+\tfrac12f''(a)h^2, and the Hessian takes the place of f′′f''. The last term 12Δ⊤HΔ\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta} is a single number: multiply H\mathbf{H} into Δ\boldsymbol{\Delta}, then take the dot product with Δ\boldsymbol{\Delta}.

Check on the P0.2 loss at (1,0)(1,0). There L=6\mathcal{L}=6 and ∇L=(−10,−4)\nabla\mathcal{L}=(-10,-4). Take the step Δ=(0.05,0.02)\boldsymbol{\Delta}=(0.05,0.02) of P1.C5. The first-order change is −10(0.05)−4(0.02)=−0.58-10(0.05)-4(0.02)=-0.58, while the true change is −0.5639-0.5639. The curvature term is

12Δ⊤HΔ=12(9.333⋅0.0025+2⋅4⋅0.001+2⋅0.0004)=0.016067,\begin{aligned} \tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta}&=\tfrac12\big(9.333\cdot0.0025\\ &\qquad+2\cdot4\cdot0.001\\ &\qquad+2\cdot0.0004\big)\\ &=0.016067, \end{aligned}

which is exactly P1.C5’s error (0.01610.0161). Halve the step to (0.025,0.01)(0.025,0.01) and the term becomes 0.0040170.004017 (a quarter, since it contains Δ\boldsymbol{\Delta} twice), matching P1.C5’s 0.00400.0040. For the bigger steps of P1.C5 the same term repairs the estimate:

step Δ\boldsymbol{\Delta}first ordercurvature termsum = true change
(0.5,0.2)(0.5,0.2)−5.8-5.8+1.6067+1.6067−4.1933-4.1933
(2,0.8)(2,0.8)−23.2-23.2+25.7067+25.7067+2.5067+2.5067

The quadratic model is not just close here: it is exact, because the loss is itself a quadratic polynomial, so there are no higher-order terms. Even the big step (2,0.8)(2,0.8), where the tangent plane predicted a fall of 23.223.2 while the loss actually rose, is predicted perfectly. For a function that is not quadratic the model is only a good local copy.

Check on a function that is not quadratic. Take f(x,y)=x2yf(x,y)=x^2y at (1,2)(1,2). Then ∇f=(2xy, x2)=(4,1)\nabla f=(2xy,\,x^2)=(4,1) and H=(2y2x2x0)=(4220)\mathbf{H}=\begin{pmatrix}2y&2x\\2x&0\end{pmatrix}=\begin{pmatrix}4&2\\2&0\end{pmatrix}. For Δ=(0.1,0.1)\boldsymbol{\Delta}=(0.1,0.1) the true change is f(1.1,2.1)−f(1,2)=0.541f(1.1,2.1)-f(1,2)=0.541. The first-order estimate is 0.50.5, and adding 12Δ⊤HΔ=0.04\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta}=0.04 gives 0.540.54. For Δ=(0.05,0.05)\boldsymbol{\Delta}=(0.05,0.05) the true change is 0.2601250.260125, the first-order estimate 0.250.25 and the quadratic estimate 0.260.26. Halving the step cuts the first-order error from 0.0410.041 to 0.01010.0101 (about four-fold) and the quadratic error from 0.0010.001 to 0.0001250.000125 (eight-fold), as the table of the previous section predicts.

Curvature along a direction

Walk through the point p\mathbf{p} in a direction u\mathbf{u} of length one. The height along the way is the one-variable function g(t)=f(p+tu)g(t)=f(\mathbf{p}+t\mathbf{u}) of P1.C6. Its slope at t=0t=0 is g′(0)=∇f⋅ug'(0)=\nabla f\cdot\mathbf{u}. Its second derivative, the curvature along u\mathbf{u}, is

g′′(0)=u⊤H u.g''(0)=\mathbf{u}^\top\mathbf{H}\,\mathbf{u}.

This comes from putting Δ=tu\boldsymbol{\Delta}=t\mathbf{u} in the quadratic model: the t2t^2 coefficient is 12u⊤Hu\tfrac12\mathbf{u}^\top\mathbf{H}\mathbf{u}, and g′′(0)g''(0) is twice that. P1.C6 found that the slice of the loss at (1,0)(1,0) along (0.6,0.8)(0.6,0.8) is 6−9.2t+4.24t26-9.2t+4.24t^2, so g′′(0)=8.48g''(0)=8.48. Now we see where it comes from:

u⊤Hu=0.36⋅283+2⋅0.6⋅0.8⋅4+0.64⋅2=3.36+3.84+1.28=8.48.\begin{aligned} \mathbf{u}^\top\mathbf{H}\mathbf{u}&=0.36\cdot\tfrac{28}{3}+2\cdot0.6\cdot0.8\cdot4\\ &\quad+0.64\cdot2\\ &=3.36+3.84+1.28\\ &=8.48. \end{aligned}

Along the axes the curvature is just a diagonal entry: 28/3=9.3328/3=9.33 along ww and 22 along bb. The mixed entry matters for the directions in between.

Principal curvatures: the eigenvalues

How does the curvature change as the direction turns? Because H\mathbf{H} is symmetric, P1.B12 gives two perpendicular unit eigenvectors v1,v2\mathbf{v}_1,\mathbf{v}_2 with eigenvalues λ1≥λ2\lambda_1\ge\lambda_2. Along an eigenvector the curvature equals its eigenvalue, since v⊤Hv=v⊤(λv)=λ\mathbf{v}^\top\mathbf{H}\mathbf{v}=\mathbf{v}^\top(\lambda\mathbf{v})=\lambda. In between it is a blend: if u\mathbf{u} makes the angle φ\varphi with v1\mathbf{v}_1, then

u⊤Hu=λ1cos⁡2φ+λ2sin⁡2φ,\mathbf{u}^\top\mathbf{H}\mathbf{u}=\lambda_1\cos^2\varphi+\lambda_2\sin^2\varphi,

so it never leaves the range from λ2\lambda_2 to λ1\lambda_1. For this reason the eigenvalues are called the principal curvatures: the largest and the smallest bend, along two perpendicular directions.

For the P0.2 loss the eigenvalues are λ1=11.093\lambda_1=11.093 and λ2=0.2404\lambda_2=0.2404 (trace 34/334/3, determinant 8/38/3). The sharp direction is v1=(0.915,0.403)\mathbf{v}_1=(0.915,0.403) and the nearly flat one is v2=(0.403,−0.915)\mathbf{v}_2=(0.403,-0.915). The direction (0.6,0.8)(0.6,0.8) is 29.4∘29.4^\circ from v1\mathbf{v}_1, and 11.093cos⁡2(29.4∘)+0.2404sin⁡2(29.4∘)=8.4811.093\cos^2(29.4^\circ)+0.2404\sin^2(29.4^\circ)=8.48, as before. The loss bends sharply when you move v1\mathbf{v}_1-wise and is nearly straight along v2\mathbf{v}_2: that is a long, narrow valley.

The widget: the Hessian under your finger

Drag the point and read the Hessian

ℒ(w, b) = mean squared error of ŷ = w·x + b (P0.2). H = (2/3)·[[14, 6], [6, 3]] everywhere

01234−2−1012wbλ₁λ₂u
point (w, b) = (1, 0)
ℒ = 6
gradient = (−10, −4)
length ‖gradient‖ = 10.77
Hessian 9.3334 42
eigenvalue λ₁ (largest) = 11.093
eigenvalue λ₂ (smallest) = 0.24
direction of λ₁ = (0.915, 0.403)
direction of λ₂ = (−0.403, 0.915)
curvature type: bowl· condition number λ₁/λ₂ = 46.14
curvature along u = (0.6, 0.8): uᵀHu = 8.48

The gradient is not zero here (length 10.77), so this is not a flat point and the Hessian does not name a minimum, maximum or saddle. It still says how the ground bends: bowl-shaped (both eigenvalues positive).

Thin lines are contour lines (ℒ = 2, 6, …, 38); the thick line passes through your point. λ₁ ≥ λ₂ are the eigenvalues of the Hessian at the point: the solid blue line is the direction in which the ground bends most upward, the dashed blue line the perpendicular direction. The pink arrow u is the direction whose curvature uᵀHu is read out; that curvature always lies between λ₂ and λ₁.

A contour map with a draggable point. At the point the widget shows the gradient, the Hessian matrix, its two eigenvalues and eigen-directions (the blue lines: solid for λ₁, the largest, dashed for λ₂, perpendicular to it) and the curvature uᵀHu along the pink arrow u, whose angle you choose with the slider. The text says whether the point is bowl-shaped, hill-shaped or saddle-shaped, and, at a flat spot, what kind of point it is. Switch on the quadratic model to draw its contour lines (dashed pink) over the true ones. Move the point by dragging, tapping the map, or using the arrow keys.

Things to try:

  1. Start on the loss (preset “loss”) and press “u = (0.6, 0.8)”: the curvature reads 8.488.48 anywhere on the map, because the Hessian is constant. Then press “along λ₁” and “along λ₂” to read the eigenvalues.
  2. Switch on the quadratic model on the loss: the dashed lines sit on top of the true ones, since the model is exact. On the “curved valley” they match only near the point.
  3. On the round bowl, the eigenvalues are equal and no axes are drawn: the ground bends the same way in every direction.
  4. On the stretched bowl, the ratio λ1/λ2\lambda_1/\lambda_2 is 1010. Drag the point: the Hessian does not change.
  5. On the saddle press “Go to the flat spot”: the eigenvalues are 22 and −2-2, and the status names a saddle.

Bowl, hill or saddle: classifying a flat point

Suppose ∇f=0\nabla f=\mathbf{0} at p\mathbf{p} (a flat point, P1.C5). Then the quadratic model loses its linear term:

f(p+Δ)≈f(p)+12Δ⊤HΔ=f(p)+12(λ1c12+λ2c22),\begin{aligned} f(\mathbf{p}+\boldsymbol{\Delta})&\approx f(\mathbf{p})+\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta}\\ &=f(\mathbf{p})+\tfrac12\left(\lambda_1c_1^2+\lambda_2c_2^2\right), \end{aligned}

where c1,c2c_1,c_2 are the components of Δ\boldsymbol{\Delta} along the two eigenvectors. The sign of each eigenvalue says whether ff rises or falls along that axis. This is the preview of P1.B12 made precise. At a point where the gradient is zero and no eigenvalue is 00:

eigenvalues of H\mathbf{H}the groundthe flat point is
all positivecurves up along every axisa bowl, a local minimum
all negativecurves down along every axisa hill, a local maximum
some positive, some negativeup along some axes, down along othersa saddle
one or more equal to 00flat to second order along some axisundecided between the remaining possibilities (see below)

Examples: x2+y2x^2+y^2 has eigenvalues 2,22,2 (bowl); −x2−y2-x^2-y^2 has −2,−2-2,-2 (hill); x2−y2x^2-y^2 has 2,−22,-2 (saddle). The P0.2 loss has 11.0911.09 and 0.240.24 at its flat point (2.5,−1)(2.5,-1), both positive, so it is a bowl, which agrees with the minimum of L=0.5\mathcal{L}=0.5 that P1.C4 found. For x4+y2x^4+y^2 at the origin H=diag(0,2)\mathbf{H}=\mathrm{diag}(0,2): one eigenvalue is 00 and the Hessian cannot decide. The point is in fact a minimum (both x4x^4 and y2y^2 are non-negative), whereas x3+y2x^3+y^2 has the same Hessian at the origin and no minimum there. Only terms beyond the second order can decide. A zero eigenvalue still rules some things out: with a negative eigenvalue next to it the point cannot be a minimum (as in −x2+y4-x^2+y^4), and with a positive one it cannot be a maximum.

Two warnings.

  • Away from a flat point the Hessian does not name a minimum. The Hessian exists everywhere, and its eigenvalues still say how the ground bends, but a point with ∇f≠0\nabla f\neq\mathbf{0} is not a minimum, maximum or saddle. The valley f=(1−x)2+5(y−x2)2f=(1-x)^2+5(y-x^2)^2 has its bottom at (1,1)(1,1), where H=(42−20−2010)\mathbf{H}=\begin{pmatrix}42&-20\\-20&10\end{pmatrix} with eigenvalues 51.6151.61 and 0.390.39. At (0,1)(0,1) the Hessian is diag(−18,10)\mathrm{diag}(-18,10), so the ground there is saddle-shaped, but the gradient is (−2,10)≠0(-2,10)\neq\mathbf{0} and the point is not a saddle. Drag the point on that preset to see the eigenvalues change sign.
  • Local only. A bowl-shaped flat point is a local minimum. Whether it is the lowest point everywhere is a different question (P1.F2).

Curvature, conditioning and the learning rate

Now put curvature and gradient descent together. On a quadratic loss with Hessian H\mathbf{H}, the gradient at the point x\mathbf{x} is Hx\mathbf{H}\mathbf{x} (measuring from the minimum), so one step is x←(I−ηH)x\mathbf{x}\leftarrow(\mathbf{I}-\eta\mathbf{H})\mathbf{x}, as in the box of P1.B12. Along the eigenvector with eigenvalue λ\lambda the error is multiplied by 1−ηλ1-\eta\lambda at every step. On a bowl-shaped quadratic (all eigenvalues positive) it shrinks when ∣1−ηλ∣<1|1-\eta\lambda|<1, which means 0<ηλ<20<\eta\lambda<2. (On a saddle or a hill the error grows along a direction with a negative eigenvalue for every η>0\eta>0, so descent moves away from the flat point there.) For all directions at once:

η<2λmax⁡.\eta<\frac{2}{\lambda_{\max}}.

For x2+10y2x^2+10y^2 this is η<2/20=0.1\eta<2/20=0.1, the cap that P1.F3 found by experiment: at η=0.095\eta=0.095 the factors are 1−2η=0.811-2\eta=0.81 and 1−20η=−0.91-20\eta=-0.9. For the P0.2 loss it is η<2/11.093=0.1803\eta<2/11.093=0.1803. A quick run (the second code cell) confirms it: starting from (1,0)(1,0), after 200200 steps the loss is 0.50.5 (the minimum) for η=0.15\eta=0.15 and about 3×10183\times10^{18} for η=0.19\eta=0.19.

The sharpest direction sets the speed limit. The flattest direction then sets the speed: along it the factor is 1−ηλmin⁡1-\eta\lambda_{\min}, close to 11 when λmin⁡\lambda_{\min} is small. For the P0.2 loss at η=0.15\eta=0.15 the factor along v2\mathbf{v}_2 is 1−0.15⋅0.2404=0.9641-0.15\cdot0.2404=0.964, so each step removes only a few percent of the error there, while along v1\mathbf{v}_1 it is −0.664-0.664 and the error flips sign and dies fast. That is the zig-zag of P1.F3. The size of the problem is the condition number

κ=λmax⁡λmin⁡,\kappa=\frac{\lambda_{\max}}{\lambda_{\min}},

1010 for x2+10y2x^2+10y^2 and 46.146.1 for the P0.2 loss. A large κ\kappa means a long, narrow valley: fast bounce across, slow crawl along. Rescaling the inputs (P3.A11), momentum (P1.F5) and Newton’s step (next) all attack it.

For a loss that is not quadratic the Hessian changes with the point, so 2/λmax⁡2/\lambda_{\max} computed at the current point is a local guide, not a guarantee for the whole path.

A preview of Newton’s step

If the quadratic model is a good copy of the loss, why not jump to the bottom of the model? Setting the gradient of the model to zero, ∇f+HΔ=0\nabla f+\mathbf{H}\boldsymbol{\Delta}=\mathbf{0}, gives

Δ=−H−1∇f.\boldsymbol{\Delta}=-\mathbf{H}^{-1}\nabla f.

This is Newton’s step. In practice one solves HΔ=−∇f\mathbf{H}\boldsymbol{\Delta}=-\nabla f (P1.B9) and does not form the inverse. On the P0.2 loss from (1,0)(1,0):

H−1=(0.75−1.5−1.53.5),\mathbf{H}^{-1}=\begin{pmatrix}0.75&-1.5\\-1.5&3.5\end{pmatrix}, Δ=−H−1(−10−4)=(1.5−1).\boldsymbol{\Delta}=-\mathbf{H}^{-1}\begin{pmatrix}-10\\-4\end{pmatrix}=\begin{pmatrix}1.5\\-1\end{pmatrix}.

One step lands at (2.5,−1)(2.5,-1), the minimum with L=0.5\mathcal{L}=0.5. That is no accident: the model of a quadratic is the function itself. On a function that is not quadratic the step is repeated. Newton’s method has its own lesson, P1.F6, with its trouble spots: if H\mathbf{H} has a negative eigenvalue the step can go toward a saddle or uphill, and H\mathbf{H} may have no inverse.

The cost. A model with nn weights has an n×nn\times n Hessian. For n=106n=10^6 that is 101210^{12} entries, about 44 terabytes in 32-bit numbers, and for a billion weights 101810^{18} entries. Solving with it costs far more. This is why deep learning does not form Hessians. Instead it uses Hessian-vector products: the product Hv\mathbf{H}\mathbf{v} for one direction v\mathbf{v} is the change of the gradient along v\mathbf{v}, so two gradient evaluations give Hv≈∇f(p+εv)−∇f(p−εv)2ε\mathbf{H}\mathbf{v}\approx\frac{\nabla f(\mathbf{p}+\varepsilon\mathbf{v})-\nabla f(\mathbf{p}-\varepsilon\mathbf{v})}{2\varepsilon}, and automatic differentiation (P1.C10) can differentiate the gradient to get it exactly. The second Try-it code cell below does it on the P0.2 loss. With such products the largest curvature λmax⁡\lambda_{\max} can be estimated by repeating the power-iteration idea of P1.B12.

Go deeper Where the error comes from: the remainder

Taylor’s theorem says more than “the error is small”. If f(n+1)f^{(n+1)} exists between aa and xx, then there is a point cc between them with

f(x)−Pn(x)=f(n+1)(c)(n+1)! hn+1.f(x)-P_n(x)=\frac{f^{(n+1)}(c)}{(n+1)!}\,h^{n+1}.

Here h=x−ah=x-a. This is the Lagrange form of the remainder. It shows both claims of this lesson. The error is of size ∣h∣n+1|h|^{n+1}, times a number that depends on the size of the (n+1)(n+1)-th derivative, so halving hh divides it by about 2n+12^{n+1} when that derivative is not 00 near aa. And it can be bounded: for ex\mathrm{e}^x at centre 00 and x=0.5x=0.5, every derivative between 00 and 0.50.5 is at most e0.5<1.65\mathrm{e}^{0.5}<1.65, so the degree-33 error is at most 1.65⋅0.54/24=0.00431.65\cdot0.5^4/24=0.0043. The actual error is 0.00290.0029.

The remainder does not tell the whole story about series, because a bound on f(n+1)f^{(n+1)} can itself grow with nn (this is what happens beyond the radius of convergence). A function can even be smooth and have a Taylor series that converges but equals the wrong function: e−1/x2\mathrm{e}^{-1/x^2} (with value 00 at x=0x=0) has all derivatives 00 at the origin, so its series there is 00.

Go deeper When the order of differentiation matters

The equality fxy=fyxf_{xy}=f_{yx} needs a condition. Take f(x,y)=xy(x2−y2)x2+y2f(x,y)=\frac{xy(x^2-y^2)}{x^2+y^2} with f(0,0)=0f(0,0)=0. Along the axes fx(0,y)=−yf_x(0,y)=-y and fy(x,0)=xf_y(x,0)=x, so differentiating once more gives fxy(0,0)=−1f_{xy}(0,0)=-1 but fyx(0,0)=1f_{yx}(0,0)=1. The second partials of this function exist at the origin but are not continuous there, so Schwarz’s theorem does not apply. For the smooth losses of machine learning this does not happen, and a ReLU (P4.4) has no derivative at its corner and second derivative 00 everywhere else, so the curvature of a ReLU network comes from how its layers combine, not from bends of the ReLU itself.

Go deeper The two-by-two shortcut with the determinant

For two inputs the eigenvalue rule can be tested without computing eigenvalues. The product of the eigenvalues is det⁡H=fxxfyy−fxy2\det\mathbf{H}=f_{xx}f_{yy}-f_{xy}^2. At a flat point:

  • det⁡H>0\det\mathbf{H}>0: the eigenvalues have the same sign, so it is an extremum. It is a minimum if fxx>0f_{xx}>0 and a maximum if fxx<0f_{xx}<0.
  • det⁡H<0\det\mathbf{H}<0: opposite signs, a saddle.
  • det⁡H=0\det\mathbf{H}=0: an eigenvalue is 00, and the Hessian alone cannot decide between the remaining possibilities.

For x2+4xy+y2x^2+4xy+y^2 at the origin, H=(2442)\mathbf{H}=\begin{pmatrix}2&4\\4&2\end{pmatrix} has det⁡=4−16=−12<0\det=4-16=-12<0: a saddle, with eigenvalues 66 and −2-2. With three or more inputs one needs the signs of all the eigenvalues; the determinant alone is not enough.

Go deeper Why the loss Hessian is two over N times X transposed X

For a linear model with squared error, the P0.2 loss is L=1N∥Xp−y∥2\mathcal{L}=\frac1N\lVert\mathbf{X}\mathbf{p}-\mathbf{y}\rVert^2, where the rows of X\mathbf{X} are (xi,1)(x_i,1) and p=(w,b)\mathbf{p}=(w,b). Its gradient is 2NX⊤(Xp−y)\frac2N\mathbf{X}^\top(\mathbf{X}\mathbf{p}-\mathbf{y}) and its Hessian is the constant matrix

H=2NX⊤X=23(1+4+9663)=23(14663).\begin{aligned} \mathbf{H}&=\frac2N\mathbf{X}^\top\mathbf{X} \\ &=\frac23\begin{pmatrix}1+4+9&6\\6&3\end{pmatrix} \\ &=\frac23\begin{pmatrix}14&6\\6&3\end{pmatrix}. \end{aligned}

For any vector u\mathbf{u}, u⊤Hu=2N∥Xu∥2≥0\mathbf{u}^\top\mathbf{H}\mathbf{u}=\frac2N\lVert\mathbf{X}\mathbf{u}\rVert^2\ge0, so no eigenvalue is negative: the matrix is positive semi-definite (P1.B10), and the loss can be a bowl or a trough, but not a saddle. The shape of the bowl depends only on X\mathbf{X}, and it tends to be long and narrow when the columns of X\mathbf{X} are strongly correlated or on very different scales (P3.A11). A related preview for P1.C11: the Hessian of x⊤Ax\mathbf{x}^\top\mathbf{A}\mathbf{x} is A+A⊤\mathbf{A}+\mathbf{A}^\top, which is 2A2\mathbf{A} when A\mathbf{A} is symmetric.

Go deeper More inputs, and the next order

With nn inputs the quadratic model is the same formula, with an n×nn\times n matrix, and the classification uses all nn eigenvalues: a mixture of signs is a saddle. Third derivatives form a table with n3n^3 entries, which is why nobody writes the cubic Taylor model of a neural network.

The Hessian in three depths

Intuition

The slope tells how steeply the ground tilts, and the second derivative tells how fast that tilt changes: the bend. In one variable it is a single number. With several inputs the ground can bend one way along one direction and another way along another, so the bend is a matrix, and its eigenvalues are the bends along two perpendicular directions.

A Taylor polynomial copies the ground at a spot: first the height, then the tilt, then the bend. The more terms, the better the copy near the spot, and the copy is useful only within some distance from it.

Math

Pn(x)=∑k=0nf(k)(a)k!(x−a)k,P_n(x)=\sum_{k=0}^{n}\frac{f^{(k)}(a)}{k!}(x-a)^k,f(x)−Pn(x)=f(n+1)(c)(n+1)!(x−a)n+1.\begin{gathered} f(x)-P_n(x) \\ \quad=\frac{f^{(n+1)}(c)}{(n+1)!}(x-a)^{n+1}. \end{gathered}H=∇2f,Hij=∂2f∂xi∂xj=Hji,f(p+Δ)≈f+∇f⋅Δ+12Δ⊤HΔ.\begin{aligned} \mathbf{H}&=\nabla^2f, \\ H_{ij}&=\frac{\partial^2f}{\partial x_i\partial x_j}=H_{ji}, \\ f(\mathbf{p}+\boldsymbol{\Delta})&\approx f+\nabla f\cdot\boldsymbol{\Delta} \\ &\quad+\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta}. \end{aligned}

Along a unit vector u\mathbf{u}: g(t)=f(p+tu)g(t)=f(\mathbf{p}+t\mathbf{u}) has g′(0)=∇f⋅ug'(0)=\nabla f\cdot\mathbf{u} and g′′(0)=u⊤Hu∈[λmin⁡,λmax⁡]g''(0)=\mathbf{u}^\top\mathbf{H}\mathbf{u}\in[\lambda_{\min},\lambda_{\max}]. At ∇f=0\nabla f=\mathbf{0} the signs of the eigenvalues classify the point. Gradient descent on a quadratic has x←(I−ηH)x\mathbf{x}\leftarrow(\mathbf{I}-\eta\mathbf{H})\mathbf{x}, convergent iff η<2/λmax⁡\eta<2/\lambda_{\max}, with rate governed by κ=λmax⁡/λmin⁡\kappa=\lambda_{\max}/\lambda_{\min}. Newton: Δ=−H−1∇f\boldsymbol{\Delta}=-\mathbf{H}^{-1}\nabla f, found by solving HΔ=−∇f\mathbf{H}\boldsymbol{\Delta}=-\nabla f.

Code

import numpy as np

def hessian(grad, p, h=1e-4):
    """Hessian by differencing the gradient: column j is the change of the gradient along input j."""
    p = np.asarray(p, dtype=float)
    cols = [(grad(p + h * e) - grad(p - h * e)) / (2 * h) for e in np.eye(p.size)]
    return np.column_stack(cols)

vals, vecs = np.linalg.eigh(H)      # eigenvalues in increasing order; classify with their signs
step = np.linalg.solve(H, -g)       # Newton's step: solve, do not invert

PyTorch returns the Hessian, and Hessian-vector products, with automatic differentiation (this runs in Colab or on your machine, not in the browser here):

import torch
from torch.autograd.functional import hessian, hvp

X = torch.tensor([[1., 1.], [2., 1.], [3., 1.]])
y = torch.tensor([2., 3., 7.])
loss = lambda p: ((X @ p - y) ** 2).mean()
p = torch.tensor([1.0, 0.0])

print(hessian(loss, p))                       # [[9.3333, 4.], [4., 2.]]
print(hvp(loss, p, torch.tensor([0.6, 0.8]))[1])   # [8.8, 4.0], without forming the matrix

Try it: Taylor polynomials in code

Try it: how the Taylor error shrinks, and where it fails

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

Challenge: use the centre a=1a=1 for ln⁡(1+x)\ln(1+x). The coefficients are ln⁡2\ln 2 and then (−1)k−1k 2k\frac{(-1)^{k-1}}{k\,2^k} for hkh^k. Check that at x=2.5x=2.5 the polynomials of degree 11 to 88 settle near ln⁡3.5=1.2528\ln3.5=1.2528.

Try it: the Hessian, curvature and Newton on the P0.2 loss

Try it: Hessian, eigenvalues, the quadratic model, Newton, and the learning-rate limit

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

Challenge: change the data to y=(2,3,4)y=(2,3,4) and rerun. The Hessian does not change (it depends on X\mathbf{X}, not on y\mathbf{y}). Where does Newton’s step land now?

Common mistakes

Check your understanding

  1. Let f(x)=x4f(x)=x^4. What is f′′(0)f''(0), and what does the second-derivative test conclude at the flat point x=0x=0?

  2. A smooth function has a non-zero third derivative at the centre. The error of its degree-2 Taylor polynomial is measured for a step hh and again for a step h/2h/2. About how does the error change?

  3. What is the Hessian of f(x,y)=x2+3xyf(x,y)=x^2+3xy?

  4. At a point where the gradient is zero, the Hessian has eigenvalues 33 and −1-1. What kind of point is it?

  5. Gradient descent runs on a quadratic loss whose Hessian is diag(2,20)\mathrm{diag}(2,20). Below which learning rate does it converge, and above which does it diverge?

Exercises

  1. For f(x)=x3−3xf(x)=x^3-3x, find the flat points and classify them with the second-derivative test. Where is the inflection point?
    Show answerf′=3x2−3=0f'=3x^2-3=0 at x=±1x=\pm1. f′′=6xf''=6x, so f′′(1)=6>0f''(1)=6>0 (a local minimum, f(1)=−2f(1)=-2) and f′′(−1)=−6<0f''(-1)=-6<0 (a local maximum, f(−1)=2f(-1)=2). The inflection point is at x=0x=0, where f′′=6xf''=6x changes sign.
  2. Find the degree-3 Taylor polynomial of sin⁡x\sin x at the centre 00, evaluate it at x=0.5x=0.5 and give its error.
    Show answerP3(x)=x−x36P_3(x)=x-\frac{x^3}{6}, so P3(0.5)=0.5−0.0208333=0.4791667P_3(0.5)=0.5-0.0208333=0.4791667. The true value is 0.47942550.4794255, so the error is about 0.0002590.000259.
  3. The degree-nn polynomials of ln⁡(1+x)\ln(1+x) at centre 00 for x=1.5x=1.5 are 1.5, 0.375, 1.5, 0.2344,…1.5,\,0.375,\,1.5,\,0.2344,\dots for n=1,2,3,4,…n=1,2,3,4,\dots, and the true value is 0.91630.9163. Why do they not settle?
    Show answerThe point x=1.5x=1.5 is 1.51.5 from the centre, and the radius of convergence is 11 (the distance to the trouble spot x=−1x=-1). Outside the radius, more terms make the polynomial worse: the sums swing wider, reaching 2.29552.2955 at degree 77 and −0.9081-0.9081 at degree 88.
  4. Find the gradient and the Hessian of f(x,y)=x2yf(x,y)=x^2y at (1,2)(1,2).
    Show answer∇f=(2xy, x2)=(4,1)\nabla f=(2xy,\,x^2)=(4,1). fxx=2y=4f_{xx}=2y=4, fxy=fyx=2x=2f_{xy}=f_{yx}=2x=2, fyy=0f_{yy}=0, so H=(4220)\mathbf{H}=\begin{pmatrix}4&2\\2&0\end{pmatrix}.
  5. Use the quadratic model with the answer to exercise 4 to estimate f(1.1,2.1)−f(1,2)f(1.1,2.1)-f(1,2).
    Show answerΔ=(0.1,0.1)\boldsymbol{\Delta}=(0.1,0.1): first order 4(0.1)+1(0.1)=0.54(0.1)+1(0.1)=0.5. Curvature term 12(4⋅0.01+2⋅2⋅0.01+0)=0.04\tfrac12(4\cdot0.01+2\cdot2\cdot0.01+0)=0.04. The estimate is 0.540.54 and the true change is 0.5410.541.
  6. For the P0.2 loss, with H=(28/3442)\mathbf{H}=\begin{pmatrix}28/3&4\\4&2\end{pmatrix}, find the curvature along (1,0)(1,0) and along 12(1,1)\tfrac{1}{\sqrt2}(1,1).
    Show answerAlong (1,0)(1,0) it is H11=28/3≈9.333H_{11}=28/3\approx9.333. Along 12(1,1)\frac1{\sqrt2}(1,1) it is 12(283+2⋅4+2)=12⋅583≈9.667\tfrac12\left(\tfrac{28}{3}+2\cdot4+2\right)=\tfrac12\cdot\tfrac{58}{3}\approx9.667. Both lie between λ2=0.24\lambda_2=0.24 and λ1=11.09\lambda_1=11.09.
  7. Classify the flat point at the origin of f(x,y)=x2+4xy+y2f(x,y)=x^2+4xy+y^2.
    Show answer∇f=(2x+4y, 4x+2y)=0\nabla f=(2x+4y,\,4x+2y)=\mathbf{0} at the origin. H=(2442)\mathbf{H}=\begin{pmatrix}2&4\\4&2\end{pmatrix} has eigenvalues 66 and −2-2 (trace 44, determinant −12-12). Mixed signs: a saddle. For example, f(t,t)=6t2≥0f(t,t)=6t^2\ge0 while f(t,−t)=−2t2≤0f(t,-t)=-2t^2\le0.
  8. What is the largest learning rate for which gradient descent converges on a quadratic with Hessian diag(2,20)\mathrm{diag}(2,20), and on the P0.2 loss?
    Show answer2/λmax⁡2/\lambda_{\max}: 2/20=0.12/20=0.1, and 2/11.093≈0.1802/11.093\approx0.180.
  9. Take one Newton step on f=x2+10y2f=x^2+10y^2 from (1,1)(1,1).
    Show answer∇f=(2,20)\nabla f=(2,20), H=diag(2,20)\mathbf{H}=\mathrm{diag}(2,20), so Δ=−H−1∇f=−(1,1)=(−1,−1)\boldsymbol{\Delta}=-\mathbf{H}^{-1}\nabla f=-(1,1)=(-1,-1), which lands at (0,0)(0,0), the minimum, in one step.
  10. A model has 10610^6 weights. How many entries does its Hessian have, and how much memory is that in 32-bit numbers?
    Show answer106×106=101210^6\times10^6=10^{12} entries, at 44 bytes each 4×10124\times10^{12} bytes, about 44 terabytes (about half of that if only one triangle of the symmetric matrix is stored).

Next: P1.C9 turns from slopes to areas (integrals), and P1.C10 shows how a computer gets exact derivatives automatically. P1.C11 gives the matrix-calculus rules for gradients and Hessians of the expressions of machine learning. Later, P1.F2 uses the classification of flat points for minima and saddles, P1.F3 and P1.F5 use the curvature picture for learning rates and momentum, and P1.F6 turns Newton’s step into a method.

Key takeaways

  • The second derivative f′′f'' is the derivative of the slope. Where f′′>0f''>0 the graph bends upward, where f′′<0f''<0 it bends downward, and an inflection point is where the bend changes sign. At a point with f′=0f'=0 the second-derivative test says minimum for f′′>0f''>0, maximum for f′′<0f''<0, and nothing for f′′=0f''=0: x4x^4, −x4-x^4 and x3x^3 all have f′′(0)=0f''(0)=0 and are a minimum, a maximum and neither.
  • The Taylor polynomial of degree nn at the centre aa is ∑k=0nf(k)(a)k!(x−a)k\sum_{k=0}^{n}\frac{f^{(k)}(a)}{k!}(x-a)^k: constant, then the tangent line, then the bend, and so on. For a smooth ff whose next derivative f(n+1)(a)f^{(n+1)}(a) is not 00, its error falls like ∣x−a∣n+1|x-a|^{n+1}, so halving the step divides the error by about 2n+12^{n+1} (44 for the tangent line, 88 for the quadratic).
  • A Taylor series works only within its radius of convergence: for ln⁡(1+x)\ln(1+x) and 11−x\frac{1}{1-x} at the centre 00 the radius is 11, and beyond it more terms make the polynomial worse. For ex\mathrm{e}^x and sin⁡x\sin x it works at every distance, and for the sigmoid the radius at 00 is π\pi.
  • The Hessian H\mathbf{H} is the matrix of all second partial derivatives, Hij=∂2f∂xi∂xjH_{ij}=\frac{\partial^2 f}{\partial x_i\partial x_j}. It is symmetric when the second partials are continuous. For the P0.2 loss it is the constant matrix (28/3442)\begin{pmatrix}28/3&4\\4&2\end{pmatrix}, and for x2+10y2x^2+10y^2 it is diag(2,20)\mathrm{diag}(2,20).
  • The quadratic model f(p+Δ)≈f(p)+∇f⋅Δ+12Δ⊤HΔf(\mathbf{p}+\boldsymbol{\Delta})\approx f(\mathbf{p})+\nabla f\cdot\boldsymbol{\Delta}+\tfrac12\boldsymbol{\Delta}^\top\mathbf{H}\boldsymbol{\Delta} is exact when ff is quadratic. The curvature along a unit direction u\mathbf{u} is u⊤Hu\mathbf{u}^\top\mathbf{H}\mathbf{u} (8.488.48 for the loss along (0.6,0.8)(0.6,0.8)), and the eigenvalues of H\mathbf{H} are the principal curvatures.
  • At a point where ∇f=0\nabla f=\mathbf{0}: all eigenvalues positive is a bowl (local minimum), all negative a hill (local maximum), mixed signs a saddle, and an eigenvalue 00 leaves the question open. Gradient descent on a quadratic is stable only when η<2/λmax⁡\eta<2/\lambda_{\max} (0.10.1 for x2+10y2x^2+10y^2), and it is slow when λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min} is large.
  • Newton's step −H−1∇f-\mathbf{H}^{-1}\nabla f jumps to the minimum of the quadratic model: from (1,0)(1,0) on the P0.2 loss it lands at (2.5,−1)(2.5,-1) in one step. The Hessian has n2n^2 entries for nn weights, so for large models it is rarely formed, and a Hessian-vector product Hv\mathbf{H}\mathbf{v} is computed from gradients instead.
Found a mistake? Tell us