Multivariable Calculus

Dive into Deep Learning · §34.2

Multivariable Calculus
partial derivatives · the gradient · directional derivatives · level sets · the Hessian · critical points · Lagrange multipliers

Joint Parameter Moves Across Millions of Weights

Motivation

  • From Slices to Joint Steps: Gradient descent updates all N = 10^6 weights simultaneously by \boldsymbol{\epsilon} \in \mathbb{R}^N.
  • One Vector for First-Order Slopes: The column gradient \nabla_{\mathbf{w}}\ell predicts any small joint move via \boldsymbol{\epsilon}\cdot\nabla_{\mathbf{w}}\ell and points along steepest ascent.
  • One Matrix for Surface Curvature: The symmetric Hessian \mathbf{H}_\ell captures directional bending and classifies minima vs. saddle points.

Partial Derivatives and the Log-Sum-Exp Benchmark

Coordinate Slices

  • Coordinate Slice Derivative: Freezing all coordinates except w_i defines the partial derivative \partial\ell/\partial w_i.
  • Log-Sum-Exp f(x, y) = \log(e^x + e^y): \frac{\partial f}{\partial x} = \frac{e^x}{e^x+e^y} and \frac{\partial f}{\partial y} = \frac{e^y}{e^x+e^y} form the probability vector \mathbf{p} = \operatorname{softmax}(x, y).
  • At (0, \log 2): f = \log 3 and \nabla f = (\tfrac{1}{3}, \tfrac{2}{3})^\top.

Multivariable Differentiability and the Gradient

First-Order Linear Model

  • Multivariable Differentiability: \ell(\mathbf{w} + \boldsymbol{\epsilon}) = \ell(\mathbf{w}) + \boldsymbol{\epsilon}\cdot\nabla_{\mathbf{w}}\ell(\mathbf{w}) + o(\|\boldsymbol{\epsilon}\|).
  • Why Partials Fix the Linear Map: Probing along \boldsymbol{\epsilon} = h\mathbf{e}_i forces \Lambda(\mathbf{e}_i) = \partial\ell/\partial w_i, so \Lambda(\boldsymbol{\epsilon}) = \sum_i \epsilon_i \frac{\partial\ell}{\partial w_i}.
  • Joint Prediction at (0, \log 2): Step \boldsymbol{\epsilon} = (0.01, -0.03) predicts \Delta f \approx \tfrac{1}{3}(0.01) + \tfrac{2}{3}(-0.03) = -0.01667.

Why Partial Derivatives Alone Do Not Suffice

Differentiability Pitfall

  • Pathological Ridge f(x, y) = \frac{2xy}{x^2+y^2} (f(0,0)=0): Vanishes identically along both axes (\partial_x f(0,0) = \partial_y f(0,0) = 0), yet equals 1 along the diagonal y = x \neq 0.
  • The C^1 Sufficient Condition: If all partial derivatives exist in a neighborhood of \mathbf{w} and are continuous at \mathbf{w}, \ell is guaranteed to be differentiable at \mathbf{w}.

Empirical Check: Linear Error Shrinks as O(‖ε‖²)

Numerical Verification

Differentiability promises only that the linear approximation error is o(\|\boldsymbol{\epsilon}\|), but for smooth functions like log-sum-exp, cutting the step \boldsymbol{\epsilon} = (0.01, -0.03) tenfold shrinks the gap a hundredfold:

gradient: [0.33333333 0.66666667]
|eps| 3.2e-02: linear 1.08194562, true 1.08212418, gap 1.786e-04
|eps| 3.2e-03: linear 1.09694562, true 1.09694740, gap 1.779e-06
|eps| 3.2e-04: linear 1.09844562, true 1.09844564, gap 1.778e-08

At \|\boldsymbol{\epsilon}\| = 3.2 \times 10^{-2}, the linear model misses by 1.786 \times 10^{-4}; shrinking \|\boldsymbol{\epsilon}\| by 10\times and 100\times drops the gap to 1.779 \times 10^{-6} and 1.778 \times 10^{-8}, demonstrating quadratic O(\|\boldsymbol{\epsilon}\|^2) error decay.

