Math & Statistics

Gradients, Jacobians & Hessians

Mastering first and second derivatives across scalar functions, vector outputs, and multi-dimensional loss surfaces.

🟡 intermediate5 min readcalculus
Gradients, Jacobians, and Hessians represent the calculus foundation for optimizing machine learning models. The Gradient vector ∇f(x) contains first-order partial derivatives of a scalar loss function with respect to input vector x. The Jacobian matrix J contains first-order partial derivatives of a vector-valued function f: R^n -> R^m, used in backpropagation chain rule. The Hessian matrix H contains second-order partial derivatives of a scalar function, measuring local loss surface curvature.

First and Second Derivatives in Multivariable Calculus

In single-variable calculus, we have $f'(x)$ (slope) and $f''(x)$ (curvature).

Multivariable calculus extends these concepts to vectors and matrices:

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│  1. GRADIENT ∇f(x)       │  2. JACOBIAN MATRIX J    │  3. HESSIAN MATRIX H     │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Function: f: R^n -> R    │ Function: f: R^n -> R^m  │ Function: f: R^n -> R    │
│ First derivatives        │ First derivatives        │ Second derivatives       │
│ Dimensions: n × 1 Vector │ Dimensions: m × n Matrix │ Dimensions: n × n Matrix │
│ Direction of steepest ascent| Maps input to output change| Measures surface curvature|
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

1. The Gradient Vector $\nabla f(x)$

For a scalar loss function $f(x_1, x_2, \dots, x_n)$:

$$\nabla f(x) = \left[ \frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \dots, \frac{\partial f}{\partial x_n} \right]^T$$

2. The Jacobian Matrix $J$

When a function takes $n$ inputs and outputs $m$ values ($f: \mathbb{R}^n \to \mathbb{R}^m$):

$$J_{ij} = \frac{\partial f_i}{\partial x_j} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \dots & \frac{\partial f_1}{\partial x_n} \ \vdots & \ddots & \vdots \ \frac{\partial f_m}{\partial x_1} & \dots & \frac{\partial f_m}{\partial x_n} \end{bmatrix}_{m \times n}$$

Backpropagation Chain Rule

For composed layers $y = f(g(x))$:

$$J_{y, x} = J_{y, g} \cdot J_{g, x} \quad \text{(Matrix Multiplication of Jacobians!)}$$

3. The Hessian Matrix $H$

Contains all pairwise second-order partial derivatives of scalar loss $f(x)$:

$$H_{ij} = \frac{\partial^2 f}{\partial x_i \partial x_j} = \begin{bmatrix} \frac{\partial^2 f}{\partial x_1^2} & \dots & \frac{\partial^2 f}{\partial x_1 \partial x_n} \ \vdots & \ddots & \vdots \ \frac{\partial^2 f}{\partial x_n \partial x_1} & \dots & \frac{\partial^2 f}{\partial x_n^2} \end{bmatrix}_{n \times n}$$

By Schwarz's Theorem, if functions are smooth, $H$ is a Symmetric Matrix ($H_{ij} = H_{ji}$).

Curvature Interpretation

Say this out loud

The Gradient is an n x 1 vector of first derivatives pointing in the direction of steepest ascent for a scalar loss. The Jacobian is an m x n matrix of first derivatives for vector-valued layer outputs, underpinning the chain rule in backpropagation. The Hessian is an n x n symmetric matrix of second derivatives measuring local loss surface curvature and saddle points.

Follow-ups to expect

Check yourself

Question 1 of 3

What are the tensor dimensions of the Jacobian matrix J for a vector-valued function f: R^n -> R^m?

More in Math & Statistics

See all →
Bayes’ Theorem4 minCentral Limit Theorem4 minLaw of Large Numbers4 min