Practice / Calculus

Partial derivatives, gradients and Hessians

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.

  • ff maps Rn\mathbb{R}^n to R\mathbb{R}. The partial derivative ∂f/∂xi\partial f/\partial x_i differentiates in xix_i with every other variable held fixed. In two variables, fx=∂f/∂xf_x = \partial f/\partial x, and fxyf_{xy} means differentiate in xx, then in yy.
  • The conventions are those of the matrix-calculus page: vectors are columns, the row derivative ∂f/∂x\partial f/\partial x is 1×n1 \times n, and the gradient is its transpose, ∇f=(∂f/∂x)⊤\nabla f = (\partial f/\partial x)^\top, an n×1n \times 1 column. A point in the plane is written (x,y)(x, y); a vector is written as a column, such as (4,8)⊤(4, 8)^\top.
  • The Hessian ∇2f\nabla^2 f is the n×nn \times n matrix with (∇2f)ij=∂2f/∂xi ∂xj(\nabla^2 f)_{ij} = \partial^2 f/\partial x_i\,\partial x_j; it is the Jacobian of ∇f\nabla f. When the second partials are continuous, which holds for every function on this page, it is symmetric: fxy=fyxf_{xy} = f_{yx}.
  • For a unit vector uu, the directional derivative is Duf=ddtf(x+tu)∣t=0=∇f(x)⊤uD_u f = \frac{d}{dt}f(x + tu)\big|_{t=0} = \nabla f(x)^\top u, the rate of change of ff per unit distance along uu.
  • A symmetric matrix HH is positive semidefinite (PSD) if v⊤Hv≥0v^\top H v \ge 0 for every vv, and positive definite if v⊤Hv>0v^\top H v > 0 for every v≠0v \neq 0; equivalently, all its eigenvalues are ≥0\ge 0, or all >0> 0.
  • A critical point has ∇f=0\nabla 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×22 \times 2, det⁡∇2f\det \nabla^2 f is the product of the two eigenvalues.
  • A twice-differentiable ff on Rn\mathbb{R}^n is convex exactly when ∇2f(x)\nabla^2 f(x) is PSD at every xx.
  • Newton's method steps from xkx_k to xk+1=xk−(∇2f(xk))−1∇f(xk)x_{k+1} = x_k - (\nabla^2 f(x_k))^{-1}\nabla f(x_k). σ(t)=1/(1+e−t)\sigma(t) = 1/(1+e^{-t}) is the logistic sigmoid, with σ′=σ(1−σ)\sigma' = \sigma(1-\sigma) (the differentiation-rules page).

Builds on: Differentiation rules: chain, product, quotient

Problems

  1. ·

    Let f(x,y)=x2y3+ye2xf(x, y) = x^2y^3 + ye^{2x}. Compute ∂f/∂x\partial f/\partial x, ∂f/∂y\partial f/\partial y and ∇f(0,1)\nabla f(0, 1).

  2. ·

    Let f(x,y)=xey+y2f(x, y) = xe^y + y^2. Find the rate of change of ff at (1,0)(1, 0) in the direction of v=(3,4)⊤v = (3, 4)^\top.

  3. ··

    Let f(x,y)=x2+4y2f(x, y) = x^2 + 4y^2. At (2,1)(2, 1), find the unit direction of steepest ascent and the rate of increase along it. Then show that ∇f(2,1)\nabla f(2, 1) is perpendicular to the level curve of ff through (2,1)(2, 1).

  4. ·

    Let f(x,y)=x3y2−4xy+y3f(x, y) = x^3y^2 - 4xy + y^3. Compute the Hessian ∇2f\nabla^2 f and confirm that fxy=fyxf_{xy} = f_{yx}.

  5. ··

    Find every critical point of f(x,y)=x3+x2+3xy+y2f(x, y) = x^3 + x^2 + 3xy + y^2 and classify each as a local minimum, local maximum or saddle.

  6. ··

    Let f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with A∈Rn×nA \in \mathbb{R}^{n \times n}, not necessarily symmetric, and b∈Rnb \in \mathbb{R}^n. Compute ∇f\nabla f and ∇2f\nabla^2 f. Assuming 12(A+A⊤)\tfrac12(A + A^\top) is invertible, take one Newton step from an arbitrary x0x_0. Where does it land?

  7. ···

    Let f(x)=log⁡∑j=1nexjf(x) = \log\sum_{j=1}^n e^{x_j} (log-sum-exp). Compute ∇f\nabla f and ∇2f\nabla^2 f.

  8. ··

    Write the second-order Taylor polynomial of a function ff of nn variables around xx, then apply it to log-sum-exp from Problem 7 around x=0x = 0. Express the result in terms of h∈Rnh \in \mathbb{R}^n, the displacement from 00.

  9. ···

    Logistic regression has data X∈RN×dX \in \mathbb{R}^{N \times d} with row nn equal to x(n)⊤x^{(n)\top}, labels yn∈{0,1}y_n \in \{0, 1\}, weights w∈Rdw \in \mathbb{R}^d, probabilities p=σ(Xw)p = \sigma(Xw) with pn=σ(x(n)⊤w)p_n = \sigma(x^{(n)\top}w), and the mean binary cross-entropy L(w)L(w), whose gradient is ∇wL=1NX⊤(p−y)\nabla_w L = \tfrac1N X^\top(p - y) (the regression page derives it). Show that ∇w2L=1NX⊤DX\nabla_w^2 L = \tfrac1N X^\top D X with D=diag⁡(p⊙(1−p))D = \operatorname{diag}(p \odot (1 - p)), and that it is positive semidefinite.

  10. ··

    For which real numbers cc is f(x,y)=x2+cxy+y2+exf(x, y) = x^2 + cxy + y^2 + e^x convex on R2\mathbb{R}^2?

