Skip to main content

12. Multivariable Differentiation — From Gradients to Jacobians and Hessians

Calculus 4/5. The moment the input becomes a vector, "one slope" is no longer enough. Because differentiation was defined as the "best linear approximation," the extension is natural: the linear approximation is merely promoted from a number to a vector (gradient), then from a vector to a matrix (Jacobian). Every derivative in deep learning is written in the language of this page.

Partial derivatives

For f:RnRf: \mathbb{R}^n \to \mathbb{R}, differentiate only in the xix_i direction while holding the other variables fixed:

fxi(a)=limh0f(a+hei)f(a)h\frac{\partial f}{\partial x_i}(\mathbf{a}) = \lim_{h \to 0} \frac{f(\mathbf{a} + h\,\mathbf{e}_i) - f(\mathbf{a})}{h}

A partial derivative is only the slope of a cross-section along a coordinate axis. Unlike the single-variable case, a function may fail to be "differentiable" even when every partial derivative exists—there are functions that appear well-behaved along the axes but are discontinuous in a diagonal direction. The real multivariable definition therefore uses linear approximation.

Total differentiability = existence of a linear approximation

A function f:RnRmf: \mathbb{R}^n \to \mathbb{R}^m is differentiable at a\mathbf{a} if a linear map (matrix) JJ exists such that

f(a+h)=f(a)+Jh+o(h)f(\mathbf{a} + \mathbf{h}) = f(\mathbf{a}) + J\,\mathbf{h} + o(\|\mathbf{h}\|)

This JRm×nJ \in \mathbb{R}^{m \times n} is the Jacobian matrix, whose entries are partial derivatives:

Jij=fixjJ=(f1x1f1xnfmx1fmxn)J_{ij} = \frac{\partial f_i}{\partial x_j} \qquad J = \begin{pmatrix} \frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \cdots & \frac{\partial f_m}{\partial x_n} \end{pmatrix}

A sufficient condition: if every partial derivative exists and is continuous (C1C^1), the function is differentiable. Most functions used in practice fall into this category.

Gradient — when the output is scalar

When m=1m = 1, the Jacobian is a row vector, and its transpose is called the gradient:

f=(fx1,,fxn) ⁣f(a+h)f(a)+f(a)h\nabla f = \left( \frac{\partial f}{\partial x_1}, \dots, \frac{\partial f}{\partial x_n} \right)^{\!\top} \qquad f(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a})^{\top} \mathbf{h}

Directional derivative and steepest ascent

The rate of change in the direction of a unit vector u\mathbf{u} (the directional derivative) is

Duf=fu=fcosθD_{\mathbf{u}} f = \nabla f^{\top} \mathbf{u} = \|\nabla f\| \cos\theta

By Cauchy–Schwarz, this is maximized when u\mathbf{u} points in the direction of f\nabla f. Therefore:

  • The gradient points in the direction of the function's steepest increase, and its magnitude is the slope in that direction.
  • It is always perpendicular to the level set.
  • Moving in the f-\nabla f direction therefore gives the fastest local decrease—the reason gradient descent (xxηf\mathbf{x} \leftarrow \mathbf{x} - \eta \nabla f) exists.

Multivariable chain rule = product of Jacobians

For f:RnRmf: \mathbb{R}^n \to \mathbb{R}^m and g:RmRkg: \mathbb{R}^m \to \mathbb{R}^k,

Jgf(x)=Jg(f(x))Jf(x)J_{g \circ f}(\mathbf{x}) = J_g\big(f(\mathbf{x})\big) \cdot J_f(\mathbf{x})

The "product of slopes" from one variable has merely been promoted to a matrix product; the content is identical ("the composition of linear approximations is the product of linear maps"). The gradient of a neural network L=(fL(f2(f1(x))))L = \ell(f_L(\cdots f_2(f_1(\mathbf{x})))) is exactly this product of layerwise Jacobians. The order of multiplication (left-to-right versus right-to-left) creates the distinction between forward- and reverse-mode automatic differentiation.