Directional Derivatives: Slopes Along Any Ray

Projections of the Gradient

  • Directional Derivative Formula: For any unit vector \mathbf{u} (\|\mathbf{u}\|_2 = 1), D_{\mathbf{u}}\ell(\mathbf{w}) = \lim_{h \to 0}\frac{\ell(\mathbf{w}+h\mathbf{u})-\ell(\mathbf{w})}{h} = \mathbf{u}\cdot\nabla_{\mathbf{w}}\ell(\mathbf{w}).
  • Three Rays at (0, \log 2) with \nabla f = (\tfrac{1}{3}, \tfrac{2}{3})^\top:
    • Shift ray (1, 1)/\sqrt{2} gives +1/\sqrt{2} \approx +0.707.
    • Steepest descent -(1, 2)/\sqrt{5} gives -\sqrt{5}/3 \approx -0.745.
    • Contour tangent (2, -1)/\sqrt{5} gives 0.

Steepest Descent via Cauchy-Schwarz

First-Order Optimization

  • Cauchy-Schwarz bound on directional slopes: For any unit vector \mathbf{v} (\|\mathbf{v}\|_2 = 1), -\|\nabla \ell\|_2 \le \mathbf{v} \cdot \nabla \ell \le \|\nabla \ell\|_2
  • Unique steepest directions: The upper bound is attained uniquely by \mathbf{v} = +\nabla \ell / \|\nabla \ell\|_2 (steepest ascent) and the lower bound by \mathbf{v} = -\nabla \ell / \|\nabla \ell\|_2 (steepest descent).
  • Norm dependence (SGD vs. Adam): “Steepest” depends on the norm constraining the step: under the \ell_\infty box constraint \|\mathbf{v}\|_\infty \le 1, the steepest descent step is -\operatorname{sign}(\nabla \ell) coordinate-wise, which closely resembles Adam’s normalized updates.

Gradients Stand Orthogonal to Level Sets

Contour Geometry

  • Proof via Level-Set Curves: For any curve \boldsymbol{\gamma}(t) inside \{\ell(\mathbf{x}) = c\} with \boldsymbol{\gamma}(0) = \mathbf{x} and velocity \mathbf{v} = \boldsymbol{\gamma}'(0), differentiating \ell(\boldsymbol{\gamma}(t)) = c gives 0 = \mathbf{v}\cdot\nabla\ell(\mathbf{x}).
  • LSE Check at (0, \log 2): Level curve e^x + e^y = 3 has tangent \mathbf{v} = (e^y, -e^x) = (2, -1), orthogonal to \nabla f = (\tfrac{1}{3}, \tfrac{2}{3})^\top.

Tangent Planes to Loss Surfaces

Surface Linearization

  • Graph of the linear approximation: Dropping the o(\|\mathbf{x} - \mathbf{x}_0\|) remainder from the first-order expansion defines the tangent plane to the surface z = f(\mathbf{x}) at \mathbf{x}_0: z = f(\mathbf{x}_0) + \nabla f(\mathbf{x}_0) \cdot (\mathbf{x} - \mathbf{x}_0)
  • 3D surface normal vs. 2D contour normal: Rewriting the plane as \nabla f(\mathbf{x}_0) \cdot (\mathbf{x} - \mathbf{x}_0) - (z - f(\mathbf{x}_0)) = 0 shows that [\nabla f(\mathbf{x}_0), -1]^\top (or [-\nabla f(\mathbf{x}_0), 1]^\top) is normal to the tangent plane in (\mathbf{x}, z)-space.
  • Horizontal slice: Projecting that 3D normal onto the base plane drops the z-component and leaves \nabla f(\mathbf{x}_0) normal to the level curve.

Critical Points and Global Existence Checks