Worked solutions

Problem 1

Let f(x,y)=x2y3+ye2xf(x, y) = x^2y^3 + ye^{2x}. Compute ∂f/∂x\partial f/\partial x, ∂f/∂y\partial f/\partial y and ∇f(0,1)\nabla f(0, 1).

  1. ∂∂x(x2y3)=2xy3\dfrac{\partial}{\partial x}(x^2y^3) = 2xy^3 and ∂∂x(ye2x)=2ye2x\dfrac{\partial}{\partial x}(ye^{2x}) = 2ye^{2x}.With yy held fixed, y3y^3 and yy are constant factors, and (e2x)′=2e2x(e^{2x})' = 2e^{2x} by the chain rule.
  2. ∂∂y(x2y3)=3x2y2\dfrac{\partial}{\partial y}(x^2y^3) = 3x^2y^2 and ∂∂y(ye2x)=e2x\dfrac{\partial}{\partial y}(ye^{2x}) = e^{2x}.Now xx is held fixed, so x2x^2 and e2xe^{2x} are the constant factors.
  3. ∇f(0,1)=(0+2⋅1⋅e0,  0+e0)⊤=(2,1)⊤\nabla f(0, 1) = (0 + 2\cdot 1\cdot e^0,\; 0 + e^0)^\top = (2, 1)^\top.The gradient stacks the two partials as a column; evaluate each at x=0x = 0, y=1y = 1.
  4. ∂f/∂x=2xy3+2ye2x\partial f/\partial x = 2xy^3 + 2ye^{2x}, ∂f/∂y=3x2y2+e2x\partial f/\partial y = 3x^2y^2 + e^{2x}, ∇f(0,1)=(2,1)⊤\nabla f(0, 1) = (2, 1)^\top

Problem 2

Let f(x,y)=xey+y2f(x, y) = xe^y + y^2. Find the rate of change of ff at (1,0)(1, 0) in the direction of v=(3,4)⊤v = (3, 4)^\top.

  1. ∇f=(ey,  xey+2y)⊤\nabla f = (e^y,\; xe^y + 2y)^\top.Partial in xx with yy fixed, then in yy with xx fixed.
  2. ∇f(1,0)=(1,1)⊤\nabla f(1, 0) = (1, 1)^\top.e0=1e^0 = 1 and 2y=02y = 0 at y=0y = 0.
  3. ∥v∥=32+42=5\|v\| = \sqrt{3^2 + 4^2} = 5, so u=v/∥v∥=(35,45)⊤u = v/\|v\| = (\tfrac35, \tfrac45)^\top.A rate per unit distance needs a unit vector; vv only names the direction.
  4. Duf(1,0)=∇f(1,0)⊤u=35+45D_u f(1, 0) = \nabla f(1, 0)^\top u = \tfrac35 + \tfrac45.The directional derivative is the gradient's inner product with the unit direction.
  5. Duf(1,0)=75D_u f(1, 0) = \tfrac75, with u=(35,45)⊤u = (\tfrac35, \tfrac45)^\topSanity check: it cannot exceed ∥∇f(1,0)∥=2≈1.41\|\nabla f(1,0)\| = \sqrt2 \approx 1.41 (Problem 3), and 75=1.4\tfrac75 = 1.4, because vv points almost along (1,1)⊤(1, 1)^\top.

Problem 3