Written along scalar paths, it takes the familiar form. If z=g(y1,,ym)z = g(y_1, \dots, y_m) and yj=fj(x)y_j = f_j(x),

dzdx=j=1mzyjdyjdx\frac{dz}{dx} = \sum_{j=1}^{m} \frac{\partial z}{\partial y_j} \frac{d y_j}{dx}

"Add the contribution from every path"—this is the backpropagation rule that sums every gradient entering a node in a computational graph.

Hessian — second-order information

For f:RnRf: \mathbb{R}^n \to \mathbb{R}, collect the second partial derivatives into a matrix:

Hij=2fxixjH_{ij} = \frac{\partial^2 f}{\partial x_i\, \partial x_j}
  • Schwarz's theorem: if the second partial derivatives are continuous, Hij=HjiH_{ij} = H_{ji}—the Hessian is symmetric.
  • Second-order Taylor expansion:
f(x+h)f(x)+fh+12hHhf(\mathbf{x} + \mathbf{h}) \approx f(\mathbf{x}) + \nabla f^{\top} \mathbf{h} + \frac{1}{2} \mathbf{h}^{\top} H\, \mathbf{h}
  • Classification at a critical point (f=0\nabla f = 0): if H0H \succ 0 (positive definite), it is a minimum; if H0H \prec 0, a maximum; and if the eigenvalues have mixed signs, a saddle point. It is known that high-dimensional loss landscapes contain overwhelmingly more saddle points than maxima or minima.
  • If the ratio between the Hessian's eigenvalues (condition number) is large, the loss landscape becomes a narrow valley and gradient descent zigzags slowly—the problem that preconditioning, momentum, and Adam try to alleviate.
  • Statistical connection: the negative expected Hessian of the log-likelihood is the Fisher information matrix, and it acts as the inverse covariance matrix of the posterior in the Laplace approximation.

Matrix differentiation cheatsheet

These identities recur in ML papers. (a\mathbf{a} and b\mathbf{b} are constant vectors, AA is a constant matrix, and denominator layout is used.)

f(x)f(\mathbf{x})f\nabla fNote
ax\mathbf{a}^{\top}\mathbf{x}a\mathbf{a}Linear term
xAx\mathbf{x}^{\top} A \mathbf{x}(A+A)x(A + A^{\top})\,\mathbf{x}2Ax2A\mathbf{x} when AA is symmetric
x2=xx\|\mathbf{x}\|^2 = \mathbf{x}^{\top}\mathbf{x}2x2\mathbf{x}Special case above (A=IA = I)
Axb2\|A\mathbf{x} - \mathbf{b}\|^22A(Axb)2A^{\top}(A\mathbf{x} - \mathbf{b})Least squares
lndetA\ln \det A/A=(A1)\partial/\partial A = (A^{-1})^{\top}Gaussian log-likelihood

Example — normal equations for least squares: solving Axb2=0\nabla \|A\mathbf{x} - \mathbf{b}\|^2 = 0 gives

AAx=AbA^{\top} A\, \mathbf{x} = A^{\top} \mathbf{b}

The closed-form solution to linear regression emerges from one gradient calculation. The MLE of a multivariate Gaussian (estimating mean and covariance) is derived the same way, including the lndet\ln\det identity.

Summary

  • Multivariable differentiation has the same definition as single-variable differentiation: the "best linear approximation." A scalar output gives a gradient; a vector output gives a Jacobian.
  • Existence of partial derivatives ≠ differentiability. C1C^1 is safe.
  • The gradient points in the direction of steepest ascent (perpendicular to level sets)—the basis of gradient descent.
  • The chain rule is a product of Jacobian matrices, and choosing the multiplication order gives forward/reverse-mode automatic differentiation.
  • The Hessian is second-order information that determines convexity, saddle points, condition numbers, and Fisher information.

Next: Differentiation, Optimization, and Automatic Differentiation