Before this lesson:lesson 02 (partial derivatives,
the gradient, Dvf=∇f⊤v). From linear algebra: a matrix as a map
(lesson 01), the spectral theorem and the Rayleigh quotient
(lesson 06), positive definite matrices
(lesson 07).
Time: about 75 minutes, plus the notebook.
Almost every algorithm in ML uses a derivative. The gradient tells an optimizer which way to move.
The Hessian tells how the gradient changes: it gives curvature, conditioning, saddle points,
Newton and Gauss–Newton steps, and the Laplace approximation. The Jacobian is the object that
backpropagation multiplies, layer by layer (lesson 04).
One tool is behind all of these: Taylor's theorem. Near a point, a smooth function is a
constant, plus a linear term, plus a quadratic term, plus an error that you can bound.
In ML for science the same objects have other names. The Jacobian of a weather model with
respect to its initial state is its tangent linear model. The Taylor test is the standard check
of the adjoint gradient of a PDE solver.
One dimension first. For f:R→R, the derivative f′(x) is the slope of the tangent line
(lesson 01):
f(x+h)=f(x)+f′(x)h+r(h),hr(h)→0 as h→0.
The error r(h) goes to zero faster than h. We write this as r(h)=o(∣h∣), read "small-o
of h". If you zoom in on the graph near x, it looks like a straight line.
Many dimensions. Now take a map f:Rn→Rm, for example a layer of a network. Zoom in near
a point x. A small input change h∈Rn gives an output change that is almost a matrix
times h. That matrix is the derivative. It has one row per output and one column per input, so
it is m×n. It is called the JacobianDf(x).
The figure shows this for maps of the plane. A small square around x0 goes to a curved shape.
The linear map Df(x0) sends the same square to a parallelogram. When the square is small, the two
shapes are the same.
Interactive figure: loading…
Make the square smaller with the δ slider. The image of the square under f
(solid) and its image under the linear map f(x0)+Df(x0)h (dashed) become
the same shape. The right panel is magnified so that the image always fills it. Drag the
point x0 and try the other maps. For polar coordinates, move x0 to r=0:detDf=r becomes 0, and the parallelogram becomes flat.
Read the readout: the largest relative error∥r(h)∥/∥h∥ on the square is roughly
proportional to δ. Make δ ten times smaller, and the relative error becomes ten times
smaller. This is what "r(h)=o(∥h∥)" means in practice.
Scalar functions. When m=1 (a loss), the linear map is h↦∇f(x)⊤h, a dot
product with the gradient∇f(x)∈Rn. Its graph is the tangent plane. The gradient
is orthogonal to the level set {f=f(x)} and points uphill (lesson 2).
Second order. Zoom out a little. Now the tangent plane is not good enough; the function looks
like a quadratic: a bowl, a dome or a saddle. The Hessian∇2f(x), the n×n
matrix of second derivatives, describes this shape. Its eigenvectors are the principal axes of the
shape, and its eigenvalues are the curvatures along these axes.
Interactive figure: loading…
Switch between the tangent plane and the quadratic model, and move x0. The
tangent plane (order 1) and the quadratic model (order 2)
touch the surface at x0. The magenta segments show the
error at the corners of the patch. Halve the patch size ρ (from 0.6 to 0.3): the largest error
falls by about 4 for the plane and by about 8 for the quadratic model. On the bump, find a point
where the model is a dome and one where it is a saddle; on the cubic, find a bowl.
Quick check
f:R3→R2 is differentiable at x. What is the shape of the Jacobian Df(x), and where does Df(x)v live?
Why.
The Jacobian is always outputs × inputs: one row per output fi, one column per input xj. So it is 2×3. It takes an input change v∈R3 to an output change Df(x)v∈R2. The tempting 3×2 is the shape of its transpose, which appears in backpropagation (lesson 4).
Definition.f:Rn→Rm is differentiable at x if there is an m×n matrix
Df(x) with
f(x+h)=f(x)+Df(x)h+r(h),∥h∥∥r(h)∥→0 as h→0.
Df(x) is the Jacobian (the derivative) of f at x. In words: the error of the linear
model is small even compared with the step ∥h∥. We write r(h)=o(∥h∥).
The entries are partial derivatives. Take h=tej, where ej is the j-th unit vector,
and divide by t:
The left side tends to ∂f/∂xj (one partial derivative per output). And Df(x)ej
is column j of Df(x) (linear algebra lesson 1). So
column j of the Jacobian is ∂f/∂xj, and
[Df(x)]ij=∂xj∂fi(x).
This also shows that the matrix is unique: every entry is fixed by a limit. Lesson 2 showed that
partial derivatives can exist at a point where f is not differentiable. The definition above is
what "differentiable" means; continuous partial derivatives are enough to have it.
A linear map.f(x)=Wx with W∈Rm×n. Then f(x+h)=Wx+Wh exactly, so
r(h)=0 and
Df(x)=Wfor every x.
An elementwise map.f(x)=(φ(x1),…,φ(xn)), for example tanh or the
sigmoid applied to each entry. Output i depends only on input i, so
∂fi/∂xj=0 for i=j, and
Df(x)=diag(φ′(x1),…,φ′(xn)).
A layer.f(x)=tanh(Wx+b). Write z=Wx+b. Output i is tanh(zi) with
zi=∑jWijxj+bi. By the 1-D chain rule,
∂fi/∂xj=(1−tanh2zi)Wij. In matrix form:
Df(x)=diag(1−tanh2(z))W.
It is the Jacobian of the activation times the Jacobian of the linear map. Lesson 04
proves that this is always true: the Jacobian of a composition is the product of the Jacobians.
Polar coordinates.f(r,θ)=(rcosθ,rsinθ). The columns are the partial
derivatives with respect to r and θ:
A small rectangle dr×dθ goes to a region of area about rdrdθ
(linear algebra lesson 3: ∣det∣ is the area
scale). Lesson 10 uses this for integrals.
Quick check
W∈R3×2 and f(x)=tanh(Wx) for x∈R2. What is Df(x)?
Why.
Entry (i,j) is ∂tanh(zi)/∂xj=(1−tanh2zi)Wij with z=Wx: scale row i of W by 1−tanh2zi. The result is outputs × inputs =3×2. The first option is the transpose (the shape of a backward pass), and the second evaluates the derivative at the wrong point (x instead of z=Wx).
For a scalar f, the Hessian is the Jacobian of the gradient:
∇2f(x)=D(∇f)(x), with entries [∇2f]ij=∂2f/∂xi∂xj.
We write f∈Ck when all partial derivatives up to order k exist and are continuous.
Theorem (Schwarz, also called Clairaut). If f is C2 near x, then
∂xi∂xj∂2f(x)=∂xj∂xi∂2f(x).
So ∇2f(x) is symmetric.
Why you care: a symmetric matrix has real eigenvalues and an orthonormal basis of eigenvectors
(spectral theorem). Everything below uses this:
curvature along eigenvectors, the minimum/maximum/saddle test, the quadratic model.
Example: a quadratic.f(x)=21x⊤Ax−b⊤x with any square A. Expand:
f(x+h)=f(x)+21(x⊤Ah+h⊤Ax)−b⊤h+21h⊤Ah.
The linear term is (21(A+A⊤)x−b)⊤h, because x⊤Ah=(A⊤x)⊤h. So
∇f(x)=21(A+A⊤)x−b,∇2f=21(A+A⊤).
Only the symmetric part of A matters. "∇(21x⊤Ax)=Ax" is true only when A is symmetric.
When the mixed partials differ
Without continuity of the second derivatives, the order can matter. Take
f(x,y)=xy(x2−y2)/(x2+y2) with f(0,0)=0. Then ∂xf(0,y)=−y for all y, so
∂y∂xf(0,0)=−1. And ∂yf(x,0)=x, so ∂x∂yf(0,0)=+1.
The second partial derivatives exist but are not continuous at 0.
In ML: smooth losses are C2, and autodiff Hessians are symmetric up to rounding. At the kinks of a
ReLU network the Hessian does not exist.
In one dimension. If g is C2 near 0, then for each t there is a τ between 0 and
t with
g(t)=g(0)+g′(0)t+21g′′(τ)t2.
So g(t)=g(0)+g′(0)t+21g′′(0)t2+o(t2). If g is C3, the error is O(t3): it
equals 61g′′′(ξ)t3 for some ξ between 0 and t (the Lagrange form of the
remainder). Here O(t3), "big-O", means "at most a constant times ∣t∣3 for small t".
In n dimensions: reduce to a line. Fix a point x and a direction h, and look at f along
the line through x in direction h:
g(t)=f(x+th).
The derivative of g is a directional derivative of f:g′(t)=Dhf(x+th)=∇f(x+th)⊤h
(lesson 2).
Apply the same rule to each entry of ∇f:g′′(t)=h⊤∇2f(x+th)h.
At t=0:g(0)=f(x),g′(0)=∇f(x)⊤h,g′′(0)=h⊤∇2f(x)h.
Theorem (Taylor, second order). If f is C2 near x, then
f(x+h)=f(x)+∇f(x)⊤h+21h⊤∇2f(x)h+o(∥h∥2).
If f is C3, the error is O(∥h∥3).
Proof of the error term
Apply the 1-D result to g(t)=f(x+th) at t=1: there is a τ∈(0,1) with
f(x+h)=f(x)+∇f(x)⊤h+21h⊤∇2f(x+τh)h.
Subtract the model with ∇2f(x). The error is
21h⊤(∇2f(x+τh)−∇2f(x))h.
Its size is at most 21∥∇2f(x+τh)−∇2f(x)∥2∥h∥2.
∇2f is continuous, so the matrix norm tends to 0 as h→0. So the error divided by
∥h∥2 tends to 0: it is o(∥h∥2).■
The three terms have the sizes 1,∥h∥ and ∥h∥2. The Taylor test at the end of this section
measures exactly these orders.
Write g=∇f(x) and H=∇2f(x). The quadratic model of f at x is
m(h)=f(x)+g⊤h+21h⊤Hh.
Minimize the model. Its gradient is ∇m(h)=g+Hh (the example above, with A=H
symmetric). If H≻0 (positive definite), m is a strictly convex bowl, and its minimum is
where ∇m=0:
hN=−H−1g(compute it by solving Hh=−g;never form H−1).
This is the Newton step. On a quadratic f with H≻0, the model is exact, so one Newton
step lands on the minimum.
Gradient descent is a cruder model. Replace H by η1I. The minimizer of
f(x)+g⊤h+2η1∥h∥2 is h=−ηg: the gradient step. It uses the same curvature
1/η in every direction. In a stretched bowl this is wrong in most directions, so gradient
descent zig-zags.
When H is not positive definite. If H is indefinite, the model is a saddle and has no
minimum. Then hN is the saddle point of the model, and it can point uphill. Practical methods
add damping (H+λI with λ>0) or a line search.
Interactive figure: loading…
Press "One step" a few times, then switch to the cubic. On the stretched bowl, the
Newton step (dashed arrow) goes straight to the minimum, and
gradient descent zig-zags. Change η above 0.1=2/20: gradient descent
diverges along y. On the cubic, drag the start point near the saddle at
(0,0) (for example to (0.3,−0.3)): Newton goes to the saddle, not to the minimum. The readout
says when H is indefinite.
A critical point (stationary point) is a point x∗ with ∇f(x∗)=0.
Theorem (second-order conditions). Let f be C2 near x∗, with g=∇f(x∗) and
H=∇2f(x∗).
(a) Necessary. If x∗ is a local minimum, then g=0 and H⪰0.
(b) Sufficient. If g=0 and H≻0, then x∗ is a strict local minimum.
Why this is true (intuition). Near x∗,f is its quadratic model plus a small error. If the
gradient were not 0, a small step against it would go down. If some direction had negative
curvature, a small step along it would go down. If every direction has positive curvature, the
model is a bowl, and the small error cannot cancel a bowl near its bottom.
Proof.
Taylor: f(x∗+h)=f(x∗)+g⊤h+21h⊤Hh+r(h) with r(h)/∥h∥2→0.
(a), gradient. Suppose g=0. Take h=−tg with t>0. Then
f(x∗+h)=f(x∗)−t∥g∥2+O(t2), which is below f(x∗) for small t. This contradicts the
local minimum, so g=0.
(a), Hessian. Suppose H has an eigenvalue λ<0 with unit eigenvector v. Since g=0,f(x∗+tv)=f(x∗)+21λt2+o(t2), which is <f(x∗) for small t=0.
Contradiction. So all eigenvalues are ≥0:H⪰0.
(b). Let μ=λmin(H)>0. By the Rayleigh quotient,
h⊤Hh≥μ∥h∥2 for every h. Because r(h)/∥h∥2→0, there is a δ>0 with
∣r(h)∣≤4μ∥h∥2 for ∥h∥<δ.
Then for 0<∥h∥<δ:f(x∗+h)−f(x∗)≥2μ∥h∥2−4μ∥h∥2>0.
So x∗ is a strict local minimum. ■
The gap.g=0 and H⪰0 is not sufficient. f(x,y)=x2+y3 at 0 has
∇2f=diag(2,0), but f(0,−t)=−t3<0 for t>0. Step 3 only rules out
negative curvature. Along a direction with zero curvature, the higher-order terms decide.
Classifying a critical point(∇f(x∗)=0,f∈C2) from the eigenvalues of H:
Eigenvalues of H
Type
all >0
strict local minimum
all <0
strict local maximum
some >0 and some <0
saddle point: f goes up along one eigenvector and down along another
all ≥0 (or all ≤0), some =0
degenerate: the test cannot decide
In 2-D there is a shortcut: detH=λ1λ2. So detH<0 means a saddle, and
detH>0 means a minimum (trH>0) or a maximum (trH<0).
Interactive figure: loading…
Move the eigenvalue sliders through 0. The contours are solid where f is above
f(0) and dashed where it is below. The arrows are the
eigenvectors q1,q2 of H. A minimum has only solid contours, a saddle has both kinds. Choose
"λ₂ = 0" and then each extra term along q2: the Hessian is the same, but you get a minimum, a
saddle, or neither. This is why the test cannot decide.
Quick check
At a critical point, H=(1221). What kind of point is it?
Why.
The eigenvalues decide, not the entries. detH=1−4=−3<0, so the eigenvalues have opposite signs: 3 (eigenvector (1,1)) and −1 (eigenvector (1,−1)). Along (1,−1) the function goes down: a saddle. Positive entries or a positive diagonal do not make a matrix positive definite.
Quick check
At a critical point, ∇f=0 and H=diag(2,0). Which statement is true?
Why.
H⪰0 is necessary for a minimum, not sufficient. x2+y4 (a minimum), x2−y4 (a saddle) and x2+y3 (neither) all have this gradient and this Hessian at 0. Along the zero-curvature direction the higher-order terms decide.
Answers to section 1.
(1) No. ∇f(1,1)=(2,20), so −∇f=(−2,−20) points almost straight down along −y.
The minimum lies in the direction (−1,−1). The angle between the two is arccos(22/(4042))≈39.3°.
This is why gradient descent zig-zags in stretched bowls. The Newton step is
−H−1∇f=−(2/2,20/20)=(−1,−1): it lands exactly on the minimum, because a quadratic is its
own quadratic model.
(2) The test cannot decide (second quiz above).
The Taylor test: check a gradient with remainder orders #
You have f and code for a claimed gradient g~(x). You want to know if g~=∇f.
Pick a random direction d and steps t=10−1,10−2,…. Compute
If g~=∇f, then r1(t)=O(t2):order 2. Each 10× smaller t makes r1 100× smaller.
If g~ is wrong, then r1(t)≈t∣(∇f−g~)⊤d∣: order 1 only.
With a claimed Hessian H~ (or a Hessian-vector product), also subtract 21t2d⊤H~d.
The remainder r2(t) has order 3 when both are correct (f∈C3).
The observed order between two steps is
p=log(tk/tk+1)log(r(tk)/r(tk+1)),
the slope of the line on a log–log plot.
Interactive figure: loading…
Switch the gradient to "buggy", then the direction to (1,0). With the correct gradient,
r1 has slope 2 and r2 has slope 3. With the buggy
gradient, r1 has slope 1. With d=(1,0) the bug is invisible, because the error
g~−∇f=(0,1) is orthogonal to d. At small t all curves stop at the rounding level
of f; switch to float32 to see this floor rise.
Three rules.
Use float64. In float32 the rounding floor is so high that the slope-2 range is short.
Read the order in the middle range of t. At very small t, the rounding error of f (about
ε∣f∣, with ε≈2.2⋅10−16 in float64) stops the decrease. That
region says nothing about the gradient.
Use a randomd, so that a bug is not hidden by (∇f−g~)⊤d=0.
The test needs no tolerance and no "right" step size: the order is 2 or it is not.
Lesson 07 covers finite-difference checks and
the best step size.
Quick check
In a Taylor test, the observed order of r1 is about 1.0 for t from 10−1 to 10−5. What does this tell you?
Why.
With the true gradient, r1=∣f(x+td)−f(x)−t∇f⊤d∣ is O(t2). Order 1 means a term t(∇f−g~)⊤d is left over, so g~⊤d=∇f⊤d. Smaller t does not help: the bug term t(∇f−g~)⊤d has order 1 for every t. (It is r0, without the gradient term, that should have order 1.)
The Hessian is symmetric, as Schwarz's theorem says (a polynomial is C∞).
Critical points.∇f=0 gives 3x2=3y and 3y2=3x, so y=x2 and x=y2=x4.
Then x(x3−1)=0. The real solutions are x=0 and x=1, with y=x2.
(0,0):H=(0−3−30). The eigenvalues are −3 (eigenvector
(1,1)) and +3 (eigenvector (1,−1)): a saddle. Check along the eigenvectors:
f(t,t)=2t3−3t2<0 and f(t,−t)=3t2>0 for small t=0.
(1,1):H=(6−3−36), with eigenvalues 6±3=3 and 9.
Both positive: a strict local minimum, with f(1,1)=1+1−3=−1.
At the point x0=(2,1).
f(x0)=8+1−6=3.
∇f(x0)=(3⋅4−3,3⋅1−6)=(9,−3).
H0=(12−3−36). Trace 18, determinant 72−9=63. The
eigenvalues solve λ2−18λ+63=0:λ=9±18=9±32≈13.24
and 4.76. Both positive: H0 is positive definite.
Directional derivative along u=(1,1)/2:∇f⊤u=(9−3)/2=32≈4.243.
Steepest descent: ∥∇f∥=81+9=310, so the direction is
−∇f/∥∇f∥=(−3,1)/10, with rate −310≈−9.487.
Newton step. Solve H0h=−∇f(x0):
12h1−3h2−3h1+6h2=−9,=3.
Multiply the first line by 2 and add the second: 21h1=−15, so h1=−5/7. Then
6h2=3+3h1=3−15/7=6/7, so h2=1/7. The step is h=(−5/7,1/7).
The coefficients match the theory: f(x0)=3,∇f⊤d=9−3=6,21d⊤H0d=21(12−3−3+6)=6, and the cubic rest 2t3. Compare the correct
gradient with a buggy one, g~=(9,−2), for which g~⊤d=7:
t
r0
r1, correct g
r1, buggy g~
r2 (with H0)
10−1
0.662
0.062
0.038
2⋅10−3
10−2
0.0606
6.02⋅10−4
9.40⋅10−3
2⋅10−6
10−3
6.01⋅10−3
6.00⋅10−6
9.94⋅10−4
2⋅10−9
observed order (last two rows)
1.00
2.00
0.98
3.00
The buggy gradient shows at once: its order is 1, not 2. For it, r1=∣−t+6t2+2t3∣: the
−t term is the bug. Here r2=2t3 exactly, because f is a cubic.
NumPy checks the worked example. The notebook has these cells and the exercises.
importnumpyasnpnp.set_printoptions(precision=3)deff(z):x,y=zreturnx**3+y**3-3*x*ydefgrad(z):x,y=zreturnnp.array([3*x**2-3*y,3*y**2-3*x])defhess(z):x,y=zreturnnp.array([[6*x,-3.],[-3.,6*y]])# critical points: classify by the eigenvalues of the Hessianforcin([0.,0.],[1.,1.]):print(c,np.linalg.eigvalsh(hess(np.array(c))))# [-3. 3.] saddle, then [3. 9.] minimumx0=np.array([2.,1.])g,H=grad(x0),hess(x0)print(f(x0),g,np.linalg.eigvalsh(H))# 3.0 [ 9. -3.] [ 4.757 13.243]h=np.linalg.solve(H,-g)# Newton step: solve, never invertprint(7*h,round(f(x0+h),4))# [-5. 1.] -0.7901# Taylor test along d = (1, 1): observed order of r1 for the true and a buggy gradientd=np.array([1.,1.])forg_claimin(g,np.array([9.,-2.])):r1=[abs(f(x0+t*d)-f(x0)-t*g_claim@d)fortin(1e-2,1e-3)]print(round(np.log10(r1[0]/r1[1]),3))# 2.001 (correct), then 0.976 (buggy)# a Jacobian by central differences, checked against the hand formula (polar coordinates)defpolar(p):r,th=preturnnp.array([r*np.cos(th),r*np.sin(th)])p0,eps=np.array([2.,0.5]),1e-6J_fd=np.column_stack([(polar(p0+eps*e)-polar(p0-eps*e))/(2*eps)foreinnp.eye(2)])J=np.array([[np.cos(0.5),-2*np.sin(0.5)],[np.sin(0.5),2*np.cos(0.5)]])print(np.abs(J_fd-J).max()<1e-8,round(np.linalg.det(J),12))# True 2.0 (det = r)
Automatic differentiation computes the same gradient and Hessian exactly up to rounding
(lesson 05); finite differences, as in the last cell, are
only approximations (lesson 07).
Training and the learning rate. A gradient step minimizes a linear model plus a proximity
term. On a quadratic with Hessian H≻0, gradient descent converges only for step sizes
η<2/λmax(H): the top Hessian eigenvalue (the "sharpness") limits the learning rate
(see the optimization track).
Saddles and flat directions. The Hessians of large networks have many eigenvalues near zero
and some negative ones. Degenerate critical points are normal there, so "∇f=0 and
H⪰0" does not certify a minimum.
Second-order methods. Newton, Gauss–Newton, natural gradient and K-FAC all minimize a
quadratic model with some curvature matrix in place of H. Hessian-vector products give Hv
without forming H (lesson 08).
Backpropagation. Every layer has a Jacobian: W for a linear map, diag(φ′)
for an activation. Backprop multiplies their transposes with a vector
(lesson 04, lesson 06).
Bayesian deep learning. The Laplace approximation is the second-order Taylor expansion of the
log posterior at its mode (lesson 11).
ML for science. In 4D-Var data assimilation (weather forecasting), the Jacobian of the forecast
model with respect to the initial state is the tangent linear model, and its transpose is the
adjoint model. A physics-informed network u(x) puts derivatives with respect to the input
coordinates into the PDE residual, for example the Laplacian Δu=tr∇x2u.
Adjoint gradients of PDE solvers are verified with the Taylor test (for example in dolfin-adjoint).
Row or column? MML's gradient is a row; here it is a column. A Jacobian is always outputs ×
inputs. Check shapes before you trust a formula.
"All partial derivatives exist, so f is differentiable." False (lesson 2). You need the
definition, or continuous partial derivatives.
∇(x⊤Ax)=2Ax for every A. Only for symmetric A. In general the gradient is
(A+A⊤)x and the Hessian is A+A⊤.
Classifying from the entries of H.(1221) has positive
entries, but its eigenvalues are 3 and −1: a saddle. Use the eigenvalues (in 2-D,
detH<0 already means a saddle).
Treating H⪰0 with a zero eigenvalue as a minimum. The test cannot decide.
A Newton step with an indefinite H. It goes to the saddle of the model and can increase f.
Check H≻0, or add damping (H+λI with λ>0).
Computing H−1. Solve Hh=−g instead; it is cheaper and more accurate.
Taylor-test traps: float32 (the rounding floor hides order 2), reading the order where
rounding dominates, or a direction d that hides the bug.
f:R10→R3 and ℓ:R10→R. Give the shapes of Df(x),∇ℓ(x) and
∇2ℓ(x). In which spaces do Df(x)v and Df(x)⊤u live?AnswerDf:3×10;∇ℓ: a vector in R10;∇2ℓ:10×10.Df(x)v∈R3 (the output space; v∈R10).Df(x)⊤u∈R10 (the input space; u∈R3).
Find the Jacobian of f(x,y)=(xy,x+y2,sinx) at (0,1).AnswerRows are the gradients of the three outputs: (y,x),(1,2y),(cosx,0). At (0,1):Df=111020, a 3×2 matrix.
For a,b∈Rn, find the gradient and the Hessian of f(x)=(a⊤x)(b⊤x).AnswerProduct rule: ∇f=(b⊤x)a+(a⊤x)b and ∇2f=ab⊤+ba⊤, which is symmetric, as it must be for a polynomial. A common slip is to write only ab⊤, which is not symmetric.
Classify the critical point 0 of f(x,y)=x2+4xy+y2 and give a direction in which f decreases.AnswerH=(2442) has eigenvalues 6 and −2: a saddle. Along the eigenvector (1,−1) of −2:f(t,−t)=t2−4t2+t2=−2t2<0.
For g=∇f(x)=0, when is the Newton direction −H−1g a descent direction?AnswerWhen H≻0: then H−1≻0, and the directional derivative along it is g⊤(−H−1g)<0. For an indefinite H,g⊤H−1g can be negative, and then the Newton direction points uphill.
In a Taylor test, the observed orders of r1 are about 2 for t from 10−1 to 10−6, then
fall to about 0 and become noisy for smaller t. Is the gradient correct?AnswerYes. Order 2 in the middle range means r1=O(t2). At small t the rounding error of f (about ε∣f∣) is larger than the t2 term, so r1 stops decreasing. That region says nothing about the gradient.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
Define the derivative (Jacobian) of f:Rn→Rm at x.
The m×n matrix Df(x) with f(x+h)=f(x)+Df(x)h+r(h) and ∥r(h)∥/∥h∥→0 as h→0.
What are the entries and the shape of the Jacobian Df(x) for f:Rn→Rm?
[Df(x)]ij=∂fi/∂xj; it is m×n (outputs × inputs). Column j is ∂f/∂xj.
What is the Jacobian of an elementwise map x↦φ(x)?
The diagonal matrix diag(φ′(x1),…,φ′(xn)).
Compute detDf for polar coordinates f(r,θ)=(rcosθ,rsinθ).
det(cosθsinθ−rsinθrcosθ)=r.
When is the Hessian symmetric?
When f is C2 near the point (Schwarz/Clairaut theorem): the mixed partial derivatives are then equal.
Write the second-order Taylor expansion of f:Rn→R at x.
f(x+h)=f(x)+∇f(x)⊤h+21h⊤∇2f(x)h+o(∥h∥2).
What is the Newton step, and how do you compute it?
hN=−H−1g, the minimizer of the quadratic model when H≻0. Compute it by solving Hh=−g, never by forming H−1.
State the second-order necessary and sufficient conditions for a local minimum.
Necessary: ∇f=0 and ∇2f⪰0. Sufficient: ∇f=0 and ∇2f≻0 (then the minimum is strict).
At a critical point the Hessian has eigenvalues 4 and −1. What kind of point is it?
A saddle point: f goes up along the first eigenvector and down along the second.
In the Taylor test, which orders do r1 and r2 have when the gradient and Hessian are correct?
r1=∣f(x+td)−f(x)−tg⊤d∣ has order 2; r2 (also minus 21t2d⊤Hd) has order 3. A wrong gradient gives order 1.
"Why does the gradient point in the direction of steepest ascent?" — Dvf=∇f⊤v, then
Cauchy–Schwarz (lesson 2).
"What is the Jacobian of a linear layer followed by an activation?" —
diag(φ′(z))W with z=Wx+b; outputs × inputs.
"How do you tell a minimum from a saddle? And for a network with 108 parameters?" — The signs of
the Hessian eigenvalues. At scale, estimate the extreme eigenvalues with Hessian-vector products
and power iteration or Lanczos (lesson 08).
"Derive the Newton step. When does it fail?" — Minimize the quadratic model: g+Hh=0. It fails
when H is not positive definite, or when the model is poor far from the minimum.
"Is the Hessian always symmetric?" — Yes when f is C2 (Schwarz). It does not exist at the
kinks of a ReLU network.
"How do you test a hand-written gradient?" — The Taylor test (order 2), or a finite-difference
check in float64 with a relative error (lesson 07).
A map of the plane looks linear when you zoom in; this is the first figure of this lesson.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
How the partial derivatives become the columns of the matrix of the local linear map.
Open on YouTube · the player loads from youtube-nocookie.com when you press playOpen on YouTube · the player loads from youtube-nocookie.com when you press play
The second-order Taylor expansion written with the gradient and the Hessian, as in section 3.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The 2-D test with detH; this lesson states it with eigenvalues, which works in any dimension.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The derivative as a linear operator, the view of this lesson, for vectors and matrices.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Deisenroth, Faisal, Ong, Mathematics for Machine Learning (free PDF):
§5.2 (partial derivatives and gradients), §5.3 (Jacobians), §5.7 (the Hessian), §5.8 (linearization
and multivariate Taylor series). Note its row-gradient convention.
Nocedal and Wright, Numerical Optimization, 2nd ed. (Springer, 2006): §2.1 (Taylor's theorem
and the second-order conditions) and §3.3–3.4 (Newton's method and Hessian modification).
dolfin-adjoint, Verification:
the Taylor remainder convergence test in practice.
Code along in Colab
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.