Let f(x,y)=x2+4y2f(x, y) = x^2 + 4y^2. At (2,1)(2, 1), find the unit direction of steepest ascent and the rate of increase along it. Then show that ∇f(2,1)\nabla f(2, 1) is perpendicular to the level curve of ff through (2,1)(2, 1).

  1. ∇f=(2x,8y)⊤\nabla f = (2x, 8y)^\top, so ∇f(2,1)=(4,8)⊤\nabla f(2, 1) = (4, 8)^\top.The two partials of a sum of one-variable terms.
  2. For a unit uu, Duf=∇f⊤u=∥∇f∥cos⁡θD_u f = \nabla f^\top u = \|\nabla f\|\cos\theta, with θ\theta the angle between ∇f\nabla f and uu.The inner product of two vectors is the product of their lengths and the cosine of the angle between them, and ∥u∥=1\|u\| = 1.
  3. DufD_u f is largest when θ=0\theta = 0: u=∇f/∥∇f∥=(4,8)⊤/80=(1,2)⊤/5u = \nabla f/\|\nabla f\| = (4, 8)^\top/\sqrt{80} = (1, 2)^\top/\sqrt5, at rate ∥∇f∥=80=45\|\nabla f\| = \sqrt{80} = 4\sqrt5.cos⁡θ≤1\cos\theta \le 1, with equality only when uu points along the gradient.
  4. The level curve through (2,1)(2, 1) is x2+4y2=8x^2 + 4y^2 = 8.f(2,1)=4+4=8f(2, 1) = 4 + 4 = 8, and a level curve is the set where ff keeps that value.
  5. Let r(t)r(t) be any curve on it with r(0)=(2,1)r(0) = (2, 1). Then f(r(t))=8f(r(t)) = 8 for all tt, so ∇f(2,1)⊤r′(0)=0\nabla f(2, 1)^\top r'(0) = 0.Chain rule: the derivative of f(r(t))f(r(t)) is ∇f⊤r′(t)\nabla f^\top r'(t), and the derivative of a constant is 00. Every tangent to the level curve is therefore perpendicular to the gradient.
  6. Concretely, r(t)=(22cos⁡t,  2sin⁡t)r(t) = (2\sqrt2\cos t,\; \sqrt2\sin t) traces the curve and passes through (2,1)(2, 1) at t=π/4t = \pi/4, where r′(π/4)=(−22sin⁡π4,  2cos⁡π4)⊤=(−2,1)⊤r'(\pi/4) = (-2\sqrt2\sin\tfrac\pi4,\; \sqrt2\cos\tfrac\pi4)^\top = (-2, 1)^\top.(22cos⁡t)2+4(2sin⁡t)2=8(2\sqrt2\cos t)^2 + 4(\sqrt2\sin t)^2 = 8, and sin⁡π4=cos⁡π4=1/2\sin\tfrac\pi4 = \cos\tfrac\pi4 = 1/\sqrt2.
  7. ∇f(2,1)=(4,8)⊤\nabla f(2, 1) = (4, 8)^\top; steepest ascent along (1,2)⊤/5(1, 2)^\top/\sqrt5 at rate 454\sqrt5; the level curve x2+4y2=8x^2 + 4y^2 = 8 has tangent (−2,1)⊤(-2, 1)^\top there, and (4,8) (−2,1)⊤=−8+8=0(4, 8)\,(-2, 1)^\top = -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-\nabla f for this reason.

Problem 4

Let f(x,y)=x3y2−4xy+y3f(x, y) = x^3y^2 - 4xy + y^3. Compute the Hessian ∇2f\nabla^2 f and confirm that fxy=fyxf_{xy} = f_{yx}.

  1. fx=3x2y2−4yf_x = 3x^2y^2 - 4y and fy=2x3y−4x+3y2f_y = 2x^3y - 4x + 3y^2.First partials, each with the other variable held fixed.
  2. fxx=∂∂x(3x2y2−4y)=6xy2f_{xx} = \dfrac{\partial}{\partial x}(3x^2y^2 - 4y) = 6xy^2.Differentiate fxf_x in xx again; −4y-4y is constant in xx.
  3. fxy=∂∂y(3x2y2−4y)=6x2y−4f_{xy} = \dfrac{\partial}{\partial y}(3x^2y^2 - 4y) = 6x^2y - 4.Differentiate fxf_x in yy.
  4. fyx=∂∂x(2x3y−4x+3y2)=6x2y−4f_{yx} = \dfrac{\partial}{\partial x}(2x^3y - 4x + 3y^2) = 6x^2y - 4.Differentiate fyf_y in xx: a different calculation that gives the same result, because the second partials of a polynomial are continuous.
  5. fyy=∂∂y(2x3y−4x+3y2)=2x3+6yf_{yy} = \dfrac{\partial}{\partial y}(2x^3y - 4x + 3y^2) = 2x^3 + 6y.Differentiate fyf_y in yy.
  6. ∇2f=(6xy26x2y−46x2y−42x3+6y)\nabla^2 f = \begin{pmatrix} 6xy^2 & 6x^2y - 4 \\ 6x^2y - 4 & 2x^3 + 6y \end{pmatrix}, and fxy=fyx=6x2y−4f_{xy} = f_{yx} = 6x^2y - 4Row ii is the gradient of ∂f/∂xi\partial f/\partial x_i written as a row, so the Hessian is the Jacobian of ∇f\nabla f; symmetry means the off-diagonal entries need computing only once.

