Ten problems on functions of several variables: partial derivatives, the gradient as the direction of steepest ascent and the normal to a level set, directional derivatives, Hessians and the classification of critical points, the quadratic ½xᵀAx − bᵀx with a Newton step, log-sum-exp, second-order Taylor, the logistic-loss Hessian and convexity, with worked solutions and the mistakes that misread a Hessian.
Before you start
A loss function takes many numbers and returns one. Its first derivatives, collected into the gradient, say which way is downhill and how steeply; its second derivatives, collected into the Hessian, say how the slope itself changes, which decides whether a flat point is a minimum, a maximum or a saddle, whether the function is convex, and how far a Newton step should go. These ten problems build both objects from ordinary partial derivatives, then use them on the functions machine learning keeps meeting: a quadratic, log-sum-exp and the logistic loss. The five mistakes at the end each misread one of the two.
f maps Rn to .R. The partial derivative ∂f/∂xi differentiates in xi with every other variable held fixed. In two variables, ,fx=∂f/∂x, and fxy means differentiate in ,x, then in .y.
The conventions are those of the matrix-calculus page: vectors are columns, the row derivative ∂f/∂x is ,1×n, and the gradient is its transpose, ,∇f=(∂f/∂x)⊤, an n×1 column. A point in the plane is written ;(x,y); a vector is written as a column, such as .(4,8)⊤.
The Hessian ∇2f is the n×n matrix with ;(∇2f)ij=∂2f/∂xi∂xj; it is the Jacobian of .∇f. When the second partials are continuous, which holds for every function on this page, it is symmetric: .fxy=fyx.
For a unit vector ,u, the directional derivative is ,Duf=dtdf(x+tu)t=0=∇f(x)⊤u, the rate of change of f per unit distance along .u.
A symmetric matrix H is positive semidefinite (PSD) if v⊤Hv≥0 for every ,v, and positive definite if v⊤Hv>0 for every ;v=0; equivalently, all its eigenvalues are ,≥0, or all .>0.
A critical point has .∇f=0. There, a positive definite Hessian means a strict local minimum, a negative definite one a strict local maximum, and one with eigenvalues of both signs a saddle; a zero eigenvalue leaves the test inconclusive. For ,2×2,det∇2f is the product of the two eigenvalues.
A twice-differentiable f on Rn is convex exactly when ∇2f(x) is PSD at every .x.
Newton's method steps from xk to .xk+1=xk−(∇2f(xk))−1∇f(xk).σ(t)=1/(1+e−t) is the logistic sigmoid, with σ′=σ(1−σ) (the differentiation-rules page).
Let .f(x,y)=x2y3+ye2x. Compute ,∂f/∂x,∂f/∂y and .∇f(0,1).
·
Let .f(x,y)=xey+y2. Find the rate of change of f at (1,0) in the direction of .v=(3,4)⊤.
··
Let .f(x,y)=x2+4y2. At ,(2,1), find the unit direction of steepest ascent and the rate of increase along it. Then show that ∇f(2,1) is perpendicular to the level curve of f through .(2,1).
·
Let .f(x,y)=x3y2−4xy+y3. Compute the Hessian ∇2f and confirm that .fxy=fyx.
··
Find every critical point of f(x,y)=x3+x2+3xy+y2 and classify each as a local minimum, local maximum or saddle.
··
Let f(x)=21x⊤Ax−b⊤x with ,A∈Rn×n, not necessarily symmetric, and .b∈Rn. Compute ∇f and .∇2f. Assuming 21(A+A⊤) is invertible, take one Newton step from an arbitrary .x0. Where does it land?
···
Let f(x)=log∑j=1nexj (log-sum-exp). Compute ∇f and .∇2f.
··
Write the second-order Taylor polynomial of a function f of n variables around ,x, then apply it to log-sum-exp from Problem 7 around .x=0. Express the result in terms of ,h∈Rn, the displacement from .0.
···
Logistic regression has data X∈RN×d with row n equal to ,x(n)⊤, labels ,yn∈{0,1}, weights ,w∈Rd, probabilities p=σ(Xw) with ,pn=σ(x(n)⊤w), and the mean binary cross-entropy ,L(w), whose gradient is ∇wL=N1X⊤(p−y) (the regression page derives it). Show that ∇w2L=N1X⊤DX with ,D=diag(p⊙(1−p)), and that it is positive semidefinite.
··
For which real numbers c is f(x,y)=x2+cxy+y2+ex convex on ?R2?
Answers
,∂f/∂x=2xy3+2ye2x,,∂f/∂y=3x2y2+e2x,∇f(0,1)=(2,1)⊤
,Duf(1,0)=57, with u=(53,54)⊤
;∇f(2,1)=(4,8)⊤; steepest ascent along (1,2)⊤/5 at rate ;45; the level curve x2+4y2=8 has tangent (−2,1)⊤ there, and (4,8)(−2,1)⊤=−8+8=0
,∇2f=(6xy26x2y−46x2y−42x3+6y), and fxy=fyx=6x2y−4
(0,0) is a saddle: ,det=−5<0, eigenvalues 5 and .−1.(65,−45) is a strict local minimum: det=5>0 and fxx=7>0
,∇f=21(A+A⊤)x−b,;∇2f=21(A+A⊤); Newton's step gives x1=(21(A+A⊤))−1b from any ,x0, which is A−1b when A is symmetric
∇w2L=N1X⊤DX with ,D=diag(p⊙(1−p)), and v⊤∇w2Lv=N1∑npn(1−pn)(x(n)⊤v)2≥0 for every ,v, so it is positive semidefinite
,∇2f=(2+excc2), and f is convex exactly when ∣c∣≤2
Worked solutions
Problem 1
Let .f(x,y)=x2y3+ye2x. Compute ,∂f/∂x,∂f/∂y and .∇f(0,1).
∂x∂(x2y3)=2xy3 and .∂x∂(ye2x)=2ye2x.With y held fixed, y3 and y are constant factors, and (e2x)′=2e2x by the chain rule.
∂y∂(x2y3)=3x2y2 and .∂y∂(ye2x)=e2x.Now x is held fixed, so x2 and e2x are the constant factors.
.∇f(0,1)=(0+2⋅1⋅e0,0+e0)⊤=(2,1)⊤.The gradient stacks the two partials as a column; evaluate each at ,x=0,.y=1.
,∂f/∂x=2xy3+2ye2x,,∂f/∂y=3x2y2+e2x,∇f(0,1)=(2,1)⊤
Problem 2
Let .f(x,y)=xey+y2. Find the rate of change of f at (1,0) in the direction of .v=(3,4)⊤.
.∇f=(ey,xey+2y)⊤.Partial in x with y fixed, then in y with x fixed.
.∇f(1,0)=(1,1)⊤.e0=1 and 2y=0 at .y=0.
,∥v∥=32+42=5, so .u=v/∥v∥=(53,54)⊤.A rate per unit distance needs a unit vector; v only names the direction.
.Duf(1,0)=∇f(1,0)⊤u=53+54.The directional derivative is the gradient's inner product with the unit direction.
,Duf(1,0)=57, with u=(53,54)⊤Sanity check: it cannot exceed ∥∇f(1,0)∥=2≈1.41 (Problem 3), and ,57=1.4, because v points almost along .(1,1)⊤.
Problem 3
Let .f(x,y)=x2+4y2. At ,(2,1), find the unit direction of steepest ascent and the rate of increase along it. Then show that ∇f(2,1) is perpendicular to the level curve of f through .(2,1).
,∇f=(2x,8y)⊤, so .∇f(2,1)=(4,8)⊤.The two partials of a sum of one-variable terms.
For a unit ,u,,Duf=∇f⊤u=∥∇f∥cosθ, with θ the angle between ∇f and .u.The inner product of two vectors is the product of their lengths and the cosine of the angle between them, and .∥u∥=1.
Duf is largest when :θ=0:,u=∇f/∥∇f∥=(4,8)⊤/80=(1,2)⊤/5, at rate .∥∇f∥=80=45.,cosθ≤1, with equality only when u points along the gradient.
The level curve through (2,1) is .x2+4y2=8.,f(2,1)=4+4=8, and a level curve is the set where f keeps that value.
Let r(t) be any curve on it with .r(0)=(2,1). Then f(r(t))=8 for all ,t, so .∇f(2,1)⊤r′(0)=0.Chain rule: the derivative of f(r(t)) is ,∇f⊤r′(t), and the derivative of a constant is .0. Every tangent to the level curve is therefore perpendicular to the gradient.
Concretely, r(t)=(22cost,2sint) traces the curve and passes through (2,1) at ,t=π/4, where .r′(π/4)=(−22sin4π,2cos4π)⊤=(−2,1)⊤.,(22cost)2+4(2sint)2=8, and .sin4π=cos4π=1/2.
;∇f(2,1)=(4,8)⊤; steepest ascent along (1,2)⊤/5 at rate ;45; the level curve x2+4y2=8 has tangent (−2,1)⊤ there, and (4,8)(−2,1)⊤=−8+8=0So the gradient points straight across the contours, uphill, and its length is the slope in that direction. Gradient descent steps along −∇f for this reason.
Problem 4
Let .f(x,y)=x3y2−4xy+y3. Compute the Hessian ∇2f and confirm that .fxy=fyx.
fx=3x2y2−4y and .fy=2x3y−4x+3y2.First partials, each with the other variable held fixed.
.fxx=∂x∂(3x2y2−4y)=6xy2.Differentiate fx in x again; −4y is constant in .x.
.fxy=∂y∂(3x2y2−4y)=6x2y−4.Differentiate fx in .y.
.fyx=∂x∂(2x3y−4x+3y2)=6x2y−4.Differentiate fy in :x: a different calculation that gives the same result, because the second partials of a polynomial are continuous.
.fyy=∂y∂(2x3y−4x+3y2)=2x3+6y.Differentiate fy in .y.
,∇2f=(6xy26x2y−46x2y−42x3+6y), and fxy=fyx=6x2y−4Row i is the gradient of ∂f/∂xi written as a row, so the Hessian is the Jacobian of ;∇f; symmetry means the off-diagonal entries need computing only once.
Problem 5
Find every critical point of f(x,y)=x3+x2+3xy+y2 and classify each as a local minimum, local maximum or saddle.
fx=3x2+2x+3y and .fy=3x+2y.First partials.
fy=0 gives .y=−23x.Solve the linear equation first, then substitute into the other.
,fx=3x2+2x−29x=x(3x−25)=0, so x=0 or .x=65.Substitute ;y=−23x; a product is zero when a factor is.
The critical points are (0,0) and .(65,−45).y=−23x at each root.
.∇2f=(6x+2332).,fxx=6x+2,,fxy=fyx=3,.fyy=2.
At :(0,0):∇2f=(2332) with ;det=4−9=−5; its eigenvalues are 5 and ,−1, along (1,1)⊤ and .(1,−1)⊤.A negative determinant is a negative product of the two eigenvalues, so they have opposite signs; f curves up along one eigenvector and down along the other. Along ,(1,−1)⊤,,f(t,−t)=t3−t2, which is below f(0,0)=0 for small .t=0.
At :(65,−45):,fxx=6⋅65+2=7, and ∇2f=(7332) with .det=14−9=5.A positive determinant means the eigenvalues share a sign, and the trace ,7+2=9, their sum, makes that sign positive: the Hessian is positive definite.
(0,0) is a saddle: ,det=−5<0, eigenvalues 5 and .−1.(65,−45) is a strict local minimum: det=5>0 and fxx=7>0The minimum is only local: f(x,0)=x3+x2 goes to −∞ as .x→−∞.
Problem 6
Let f(x)=21x⊤Ax−b⊤x with ,A∈Rn×n, not necessarily symmetric, and .b∈Rn. Compute ∇f and .∇2f. Assuming 21(A+A⊤) is invertible, take one Newton step from an arbitrary .x0. Where does it land?
∇(x⊤Ax)=(A+A⊤)x and .∇(b⊤x)=b.Problems 4 and 3 of the matrix-calculus page: x appears on both sides of ,A, so both A and A⊤ contribute.
.∇f=21(A+A⊤)x−b.The gradient is linear: scale the first term by 21 and subtract the second.
.∇2f=21(A+A⊤).The Hessian is the Jacobian of ,∇f, and the Jacobian of Mx is ;M; the constant b drops out. It is symmetric, as a Hessian must be, even when A is not.
Write .H=21(A+A⊤). Then .x⊤Ax=x⊤Hx.A=H+K with ,K=21(A−A⊤), and x⊤Kx is a scalar equal to its own transpose ,x⊤K⊤x=−x⊤Kx, so it is .0. Only the symmetric part of A is ever seen by .f.
.x1=x0−H−1(Hx0−b)=x0−x0+H−1b=H−1b.Newton's step with ∇f(x0)=Hx0−b and ;∇2f=H;.H−1H=I.
.∇f(x1)=HH−1b−b=0.So x1 is the critical point, reached in one step from any :x0: Newton's step jumps to the critical point of the second-order Taylor model, and for a quadratic that model is f itself.
,∇f=21(A+A⊤)x−b,;∇2f=21(A+A⊤); Newton's step gives x1=(21(A+A⊤))−1b from any ,x0, which is A−1b when A is symmetricWhen 21(A+A⊤) is also positive definite, f is strictly convex and x1 is its global minimum.
Problem 7
Let f(x)=log∑j=1nexj (log-sum-exp). Compute ∇f and .∇2f.
Let ,Z=∑jexj, so f=logZ and .∂Z/∂xi=exi.Only the j=i term of the sum depends on .xi.
,∂xi∂f=Z1⋅exi=si, where .si=exi/∑jexj.Chain rule: the derivative of log at Z is ,1/Z, times .∂Z/∂xi. The vector s is the softmax of .x.
,∂xk∂si=Z2δikexiZ−exiexk=δiksi−sisk, with δik=1 if i=k and 0 otherwise.Quotient rule on :exi/Z: the numerator depends on xk only when ,k=i, and .∂Z/∂xk=exk.
.∇2f=diag(s)−ss⊤.Entry (i,k) of the Hessian is ;∂si/∂xk; the δiksi terms form the diagonal matrix diag(s) and the sisk terms form the outer product .ss⊤.
∇f=s and ,∇2f=diag(s)−ss⊤, where si=exi/∑jexjTwo checks: the Hessian is symmetric, and ∇2f1=s−s(s⊤1)=0 because ,∑isi=1, matching ,f(x+c1)=f(x)+c, which is linear along 1 and so has no curvature there.
Problem 8
Write the second-order Taylor polynomial of a function f of n variables around ,x, then apply it to log-sum-exp from Problem 7 around .x=0. Express the result in terms of ,h∈Rn, the displacement from .0.
Let ;g(t)=f(x+th); then g′(0)=∇f(x)⊤h and .g′′(0)=h⊤∇2f(x)h.Chain rule: ,g′(t)=∇f(x+th)⊤h=∑ihi∂f/∂xi, and differentiating each ∂f/∂xi once more along h gives .∑i,jhihj∂2f/∂xi∂xj.
.f(x+h)=g(1)≈g(0)+g′(0)+21g′′(0)=f(x)+∇f(x)⊤h+21h⊤∇2f(x)h.The one-variable Taylor polynomial of g at ,0, evaluated at ;t=1; the error is third order in .∥h∥.
At :x=0:f(0)=logn and .s=n11.Every ,e0=1, so the sum is n and each softmax entry is .1/n.
.∇f(0)⊤h=n1∑ihi.∇f=s (Problem 7).
,∇2f(0)=n1I−n2111⊤, so .h⊤∇2f(0)h=n1∑ihi2−n21(∑ihi)2.∇2f=diag(s)−ss⊤ (Problem 7) with ,s=n11, and .h⊤1=∑ihi.
log∑iehi≈logn+n1∑ihi+2n1∑ihi2−2n21(∑ihi)2Steps 2 to 5. Read with ,hˉ=n1∑ihi, it is :logn+hˉ+21⋅n1∑i(hi−hˉ)2: the mean of the hi plus half their variance, so near 0 log-sum-exp sits above the mean by an amount set by the spread.
Problem 9
Logistic regression has data X∈RN×d with row n equal to ,x(n)⊤, labels ,yn∈{0,1}, weights ,w∈Rd, probabilities p=σ(Xw) with ,pn=σ(x(n)⊤w), and the mean binary cross-entropy ,L(w), whose gradient is ∇wL=N1X⊤(p−y) (the regression page derives it). Show that ∇w2L=N1X⊤DX with ,D=diag(p⊙(1−p)), and that it is positive semidefinite.
.∇wL=N1∑n(pn−yn)x(n).X⊤ has columns ,x(n), so X⊤(p−y) is the sum of those columns weighted by the entries of .p−y.
,∂w∂pn=pn(1−pn)x(n)⊤, a 1×d row.Chain rule: σ′=σ(1−σ) evaluated at ,x(n)⊤w, times the row derivative of ,x(n)⊤w, which is .x(n)⊤.
The Jacobian of (pn−yn)x(n) with respect to w is ,x(n)∂w∂pn=pn(1−pn)x(n)x(n)⊤, a d×d matrix.x(n) and yn do not depend on ;w; only the scalar pn does, and a constant column times a scalar's row derivative is a column times a row.
.∇w2L=N1∑npn(1−pn)x(n)x(n)⊤=N1X⊤DX.The Hessian is the Jacobian of the gradient, so sum step 3 over ;n; a weighted sum of outer products of the rows of X is ,X⊤diag(weights)X, since .X⊤DX=∑nDnnx(n)x(n)⊤.
For any :v∈Rd:.v⊤∇w2Lv=N1∑npn(1−pn)(x(n)⊤v)2.,v⊤x(n)x(n)⊤v=(x(n)⊤v)2, a square.
∇w2L=N1X⊤DX with ,D=diag(p⊙(1−p)), and v⊤∇w2Lv=N1∑npn(1−pn)(x(n)⊤v)2≥0 for every ,v, so it is positive semidefiniteEvery pn lies strictly between 0 and 1, so every weight pn(1−pn) is positive and every term is .≥0. A PSD Hessian at every w makes L convex, so any critical point is a global minimum.
Problem 10
For which real numbers c is f(x,y)=x2+cxy+y2+ex convex on ?R2?
fx=2x+cy+ex and .fy=cx+2y.First partials.
.∇2f=(2+excc2).,fxx=2+ex,,fxy=fyx=c,.fyy=2. It depends on x but not on .y.
A symmetric 2×2 matrix with positive diagonal is PSD exactly when its determinant is .≥0.The determinant is the product of the eigenvalues and the trace their sum; a positive trace rules out two negative eigenvalues, so det≥0 leaves both ,≥0, while det<0 forces one negative.
.det∇2f=2(2+ex)−c2=4+2ex−c2.Product of the diagonal minus the square of the off-diagonal entry.
If ,c2≤4, then det∇2f≥2ex>0 at every point, so the Hessian is positive definite everywhere and f is convex.,4−c2≥0, and ex>0 for every .x.
If ,c2>4, then det∇2f<0 wherever ,ex<21(c2−4), so the Hessian has a negative eigenvalue there and f is not convex.ex takes every positive value, so such x always exist; at those points f curves down along an eigenvector, which a convex function never does.
,∇2f=(2+excc2), and f is convex exactly when ∣c∣≤2The boundary case ∣c∣=2 is still convex because ex keeps the determinant positive; without the ex term, x2±2xy+y2=(x±y)2 would be only just convex, flat along one line.
Where this goes wrong
1. Directional derivative along a non-unit vector
The formula ∇f⊤u is easy to remember and easy to use with whatever vector the problem gives.
∇f(1,0)=(1,1)⊤ for f=xey+y2Right so far: this is step 2 of Problem 2.
“The directional derivative is the gradient dotted with the direction.”The shortcut that causes the mistake: true only when the direction is a unit vector.
Dvf(1,0)=(1,1)(3,4)⊤=7∇f⊤v scales with the length of :v: it is the rate along v per unit of the parameter t in ,f(x+tv), moving at speed .∥v∥=5. The rate per unit distance divides by that: 7/5 (Problem 2). The giveaway is that 7 exceeds ,∥∇f(1,0)∥=2, the steepest rate in any direction (Problem 3).
2. Calling a saddle a minimum because fₓₓ > 0
In one variable a positive second derivative at a critical point settles it, and fxx looks like that second derivative.
At ,(0,0),f=x3+x2+3xy+y2 has ∇2f=(2332) and det=−5Right so far: these are the numbers in step 6 of Problem 5.
“f′′>0 means a minimum; the determinant only has to be nonzero for the test to apply.”The analogy that causes the mistake: the one-variable test, with the determinant treated as a validity check rather than a sign to read.
fxx(0,0)=2>0 and ,det=0, so (0,0) is a local minimumThe sign of the determinant is the content of the test: det is the product of the eigenvalues, and −5 means one is negative. fxx>0 is the curvature along the x axis only. Along ,(1,−1)⊤,f(t,−t)=t3−t2<0=f(0,0) for small ,t=0, so (0,0) is a saddle. The fxx sign matters only once .det>0.
3. Reading convexity off the diagonal of the Hessian
For a diagonal matrix the diagonal entries are the eigenvalues, and it is tempting to read every Hessian that way.
∇2f=(2+excc2) for f=x2+cxy+y2+exRight so far: this is step 2 of Problem 10.
“A matrix with positive entries on its diagonal is positive definite.”The shortcut that causes the mistake: true for diagonal matrices, where the diagonal entries are the eigenvalues.
Both diagonal entries, 2+ex and ,2, are positive for every ,c, so f is convex for every cThe diagonal entries are u⊤∇2fu for u along the axes, the curvature in two directions out of all of them. With c=3 at x=0 the Hessian is ,(3332), and along u=(1,−1)⊤ the curvature is .3−6+2=−1. The off-diagonal entries count; the determinant brings them in, and convexity needs ∣c∣≤2 (Problem 10).
4. Using A where its symmetric part belongs
The one-variable rule dxd(21ax2−bx)=ax−b suggests an answer that holds only for symmetric .A.
f(x)=21x⊤Ax−b⊤x with A not symmetricRight so far: the function is stated correctly, and nothing has been differentiated yet.
“21x⊤Ax−b⊤x is the matrix version of .21ax2−bx.”The analogy that causes the mistake.
,∇f=Ax−b, so Newton's step lands at A−1bThe two occurrences of x give Ax and ,A⊤x, so ∇f=21(A+A⊤)x−b (Problem 6), and f never sees the antisymmetric part of .A. With A=(2022) and ,b=(3,0)⊤,,A−1b=(23,0)⊤, where the true gradient is ,(0,23)⊤, not .0. The two answers agree only when ,A=A⊤, which is why examples built on symmetric matrices never expose it.
5. Gradient in the place of the row derivative
In one variable the linear term of Taylor's formula is ,f′(x)h, and ∇f is called “the derivative” often enough to be written in its place.
The quadratic term of the second-order Taylor polynomial is 21h⊤∇2f(x)hRight so far: it is ,1×1, as a term of a scalar must be (Problem 8).
“,f(x+h)≈f(x)+f′(x)h, and ∇f is the derivative.”The analogy that causes the mistake: the one-variable formula with ∇f substituted for .f′.
f(x+h)≈f(x)+∇f(x)h+21h⊤∇2f(x)h∇f(x) is n×1 and h is ,n×1, so the product does not exist. The linear term uses the row derivative ,∂f/∂x=∇f⊤, the 1×n Jacobian of a scalar: .∇f(x)⊤h. In code with 1-D arrays, g * h multiplies entry by entry and returns a vector instead of failing, so the “approximation” of a scalar comes out as a vector and nothing raises an error.