← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Jacobian matrices

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.09 · The Jacobian Matrix

Introduction

One gradient describes one output, but a function can return several outputs. For a two-input, two-output linear map, the Jacobian arranges four sensitivities so each row belongs to an output and each column to an input.

Learning goal

Construct and interpret a Jacobian matrix, then use its products in multivariable chain-rule calculations efficiently.

Before you start

Partial derivatives, gradient vectors, matrices, matrix multiplication, and multivariable function composition.

Lesson plan

  1. Arrange output-by-input partial derivatives into a matrix with clear dimensions.
  2. Calculate Jacobians for a linear map and an element-wise nonlinear function.
  3. Connect Jacobian products and vector products to efficient automatic differentiation.

Count rows and columns. If a function takes two inputs and returns three outputs, its Jacobian has shape (3,2). Row i describes output i. Column j describes sensitivity to input j.

When outputs are vectors

The gradient applies to scalar-valued functions. When $f: \mathbb{R}^n \to \mathbb{R}^m$ produces a vector output, we need the Jacobian matrix:

Definition · Jacobian

For $f: \mathbb{R}^n \to \mathbb{R}^m$ with components $f_1, \ldots, f_m$, the Jacobian is the $m \times n$ matrix:

$$J_f = \begin{bmatrix} \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{bmatrix}$$

Row $i$ contains the gradient of the $i$-th output component.

Example: a linear map

For $f(\mathbf{x}) = A\mathbf{x}$ where $A \in \mathbb{R}^{m \times n}$, the Jacobian is simply $A$ itself:

$$J_f = A$$

This makes sense: the linear map is its own best linear approximation everywhere.

Example: element-wise nonlinearity

For $f(\mathbf{x}) = \sigma(\mathbf{x})$ applied element-wise (e.g., sigmoid, ReLU), the Jacobian is diagonal:

$$J_f = \text{diag}(\sigma'(x_1), \sigma'(x_2), \ldots, \sigma'(x_n))$$

Each output $f_i$ depends only on input $x_i$, so all off-diagonal entries are zero.

Example · Softmax Jacobian

For $p_i = \frac{e^{z_i}}{\sum_j e^{z_j}}$, the Jacobian is:

$$J_{ij} = \frac{\partial p_i}{\partial z_j} = p_i(\delta_{ij} - p_j) = \begin{cases} p_i(1-p_i) & i=j \\ -p_i p_j & i \neq j \end{cases}$$

In matrix form: $J = \text{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top$.

The Jacobian in the chain rule

For a composition $\mathbf{y} = f(g(\mathbf{x}))$, the chain rule becomes:

$$J_{f \circ g}(\mathbf{x}) = J_f(g(\mathbf{x})) \cdot J_g(\mathbf{x})$$

This is just matrix multiplication. For a neural network with $K$ layers:

$$J_{\text{network}} = J_K \cdot J_{K-1} \cdots J_1$$

Jacobian-vector products (JVPs)

In practice, we rarely compute the full Jacobian (which is $m \times n$ and can be huge). Instead, we compute:

  • JVP (forward-mode): $J\mathbf{v}$ — how the output changes when the input moves in direction $\mathbf{v}$
  • VJP (reverse-mode): $\mathbf{u}^\top J$ — the gradient of $\mathbf{u}^\top f(\mathbf{x})$ with respect to $\mathbf{x}$

Backpropagation computes VJPs. PyTorch's torch.autograd.grad computes VJPs; torch.autograd.functional.jvp computes JVPs.

Gradient size and stability

Backpropagation multiplies gradients by layer Jacobians. Their singular values describe how much they can stretch or shrink vectors. Many contractions can make gradients small; repeated expansion can make them large. The directions and the full product matter, not just one number for each layer.

Eigenvalues apply only to square matrices, and the largest eigenvalue magnitude alone is not a general test for gradient stability. Initialization methods help control activation and gradient scales, but do not guarantee stable training.