First-Order Optimality

  • Critical Points (\nabla f(\mathbf{x}_0) = \mathbf{0}): For \psi(x, y) = x e^{-x^2 - y^2}, setting \nabla\psi = \mathbf{0} forces y = 0 and x = \pm 1/\sqrt{2} (\psi \approx \mp 0.4289).
  • Why Global Checks Are Mandatory: \nabla f = \mathbf{0} alone does not prove a minimum exists (x^3 at 0).
  • Compact Disc Bound: Outside R = 2, |\psi| \le 2e^{-4} < 0.04, so Extreme Value Theorem certifies (-1/\sqrt{2}, 0) as the global minimum.

The Hessian Matrix and Log-Sum-Exp Curvature

Second Partial Derivatives

  • Hessian Entries: [\mathbf{H}_f(\mathbf{x})]_{ij} = \frac{\partial^2 f}{\partial x_i \partial x_j}(\mathbf{x}) measures how slope j changes along axis i.
  • Log-Sum-Exp Hessian: Differentiating p_i = e^{x_i}/\sum_k e^{x_k} gives \mathbf{H}_f(\mathbf{x}) = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top.
  • Spectrum at (0, \log 2): Eigenvalue 0 along (1, 1)^\top (flat shift ray) and \tfrac{4}{9} along (1, -1)^\top.

Symmetry of the Hessian: Clairaut-Schwarz Theorem

Mixed Partials

  • Clairaut–Schwarz Theorem: When mixed partials \partial^2_{ij}f and \partial^2_{ji}f are continuous at \mathbf{x}, they are equal, so \mathbf{H}_f(\mathbf{x}) = \mathbf{H}_f(\mathbf{x})^\top.
  • Symmetric Second Difference Proof: Both orders are limits of the same i \leftrightarrow j symmetric four-corner ratio \Delta_h.
  • Spectral Theorem Payoff: Every C^2 Hessian has real eigenvalues and an orthonormal eigenbasis.

The Multivariable Second-Order Taylor Expansion

Quadratic Approximation

  • 1D Slice Reduction (\varphi(t) = f(\mathbf{x}_0 + t\boldsymbol{\delta})): \varphi'(t) = \nabla f\cdot\boldsymbol{\delta} and \varphi''(t) = \boldsymbol{\delta}^\top\mathbf{H}_f\boldsymbol{\delta}.
  • Exact Lagrange Form: f(\mathbf{x}_0 + \boldsymbol{\delta}) = f(\mathbf{x}_0) + \nabla f(\mathbf{x}_0)\cdot\boldsymbol{\delta} + \tfrac{1}{2}\boldsymbol{\delta}^\top\mathbf{H}_f(\boldsymbol{\xi})\boldsymbol{\delta} for \boldsymbol{\xi} on the segment.
  • Local Quadratic Bowl: Replacing \mathbf{H}_f(\boldsymbol{\xi}) by \mathbf{H}_f(\mathbf{x}_0) leaves an o(\|\boldsymbol{\delta}\|^2) remainder (O(\|\boldsymbol{\delta}\|^3) for C^3).

Predicting the Linear Model Gap with the Hessian

Second-Order Accuracy

For log-sum-exp at (0, \log 2) with step \boldsymbol{\epsilon} = (0.01, -0.03), the quadratic term \tfrac{1}{2}\boldsymbol{\epsilon}^\top \mathbf{H}_f \boldsymbol{\epsilon} = \tfrac{1}{9}(\epsilon_1 - \epsilon_2)^2 = \tfrac{1}{9}(0.04)^2 \approx 1.7778 \times 10^{-4} predicts the measured gap 1.786 \times 10^{-4} to three digits:

9 H = [[2.0, -2.0], [-2.0, 2.0]]
eigenvalues: [0.       0.444444]
|eps| 3.2e-02: predicted gap 1.7778e-04
|eps| 3.2e-03: predicted gap 1.7778e-06

