AI Wiki中文/EN

The Jacobian

雅可比矩阵

Track how several outputs respond to several inputs, with a fixed shape convention and checked forward and reverse products.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

Two knobs, three readings

Imagine a toy sensor with two inputs, a and b, and three readings: a2+ba^2+b, abab, and sin⁡a\sin a. Turn the first knob a little. All three readings may move. Turn the second knob: the third reading stays still. A single slope cannot record this behavior. A table with one row per reading and one column per knob can.

That table is the Jacobian. It describes the local linear response of a differentiable vector-valued function. The entries need not stay constant as the knobs move. For a linear map they do; for a nonlinear sensor they must be evaluated at a specified input. MML, Section 5.3

Rows are outputs; columns are inputs

Let f:Rn→Rmf:\mathbb R^n\to\mathbb R^m be differentiable at x. Throughout this encyclopedia, inputs and scalar-loss gradients are column vectors. The Jacobian has shape m×nm\times n:

Jf(x)ij=∂fi∂xj(x),f(x+h)=f(x)+Jf(x)h+r(h),∥r(h)∥∥h∥⟶0.J_f(x)_{ij}=\frac{\partial f_i}{\partial x_j}(x),\qquad f(x+h)=f(x)+J_f(x)h+r(h),\qquad \frac{\|r(h)\|}{\|h\|}\longrightarrow0.

The limit is as nonzero h tends to zero. It means the leftover error is small compared with the input displacement, in every direction. Merely having all coordinate partial derivatives at a point does not guarantee this property. Continuous partial derivatives in a neighborhood are a useful sufficient condition. MML uses a row convention for a scalar derivative; here the scalar Jacobian is the row Jf=(∇f)⊤J_f=(\nabla f)^\top, keeping the column-gradient convention explicit.

For the sensor at (a,b)=(1,2)(a,b)=(1,2),

Jf(1,2)=[2121cos⁡10],Jf(1,2)[0.2−0.1]=[0.30.30.2cos⁡1].J_f(1,2)=\begin{bmatrix}2&1\\2&1\\\cos1&0\end{bmatrix},\qquad J_f(1,2)\begin{bmatrix}0.2\\-0.1\end{bmatrix}=\begin{bmatrix}0.3\\0.3\\0.2\cos1\end{bmatrix}.

These are first-order predicted changes. The actual changes in the first two readings are 0.34 and 0.28: the omitted terms are ha2=0.04h_a^2=0.04 and hahb=−0.02h_ah_b=-0.02. Halving h quarters those particular remainders. The sine remainder has a second-order bound from its bounded second derivative, but is not the same polynomial.

For another differentiable map g:Rm→Rkg:\mathbb R^m\to\mathbb R^k, substitute the two local approximations. The chain rule becomes

Jg∘f(x)=Jg(f(x))Jf(x).J_{g\circ f}(x)=J_g(f(x))J_f(x).

The shapes are (k×m)(m×n)(k\times m)(m\times n). The order follows the computation: first change the sensor readings, then change g. Swapping the factors is generally meaningless or incorrect.

Check the table without an autodiff library

This complete standard-library program compares the analytic table with central finite differences. The sensor and matvec helper assume matching dimensions; finite_jacobian validates its own vector and output contract. It is a diagnostic for small smooth functions, not a general automatic differentiation engine.

import math

def sensor(x):
    a, b = x
    return [a*a+b, a*b, math.sin(a)]

def sensor_jacobian(x):
    a, b = x
    return [[2*a, 1.], [b, a], [math.cos(a), 0.]]

def finite_jacobian(f, x, step=1e-5):
    x = list(x)
    if not x or not all(math.isfinite(t) for t in x) or not math.isfinite(step) or step <= 0:
        raise ValueError("finite nonempty input and positive finite step required")
    base = list(f(x))
    if not base or not all(math.isfinite(t) for t in base):
        raise ValueError("finite nonempty vector output required")
    result = [[0.]*len(x) for _ in base]
    for j in range(len(x)):
        plus, minus = x.copy(), x.copy()
        plus[j] += step; minus[j] -= step
        if plus[j] == x[j] or minus[j] == x[j]:
            raise ValueError("step is too small for this input scale")
        fp, fm = list(f(plus)), list(f(minus))
        if len(fp) != len(base) or len(fm) != len(base) or not all(math.isfinite(t) for t in fp+fm):
            raise ValueError("output shape changed or became nonfinite")
        for i in range(len(base)):
            result[i][j] = (fp[i]-fm[i])/(2*step)
            if not math.isfinite(result[i][j]):
                raise ArithmeticError("nonfinite finite difference")
    return result