Problem 5

Find every critical point of f(x,y)=x3+x2+3xy+y2f(x, y) = x^3 + x^2 + 3xy + y^2 and classify each as a local minimum, local maximum or saddle.

  1. fx=3x2+2x+3yf_x = 3x^2 + 2x + 3y and fy=3x+2yf_y = 3x + 2y.First partials.
  2. fy=0f_y = 0 gives y=−32xy = -\tfrac32x.Solve the linear equation first, then substitute into the other.
  3. fx=3x2+2x−92x=x(3x−52)=0f_x = 3x^2 + 2x - \tfrac92x = x\big(3x - \tfrac52\big) = 0, so x=0x = 0 or x=56x = \tfrac56.Substitute y=−32xy = -\tfrac32 x; a product is zero when a factor is.
  4. The critical points are (0,0)(0, 0) and (56,−54)(\tfrac56, -\tfrac54).y=−32xy = -\tfrac32 x at each root.
  5. ∇2f=(6x+2332)\nabla^2 f = \begin{pmatrix} 6x + 2 & 3 \\ 3 & 2 \end{pmatrix}.fxx=6x+2f_{xx} = 6x + 2, fxy=fyx=3f_{xy} = f_{yx} = 3, fyy=2f_{yy} = 2.
  6. At (0,0)(0, 0): ∇2f=(2332)\nabla^2 f = \begin{pmatrix} 2 & 3 \\ 3 & 2 \end{pmatrix} with det⁡=4−9=−5\det = 4 - 9 = -5; its eigenvalues are 55 and −1-1, along (1,1)⊤(1, 1)^\top and (1,−1)⊤(1, -1)^\top.A negative determinant is a negative product of the two eigenvalues, so they have opposite signs; ff curves up along one eigenvector and down along the other. Along (1,−1)⊤(1, -1)^\top, f(t,−t)=t3−t2f(t, -t) = t^3 - t^2, which is below f(0,0)=0f(0,0) = 0 for small t≠0t \neq 0.
  7. At (56,−54)(\tfrac56, -\tfrac54): fxx=6⋅56+2=7f_{xx} = 6\cdot\tfrac56 + 2 = 7, and ∇2f=(7332)\nabla^2 f = \begin{pmatrix} 7 & 3 \\ 3 & 2 \end{pmatrix} with det⁡=14−9=5\det = 14 - 9 = 5.A positive determinant means the eigenvalues share a sign, and the trace 7+2=97 + 2 = 9, their sum, makes that sign positive: the Hessian is positive definite.
  8. (0,0)(0, 0) is a saddle: det⁡=−5<0\det = -5 < 0, eigenvalues 55 and −1-1. (56,−54)(\tfrac56, -\tfrac54) is a strict local minimum: det⁡=5>0\det = 5 > 0 and fxx=7>0f_{xx} = 7 > 0The minimum is only local: f(x,0)=x3+x2f(x, 0) = x^3 + x^2 goes to −∞-\infty as x→−∞x \to -\infty.

Problem 6

Let f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with A∈Rn×nA \in \mathbb{R}^{n \times n}, not necessarily symmetric, and b∈Rnb \in \mathbb{R}^n. Compute ∇f\nabla f and ∇2f\nabla^2 f. Assuming 12(A+A⊤)\tfrac12(A + A^\top) is invertible, take one Newton step from an arbitrary x0x_0. Where does it land?

  1. ∇(x⊤Ax)=(A+A⊤)x\nabla(x^\top A x) = (A + A^\top)x and ∇(b⊤x)=b\nabla(b^\top x) = b.Problems 4 and 3 of the matrix-calculus page: xx appears on both sides of AA, so both AA and A⊤A^\top contribute.
  2. ∇f=12(A+A⊤)x−b\nabla f = \tfrac12(A + A^\top)x - b.The gradient is linear: scale the first term by 12\tfrac12 and subtract the second.
  3. ∇2f=12(A+A⊤)\nabla^2 f = \tfrac12(A + A^\top).The Hessian is the Jacobian of ∇f\nabla f, and the Jacobian of MxMx is MM; the constant bb drops out. It is symmetric, as a Hessian must be, even when AA is not.
  4. Write H=12(A+A⊤)H = \tfrac12(A + A^\top). Then x⊤Ax=x⊤Hxx^\top A x = x^\top H x.A=H+KA = H + K with K=12(A−A⊤)K = \tfrac12(A - A^\top), and x⊤Kxx^\top K x is a scalar equal to its own transpose x⊤K⊤x=−x⊤Kxx^\top K^\top x = -x^\top K x, so it is 00. Only the symmetric part of AA is ever seen by ff.
  5. x1=x0−H−1(Hx0−b)=x0−x0+H−1b=H−1bx_1 = x_0 - H^{-1}(Hx_0 - b) = x_0 - x_0 + H^{-1}b = H^{-1}b.Newton's step with ∇f(x0)=Hx0−b\nabla f(x_0) = Hx_0 - b and ∇2f=H\nabla^2 f = H; H−1H=IH^{-1}H = I.
  6. ∇f(x1)=HH−1b−b=0\nabla f(x_1) = HH^{-1}b - b = 0.So x1x_1 is the critical point, reached in one step from any x0x_0: Newton's step jumps to the critical point of the second-order Taylor model, and for a quadratic that model is ff itself.
  7. ∇f=12(A+A⊤)x−b\nabla f = \tfrac12(A + A^\top)x - b, ∇2f=12(A+A⊤)\nabla^2 f = \tfrac12(A + A^\top); Newton's step gives x1=(12(A+A⊤))−1bx_1 = \big(\tfrac12(A + A^\top)\big)^{-1}b from any x0x_0, which is A−1bA^{-1}b when AA is symmetricWhen 12(A+A⊤)\tfrac12(A + A^\top) is also positive definite, ff is strictly convex and x1x_1 is its global minimum.