The tiny residual difference (\approx 8 \times 10^{-7}) between the true gap and \tfrac{1}{2}\boldsymbol{\epsilon}^\top \mathbf{H}_f \boldsymbol{\epsilon} is the third-order O(\|\boldsymbol{\epsilon}\|^3) Taylor term, which drops a thousandfold when \|\boldsymbol{\epsilon}\| shrinks tenfold.

Visualizing a Curved Surface and Its Taylor Bowl

3D Quadratic Fit

  • Quadratic model of \psi(x, y) = x e^{-x^2 - y^2} at (-1, 0): With \psi(-1, 0) = -e^{-1}, \nabla \psi(-1, 0) = (-e^{-1}, 0)^\top, and \mathbf{H}_\psi(-1, 0) = 2e^{-1}\mathbf{I}, the second-order Taylor quadratic is: q(x, y) = e^{-1}\bigl(-1 - (x + 1) + (x + 1)^2 + y^2\bigr)
  • Geometric agreement: Near (-1, 0), the surface z = \psi(x, y) (blue) and its Taylor paraboloid z = q(x, y) (orange) agree in value, gradient, and curvature.

Verifying Cubic Remainder Scaling of the Taylor Bowl

Third-Order Remainder

Evaluating \psi(x, y) and its second-order quadratic approximation q(x, y) at steps (-1 + d, d) away from (-1, 0) confirms that the remainder scales as O(d^3):

step 0.00: psi = -0.367879, quadratic = -0.367879, gap = 0.000000
step 0.05: psi = -0.384315, quadratic = -0.384434, gap = 0.000119
step 0.10: psi = -0.396388, quadratic = -0.397310, gap = 0.000921
step 0.30: psi = -0.391929, quadratic = -0.412025, gap = 0.020096

Doubling the coordinate step d from 0.05 to 0.10 multiplies the gap |\psi - q| by nearly 8 = 2^3 (0.000119 \to 0.000921). Jumping directly to the minimizer of this local quadratic model at each step is multivariable Newton’s method.

The Second-Derivative Test via Eigenvalues

Critical Point Classification

At a critical point \nabla f(\mathbf{x}_0) = \mathbf{0}, expanding \mathbf{v} in the orthonormal eigenbasis \{(\lambda_i, \mathbf{q}_i)\} of the symmetric Hessian \mathbf{H} = \mathbf{H}_f(\mathbf{x}_0) diagonalizes the quadratic form:

\mathbf{v}^\top \mathbf{H} \mathbf{v} = \sum_{i=1}^n \lambda_i (\mathbf{q}_i^\top \mathbf{v})^2

Proposition (Second-Order Optimality Conditions at \nabla f(\mathbf{x}_0) = \mathbf{0}):

  1. Necessary condition for a local minimum: If \mathbf{x}_0 is a local minimum, then \mathbf{H} \succeq 0 (all \lambda_i \ge 0).
  2. Sufficient condition for a strict local minimum: If \mathbf{H} \succ 0 (all \lambda_i > 0), then for small \boldsymbol{\delta} \ne \mathbf{0}, f(\mathbf{x}_0 + \boldsymbol{\delta}) - f(\mathbf{x}_0) \ge \tfrac{1}{4}\lambda_{\min}\|\boldsymbol{\delta}\|^2 > 0, so \mathbf{x}_0 is a strict local minimum (and if \mathbf{H} \prec 0, a strict local maximum).
  3. Saddle point: If \mathbf{H} has both strictly positive and strictly negative eigenvalues, f rises along positive eigenvectors and falls along negative eigenvectors, so \mathbf{x}_0 is a saddle point.

Four Curvature Regimes at Critical Points