def matvec(a, v):
    return [sum(t*s for t,s in zip(row,v)) for row in a]

x = [1., 2.]
j = sensor_jacobian(x)
approx = finite_jacobian(sensor, x)
assert max(abs(a-b) for r,s in zip(j,approx) for a,b in zip(r,s)) < 1e-9
v = [.2, -.1]
assert all(abs(a-b)<1e-12 for a,b in zip(matvec(j,v), [.3,.3,.2*math.cos(1)]))
g = [2., -1., 0.]
pullback = matvec(list(zip(*j)), g)
assert pullback == [2.,1.]
assert abs(sum(a*b for a,b in zip(g,matvec(j,v)))-sum(a*b for a,b in zip(pullback,v))) < 1e-12

For n inputs and m outputs it makes 2n+12n+1 function calls, uses O(mn+n)O(mn+n) storage, and performs O(mn+n2)O(mn+n^2) work outside those calls, including copying n-element input vectors. Smoothness, step size, roundoff, and the cost of f all matter. A tiny step can round back to the original input; a large one can measure a secant far from the desired derivative. A successful check at one x is evidence about that case, not a proof for all x.

Usually you need a product, not the whole table

A direction v produces a Jacobian–vector product, JfvJ_fv. A scalar loss L on the outputs sends back Jf⊤∇LJ_f^\top\nabla L. With an arbitrary column seed g, this reverse product obeys

g⊤(Jfv)=(Jf⊤g)⊤v.g^\top(J_fv)=(J_f^\top g)^\top v.

For the sensor, g=(2,−1,0)g=(2,-1,0) gives Jf⊤g=(2,1)J_f^\top g=(2,1). This identity checks both shape and transpose choices. Some libraries call the reverse operation a vector–Jacobian product because they write the seed as a row. The mathematical content is the same after converting conventions. Forward and reverse automatic differentiation can compute these products without storing a dense Jacobian. Baydin et al., Sections 3.1–3.2

At a ReLU kink there is no ordinary Jacobian; a framework’s selected derivative is an implementation convention. Stochastic functions also need fixed randomness for a meaningful finite-difference comparison. For square, nonsingular Jacobians, determinants describe local volume scaling. Rectangular Jacobians still describe derivatives perfectly well, even though an ordinary determinant is unavailable.

Follow the shape before multiplying

  1. A function maps five numbers to two. What is its Jacobian shape? Answer: 2×52\times5; a reverse seed has length two and returns a length-five input gradient.
  2. Why does the first reading change by 0.34 rather than 0.3? Answer: the linear prediction omits the quadratic term 0.220.2^2.
  3. For g(y)=y1−2y2g(y)=y_1-2y_2, what is Jg∘f(1,2)J_{g\circ f}(1,2)? Answer: (1,−2,0)Jf=(−2,−1)(1,-2,0)J_f=(-2,-1).
  4. Does a zero third-column derivative prove an output never depends on that input? Answer: no. A derivative may vanish only at the tested point, as the derivative of x2x^2 does at zero.

From local response to learning systems

A network is a composition of maps, so the same chain rule connects input sensitivity, parameter gradients, and local changes of representation. A large singular value of a Jacobian indicates a direction of strong local amplification; it does not by itself establish global behavior or robustness. Continue with the Hessian for how a scalar gradient itself changes, or automatic differentiation for computing products through a program.

Make a prediction, then check

Five inputs map to two outputs. What is the Jacobian shape?

Choose one answer

Learning objectives

  • Derive a composite Jacobian using output rows and input columns.
  • Check shapes and sensitivity with forward and reverse products.

Prerequisites & connections

Explore related knowledge →

References

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. Automatic Differentiation in Machine Learning: a Survey
    Atilim Gunes Baydin; Barak A. Pearlmutter; Alexey Andreyevich Radul; Jeffrey Mark Siskind · 2018

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: specified-source comparison, derivation checks, independent numerical checks, and paragraph-level bilingual comparison; no independent human peer review.

  • Compared analytic chain rules with central differences for 50 nonlinear compositions; checked JVP/VJP duality, changing output shapes, and steps rounded away.
  • Recomputed sensor changes and quadratic remainders; unified column gradients and output rows, and did not equate existing partial derivatives with differentiability.
  • Compared bilingual assumptions, claims, numbers, exercise answers and citations; structural checks additionally compare formulas, code, main anchors and source IDs.
  • Static link checking found an abbreviated autodiff route; verified the target article and corrected it to the stable ID automatic-differentiation.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.