Problem 7

Let f(x)=log⁡∑j=1nexjf(x) = \log\sum_{j=1}^n e^{x_j} (log-sum-exp). Compute ∇f\nabla f and ∇2f\nabla^2 f.

  1. Let Z=∑jexjZ = \sum_j e^{x_j}, so f=log⁡Zf = \log Z and ∂Z/∂xi=exi\partial Z/\partial x_i = e^{x_i}.Only the j=ij = i term of the sum depends on xix_i.
  2. ∂f∂xi=1Z⋅exi=si\dfrac{\partial f}{\partial x_i} = \dfrac{1}{Z}\cdot e^{x_i} = s_i, where si=exi/∑jexjs_i = e^{x_i}/\sum_j e^{x_j}.Chain rule: the derivative of log⁡\log at ZZ is 1/Z1/Z, times ∂Z/∂xi\partial Z/\partial x_i. The vector ss is the softmax of xx.
  3. ∂si∂xk=δikexiZ−exiexkZ2=δiksi−sisk\dfrac{\partial s_i}{\partial x_k} = \dfrac{\delta_{ik}e^{x_i}Z - e^{x_i}e^{x_k}}{Z^2} = \delta_{ik}s_i - s_is_k, with δik=1\delta_{ik} = 1 if i=ki = k and 00 otherwise.Quotient rule on exi/Ze^{x_i}/Z: the numerator depends on xkx_k only when k=ik = i, and ∂Z/∂xk=exk\partial Z/\partial x_k = e^{x_k}.
  4. ∇2f=diag⁡(s)−ss⊤\nabla^2 f = \operatorname{diag}(s) - ss^\top.Entry (i,k)(i, k) of the Hessian is ∂si/∂xk\partial s_i/\partial x_k; the δiksi\delta_{ik}s_i terms form the diagonal matrix diag⁡(s)\operatorname{diag}(s) and the sisks_is_k terms form the outer product ss⊤ss^\top.
  5. ∇f=s\nabla f = s and ∇2f=diag⁡(s)−ss⊤\nabla^2 f = \operatorname{diag}(s) - ss^\top, where si=exi/∑jexjs_i = e^{x_i}/\sum_j e^{x_j}Two checks: the Hessian is symmetric, and ∇2f 1=s−s(s⊤1)=0\nabla^2 f\,\mathbf 1 = s - s(s^\top\mathbf 1) = 0 because ∑isi=1\sum_i s_i = 1, matching f(x+c1)=f(x)+cf(x + c\mathbf 1) = f(x) + c, which is linear along 1\mathbf 1 and so has no curvature there.

Problem 8