Geometric Classification

  • (a) \mathbf{H} \succ 0 (Local minimum): All eigenvalues positive (\psi at (-1/\sqrt{2}, 0)); contours are ellipses and -\mathbf{H}\mathbf{v} points inward.
  • (b) \mathbf{H} \prec 0 (Local maximum): All eigenvalues negative (\psi at (+1/\sqrt{2}, 0)); surface curves downward in all directions.
  • (c) Indefinite \mathbf{H} (Saddle point): Mixed signs (\lambda = \pm 4 on x^4 + y^4 - 4xy at \mathbf{0}); hyperbolic contours flow inward along one axis and outward along the other.
  • (d) Singular semidefinite \mathbf{H} (Inconclusive): Zero eigenvalue leaves higher-order terms in control (x^2 + y^4 is a minimum, x^2 - y^4 is a saddle, yet both have \mathbf{H}(\mathbf{0}) = \operatorname{diag}(2, 0)).

Numerical Classification and Flat-Direction Traps

Finite-Difference Hessians

Computing a 2 \times 2 Hessian from centered second differences and classifying critical points by their eigenvalues requires a positive zero-tolerance tol so finite-difference truncation error along flat quartic directions is not mistaken for genuine curvature:

(-0.7071, 0): eigenvalues +0.858, +1.716 -> minimum
(+0.7071, 0): eigenvalues -1.716, -0.858 -> maximum
(+0.0000, 0): eigenvalues -4.000, +4.000 -> saddle
(+0.0000, 0): eigenvalues +0.000, +2.000 -> inconclusive

On the trough x^2 + y^4 at (0, 0), the true second partial \partial_{yy} f(0, 0) is 0, but finite differences with step h = 10^{-4} return 2h^2 = 2 \times 10^{-8} > 0. Setting tol=1e-6 rightly flags the second-order test as inconclusive.

Why Saddles Dominate High-Dimensional Landscapes

Random Matrix Theory

  • Eigenvalue Repulsion in Random Hessians: In n dimensions, a local minimum requires all n eigenvalues to be positive. For GOE random symmetric matrices, P(\mathbf{H} \succ 0) \sim \exp(-\frac{\ln 3}{4}n^2) \ll 2^{-n}.
  • Loss vs. Index (Dauphin et al., 2014): High-loss critical points are overwhelmingly saddle points (high fraction of negative \lambda_i), while local minima concentrate near the global minimum loss floor.

Constrained Optimization and Lagrange Multipliers

Constraint Surfaces

  • Restricting \mathbf{x} to \{g(\mathbf{x}) = c\}: At a constrained local optimum \mathbf{x}^\star, any move \mathbf{v} tangent to the constraint surface must produce zero first-order change in f, so \mathbf{v} \cdot \nabla f(\mathbf{x}^\star) = 0.
  • Gradients align at tangency: Because \nabla g(\mathbf{x}^\star) \ne \mathbf{0} is normal to the constraint surface, every vector orthogonal to \nabla g(\mathbf{x}^\star) is also orthogonal to \nabla f(\mathbf{x}^\star), forcing the Lagrange condition: \nabla f(\mathbf{x}^\star) = \mu\,\nabla g(\mathbf{x}^\star)
  • Worked example (\min x^2 + y^2 on xy = 1): (2x, 2y) = \mu(y, x) with xy = 1 yields \mu = 2 and closest points \pm(1, 1) at squared distance 2.

Concept Check: Multivariable Calculus

Review & Practice

Test your understanding of directional derivatives, Hessian eigenvalues, and Lagrange multipliers:

  1. Directional derivative & level sets: At (0, \log 2), log-sum-exp has \nabla f = (\tfrac{1}{3}, \tfrac{2}{3})^\top. What is the maximum possible directional derivative D_{\mathbf{u}}f over all unit vectors \mathbf{u}, and along which unit vector \mathbf{u} does it occur?
  2. Hessian classification: Suppose \nabla f(\mathbf{x}_0) = \mathbf{0} and \mathbf{H}_f(\mathbf{x}_0) = \begin{bmatrix} 1 & 3 \\ 3 & 1 \end{bmatrix}. Is \mathbf{x}_0 a local minimum, local maximum, or saddle point?
  3. Lagrange multiplier for maximum entropy: Maximizing \mathbf{z} \cdot \mathbf{p} - \sum_i p_i \log p_i subject to g(\mathbf{p}) = \sum_i p_i = 1 gives z_i - \log p_i - 1 = \mu. Which probability distribution \mathbf{p}^\star solves this?

Answers:

  1. By Cauchy-Schwarz, the maximum directional derivative is \|\nabla f\|_2 = \sqrt{(1/3)^2 + (2/3)^2} = \sqrt{5}/3 \approx 0.745, achieved along \mathbf{u} = \nabla f / \|\nabla f\|_2 = \frac{1}{\sqrt{5}}(1, 2)^\top.
  2. \mathbf{H}_f(\mathbf{x}_0) has trace 2 and determinant 1 - 9 = -8 < 0 (eigenvalues \lambda_1 = 4 > 0 along (1,1)^\top and \lambda_2 = -2 < 0 along (1,-1)^\top). Because the eigenvalues have opposite signs, \mathbf{x}_0 is a saddle point despite positive diagonal entries.
  3. Solving z_i - \log p_i - 1 = \mu gives p_i = e^{z_i - 1 - \mu} \propto e^{z_i}; normalizing so \sum_i p_i = 1 yields the softmax distribution p_i^\star = e^{z_i} / \sum_k e^{z_k}.

Summary: Multivariable Calculus Toolkit

Key Takeaways

Mathematical Object / Rule Formula & Condition Geometric & Deep Learning Interpretation
Multivariable Linear Model \ell(\mathbf{w}+\boldsymbol{\epsilon}) = \ell(\mathbf{w}) + \boldsymbol{\epsilon}\cdot\nabla_{\mathbf{w}}\ell(\mathbf{w}) + o(\|\boldsymbol{\epsilon}\|) Holds when partials are continuous (C^1); predicts joint weight moves
Directional Derivative D_{\mathbf{u}}\ell(\mathbf{w}) = \mathbf{u} \cdot \nabla_{\mathbf{w}}\ell(\mathbf{w}) (\|\mathbf{u}\|_2 = 1) Instantaneous slope along ray \mathbf{u}; bounded in [-\|\nabla\ell\|_2, +\|\nabla\ell\|_2]
Steepest Descent & Contours \mathbf{u}^\star = -\nabla\ell / \|\nabla\ell\|_2; \mathbf{v}_{\text{tangent}} \cdot \nabla\ell = 0 -\nabla\ell points downhill perpendicular to level sets \{\ell = c\}
Symmetric Hessian \mathbf{H}_f [\mathbf{H}_f]_{ij} = \frac{\partial^2 f}{\partial x_i \partial x_j} = \frac{\partial^2 f}{\partial x_j \partial x_i} Captures surface curvature; for log-sum-exp, \mathbf{H} = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top
Second-Order Taylor Model f(\mathbf{x}_0+\boldsymbol{\delta}) \approx f(\mathbf{x}_0) + \nabla f\cdot\boldsymbol{\delta} + \tfrac{1}{2}\boldsymbol{\delta}^\top\mathbf{H}_f\boldsymbol{\delta} Local quadratic bowl; accounts for the O(\|\boldsymbol{\delta}\|^2) gap of the tangent plane
Second-Derivative Test \mathbf{v}^\top\mathbf{H}\mathbf{v} = \sum_i \lambda_i (\mathbf{q}_i^\top\mathbf{v})^2 at \nabla f(\mathbf{x}_0)=\mathbf{0} \lambda_i > 0 \Rightarrow strict min; \lambda_i < 0 \Rightarrow strict max; mixed signs \Rightarrow saddle
Lagrange Multipliers \nabla f(\mathbf{x}^\star) = \mu\,\nabla g(\mathbf{x}^\star) on g(\mathbf{x}) = c Level set of f is tangent to constraint surface (\nabla g(\mathbf{x}^\star) \ne \mathbf{0})