Write the second-order Taylor polynomial of a function ff of nn variables around xx, then apply it to log-sum-exp from Problem 7 around x=0x = 0. Express the result in terms of h∈Rnh \in \mathbb{R}^n, the displacement from 00.

  1. Let g(t)=f(x+th)g(t) = f(x + th); then g′(0)=∇f(x)⊤hg'(0) = \nabla f(x)^\top h and g′′(0)=h⊤∇2f(x) hg''(0) = h^\top \nabla^2 f(x)\,h.Chain rule: g′(t)=∇f(x+th)⊤h=∑ihi ∂f/∂xig'(t) = \nabla f(x + th)^\top h = \sum_i h_i\,\partial f/\partial x_i, and differentiating each ∂f/∂xi\partial f/\partial x_i once more along hh gives ∑i,jhihj ∂2f/∂xi ∂xj\sum_{i,j} h_i h_j\,\partial^2 f/\partial x_i\,\partial x_j.
  2. f(x+h)=g(1)≈g(0)+g′(0)+12g′′(0)=f(x)+∇f(x)⊤h+12h⊤∇2f(x) hf(x + h) = g(1) \approx g(0) + g'(0) + \tfrac12 g''(0) = f(x) + \nabla f(x)^\top h + \tfrac12 h^\top \nabla^2 f(x)\,h.The one-variable Taylor polynomial of gg at 00, evaluated at t=1t = 1; the error is third order in ∥h∥\|h\|.
  3. At x=0x = 0: f(0)=log⁡nf(0) = \log n and s=1n1s = \tfrac1n\mathbf 1.Every e0=1e^{0} = 1, so the sum is nn and each softmax entry is 1/n1/n.
  4. ∇f(0)⊤h=1n∑ihi\nabla f(0)^\top h = \tfrac1n\sum_i h_i.∇f=s\nabla f = s (Problem 7).
  5. ∇2f(0)=1nI−1n211⊤\nabla^2 f(0) = \tfrac1n I - \tfrac1{n^2}\mathbf 1\mathbf 1^\top, so h⊤∇2f(0) h=1n∑ihi2−1n2(∑ihi)2h^\top\nabla^2 f(0)\,h = \tfrac1n\sum_i h_i^2 - \tfrac1{n^2}\big(\sum_i h_i\big)^2.∇2f=diag⁡(s)−ss⊤\nabla^2 f = \operatorname{diag}(s) - ss^\top (Problem 7) with s=1n1s = \tfrac1n\mathbf 1, and h⊤1=∑ihih^\top\mathbf 1 = \sum_i h_i.
  6. log⁡∑iehi≈log⁡n+1n∑ihi+12n∑ihi2−12n2(∑ihi)2\log\sum_i e^{h_i} \approx \log n + \tfrac1n\sum_i h_i + \tfrac1{2n}\sum_i h_i^2 - \tfrac1{2n^2}\big(\sum_i h_i\big)^2Steps 2 to 5. Read with hˉ=1n∑ihi\bar h = \tfrac1n\sum_i h_i, it is log⁡n+hˉ+12⋅1n∑i(hi−hˉ)2\log n + \bar h + \tfrac12\cdot\tfrac1n\sum_i (h_i - \bar h)^2: the mean of the hih_i plus half their variance, so near 00 log-sum-exp sits above the mean by an amount set by the spread.

Problem 9

Logistic regression has data X∈RN×dX \in \mathbb{R}^{N \times d} with row nn equal to x(n)⊤x^{(n)\top}, labels yn∈{0,1}y_n \in \{0, 1\}, weights w∈Rdw \in \mathbb{R}^d, probabilities p=σ(Xw)p = \sigma(Xw) with pn=σ(x(n)⊤w)p_n = \sigma(x^{(n)\top}w), and the mean binary cross-entropy L(w)L(w), whose gradient is ∇wL=1NX⊤(p−y)\nabla_w L = \tfrac1N X^\top(p - y) (the regression page derives it). Show that ∇w2L=1NX⊤DX\nabla_w^2 L = \tfrac1N X^\top D X with D=diag⁡(p⊙(1−p))D = \operatorname{diag}(p \odot (1 - p)), and that it is positive semidefinite.

  1. ∇wL=1N∑n(pn−yn) x(n)\nabla_w L = \tfrac1N\sum_n (p_n - y_n)\,x^{(n)}.X⊤X^\top has columns x(n)x^{(n)}, so X⊤(p−y)X^\top(p - y) is the sum of those columns weighted by the entries of p−yp - y.
  2. ∂pn∂w=pn(1−pn) x(n)⊤\dfrac{\partial p_n}{\partial w} = p_n(1 - p_n)\,x^{(n)\top}, a 1×d1 \times d row.Chain rule: σ′=σ(1−σ)\sigma' = \sigma(1 - \sigma) evaluated at x(n)⊤wx^{(n)\top}w, times the row derivative of x(n)⊤wx^{(n)\top}w, which is x(n)⊤x^{(n)\top}.
  3. The Jacobian of (pn−yn) x(n)(p_n - y_n)\,x^{(n)} with respect to ww is x(n) ∂pn∂w=pn(1−pn) x(n)x(n)⊤x^{(n)}\,\dfrac{\partial p_n}{\partial w} = p_n(1 - p_n)\,x^{(n)}x^{(n)\top}, a d×dd \times d matrix.x(n)x^{(n)} and yny_n do not depend on ww; only the scalar pnp_n does, and a constant column times a scalar's row derivative is a column times a row.
  4. ∇w2L=1N∑npn(1−pn) x(n)x(n)⊤=1NX⊤DX\nabla_w^2 L = \tfrac1N\sum_n p_n(1 - p_n)\,x^{(n)}x^{(n)\top} = \tfrac1N X^\top D X.The Hessian is the Jacobian of the gradient, so sum step 3 over nn; a weighted sum of outer products of the rows of XX is X⊤diag⁡(weights)XX^\top \operatorname{diag}(\text{weights})X, since X⊤DX=∑nDnn x(n)x(n)⊤X^\top D X = \sum_n D_{nn}\,x^{(n)}x^{(n)\top}.
  5. For any v∈Rdv \in \mathbb{R}^d: v⊤∇w2L v=1N∑npn(1−pn) (x(n)⊤v)2v^\top\nabla_w^2 L\,v = \tfrac1N\sum_n p_n(1 - p_n)\,(x^{(n)\top}v)^2.v⊤x(n)x(n)⊤v=(x(n)⊤v)2v^\top x^{(n)}x^{(n)\top}v = (x^{(n)\top}v)^2, a square.
  6. ∇w2L=1NX⊤DX\nabla_w^2 L = \tfrac1N X^\top D X with D=diag⁡(p⊙(1−p))D = \operatorname{diag}(p \odot (1 - p)), and v⊤∇w2L v=1N∑npn(1−pn)(x(n)⊤v)2≥0v^\top \nabla_w^2 L\,v = \tfrac1N\sum_n p_n(1 - p_n)(x^{(n)\top}v)^2 \ge 0 for every vv, so it is positive semidefiniteEvery pnp_n lies strictly between 0 and 1, so every weight pn(1−pn)p_n(1 - p_n) is positive and every term is ≥0\ge 0. A PSD Hessian at every ww makes LL convex, so any critical point is a global minimum.

Problem 10

For which real numbers cc is f(x,y)=x2+cxy+y2+exf(x, y) = x^2 + cxy + y^2 + e^x convex on R2\mathbb{R}^2?

  1. fx=2x+cy+exf_x = 2x + cy + e^x and fy=cx+2yf_y = cx + 2y.First partials.
  2. ∇2f=(2+excc2)\nabla^2 f = \begin{pmatrix} 2 + e^x & c \\ c & 2 \end{pmatrix}.fxx=2+exf_{xx} = 2 + e^x, fxy=fyx=cf_{xy} = f_{yx} = c, fyy=2f_{yy} = 2. It depends on xx but not on yy.
  3. A symmetric 2×22 \times 2 matrix with positive diagonal is PSD exactly when its determinant is ≥0\ge 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\det \ge 0 leaves both ≥0\ge 0, while det⁡<0\det < 0 forces one negative.
  4. det⁡∇2f=2(2+ex)−c2=4+2ex−c2\det\nabla^2 f = 2(2 + e^x) - c^2 = 4 + 2e^x - c^2.Product of the diagonal minus the square of the off-diagonal entry.
  5. If c2≤4c^2 \le 4, then det⁡∇2f≥2ex>0\det\nabla^2 f \ge 2e^x > 0 at every point, so the Hessian is positive definite everywhere and ff is convex.4−c2≥04 - c^2 \ge 0, and ex>0e^x > 0 for every xx.
  6. If c2>4c^2 > 4, then det⁡∇2f<0\det\nabla^2 f < 0 wherever ex<12(c2−4)e^x < \tfrac12(c^2 - 4), so the Hessian has a negative eigenvalue there and ff is not convex.exe^x takes every positive value, so such xx always exist; at those points ff curves down along an eigenvector, which a convex function never does.
  7. ∇2f=(2+excc2)\nabla^2 f = \begin{pmatrix} 2 + e^x & c \\ c & 2 \end{pmatrix}, and ff is convex exactly when ∣c∣≤2\lvert c\rvert \le 2The boundary case ∣c∣=2\lvert c\rvert = 2 is still convex because exe^x keeps the determinant positive; without the exe^x term, x2±2xy+y2=(x±y)2x^2 \pm 2xy + y^2 = (x \pm 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\nabla f^\top u is easy to remember and easy to use with whatever vector the problem gives.

  1. ∇f(1,0)=(1,1)⊤\nabla f(1, 0) = (1, 1)^\top for f=xey+y2f = xe^y + y^2Right so far: this is step 2 of Problem 2.
  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.
  3. Dvf(1,0)=(1,1) (3,4)⊤=7D_v f(1, 0) = (1, 1)\,(3, 4)^\top = 7∇f⊤v\nabla f^\top v scales with the length of vv: it is the rate along vv per unit of the parameter tt in f(x+tv)f(x + tv), moving at speed ∥v∥=5\|v\| = 5. The rate per unit distance divides by that: 7/57/5 (Problem 2). The giveaway is that 77 exceeds ∥∇f(1,0)∥=2\|\nabla f(1,0)\| = \sqrt2, 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 fxxf_{xx} looks like that second derivative.

  1. At (0,0)(0, 0), f=x3+x2+3xy+y2f = x^3 + x^2 + 3xy + y^2 has ∇2f=(2332)\nabla^2 f = \begin{pmatrix} 2 & 3 \\ 3 & 2 \end{pmatrix} and det⁡=−5\det = -5Right so far: these are the numbers in step 6 of Problem 5.
  2. “f′′>0f'' > 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.
  3. fxx(0,0)=2>0f_{xx}(0, 0) = 2 > 0 and det⁡≠0\det \neq 0, so (0,0)(0, 0) is a local minimumThe sign of the determinant is the content of the test: det⁡\det is the product of the eigenvalues, and −5-5 means one is negative. fxx>0f_{xx} > 0 is the curvature along the xx axis only. Along (1,−1)⊤(1, -1)^\top, f(t,−t)=t3−t2<0=f(0,0)f(t, -t) = t^3 - t^2 < 0 = f(0, 0) for small t≠0t \neq 0, so (0,0)(0, 0) is a saddle. The fxxf_{xx} sign matters only once det⁡>0\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.

  1. ∇2f=(2+excc2)\nabla^2 f = \begin{pmatrix} 2 + e^x & c \\ c & 2 \end{pmatrix} for f=x2+cxy+y2+exf = x^2 + cxy + y^2 + e^xRight so far: this is step 2 of Problem 10.
  2. “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.
  3. Both diagonal entries, 2+ex2 + e^x and 22, are positive for every cc, so ff is convex for every ccThe diagonal entries are u⊤∇2f uu^\top\nabla^2 f\,u for uu along the axes, the curvature in two directions out of all of them. With c=3c = 3 at x=0x = 0 the Hessian is (3332)\begin{pmatrix} 3 & 3 \\ 3 & 2 \end{pmatrix}, and along u=(1,−1)⊤u = (1, -1)^\top the curvature is 3−6+2=−13 - 6 + 2 = -1. The off-diagonal entries count; the determinant brings them in, and convexity needs ∣c∣≤2\lvert c\rvert \le 2 (Problem 10).

4. Using A where its symmetric part belongs

The one-variable rule ddx(12ax2−bx)=ax−b\tfrac{d}{dx}(\tfrac12 ax^2 - bx) = ax - b suggests an answer that holds only for symmetric AA.

  1. f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with AA not symmetricRight so far: the function is stated correctly, and nothing has been differentiated yet.
  2. “12x⊤Ax−b⊤x\tfrac12 x^\top A x - b^\top x is the matrix version of 12ax2−bx\tfrac12 ax^2 - bx.”The analogy that causes the mistake.
  3. ∇f=Ax−b\nabla f = Ax - b, so Newton's step lands at A−1bA^{-1}bThe two occurrences of xx give AxAx and A⊤xA^\top x, so ∇f=12(A+A⊤)x−b\nabla f = \tfrac12(A + A^\top)x - b (Problem 6), and ff never sees the antisymmetric part of AA. With A=(2202)A = \begin{pmatrix} 2 & 2 \\ 0 & 2 \end{pmatrix} and b=(3,0)⊤b = (3, 0)^\top, A−1b=(32,0)⊤A^{-1}b = (\tfrac32, 0)^\top, where the true gradient is (0,32)⊤(0, \tfrac32)^\top, not 00. The two answers agree only when A=A⊤A = A^\top, 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) hf'(x)\,h, and ∇f\nabla f is called “the derivative” often enough to be written in its place.

  1. The quadratic term of the second-order Taylor polynomial is 12h⊤∇2f(x) h\tfrac12 h^\top\nabla^2 f(x)\,hRight so far: it is 1×11 \times 1, as a term of a scalar must be (Problem 8).
  2. “f(x+h)≈f(x)+f′(x) hf(x + h) \approx f(x) + f'(x)\,h, and ∇f\nabla f is the derivative.”The analogy that causes the mistake: the one-variable formula with ∇f\nabla f substituted for f′f'.
  3. f(x+h)≈f(x)+∇f(x) h+12h⊤∇2f(x) hf(x + h) \approx f(x) + \nabla f(x)\,h + \tfrac12 h^\top\nabla^2 f(x)\,h∇f(x)\nabla f(x) is n×1n \times 1 and hh is n×1n \times 1, so the product does not exist. The linear term uses the row derivative ∂f/∂x=∇f⊤\partial f/\partial x = \nabla f^\top, the 1×n1 \times n Jacobian of a scalar: ∇f(x)⊤h\nabla f(x)^\top 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.

Print this set: partial-derivatives-gradients-and-hessians.pdf (problems, answers, and worked solutions on separate pages).