Calculus and Matrix Calculus for ML

Jacobians

Jacobians as the local linear maps of vector-valued functions, with explicit input-output shape discipline.

Lesson
36

Goal

Generalize the gradient from a function that returns one number to a function that returns a vector.

The result is the Jacobian: a matrix that answers

If the input moves a little, how does every output move?

Motivation

Lesson 35 studied a scalar field:

f:RnR.f:\mathbb R^n\to\mathbb R.

Its derivative maps an input displacement to one first-order output change. But most machine-learning systems contain intermediate functions with several outputs:

For

F:RnRm,F:\mathbb R^n\to\mathbb R^m,

the local derivative must turn an nn-dimensional input displacement into an mm-dimensional output displacement. A matrix has exactly that shape.

Intuition: A Tiny Linear Machine

A nonlinear function may bend and stretch space differently at different points. Near one chosen point, however, a differentiable function is approximately linear.

The Jacobian is the matrix for that local linear machine:

ΔyJF(x)Δx.\Delta y\approx J_F(x)\Delta x.

Read this from right to left:

  1. choose a small input motion Δx\Delta x;
  2. apply the local matrix JF(x)J_F(x);
  3. obtain the predicted output motion Δy\Delta y.

The Jacobian can change from point to point even though each individual Jacobian acts linearly on small displacements.

Notation / Symbol Table

SymbolPlain-English nameShape and meaning
F:RnRmF:\mathbb R^n\to\mathbb R^mvector-valued functionAccepts nn inputs and returns mm outputs.
xRnx\in\mathbb R^ninput pointPoint where the local derivative is evaluated.
Fi(x)F_i(x)output componentThe iith scalar output of FF.
JF(x)J_F(x)Jacobianm×nm\times n matrix of output-by-input partial derivatives.
ΔxRn\Delta x\in\mathbb R^ninput displacementSmall movement in input space.
ΔyRm\Delta y\in\mathbb R^moutput displacementFirst-order response in output space.
eje_jinput basis vectorUnit motion through input coordinate jj.

The course uses the output-by-input convention:

JF(x)Rm×n.J_F(x)\in\mathbb R^{m\times n}.

Rows correspond to outputs. Columns correspond to inputs.

Formal Definition

Write the vector-valued function component by component:

F(x)=[F1(x)F2(x)Fm(x)].F(x)= \begin{bmatrix} F_1(x)\\ F_2(x)\\ \vdots\\ F_m(x) \end{bmatrix}.

Its Jacobian is:

JF(x)=[F1x1F1x2F1xnF2x1F2x2F2xnFmx1Fmx2Fmxn].J_F(x)= \begin{bmatrix} \frac{\partial F_1}{\partial x_1} & \frac{\partial F_1}{\partial x_2} & \cdots & \frac{\partial F_1}{\partial x_n}\\ \frac{\partial F_2}{\partial x_1} & \frac{\partial F_2}{\partial x_2} & \cdots & \frac{\partial F_2}{\partial x_n}\\ \vdots & \vdots & \ddots & \vdots\\ \frac{\partial F_m}{\partial x_1} & \frac{\partial F_m}{\partial x_2} & \cdots & \frac{\partial F_m}{\partial x_n} \end{bmatrix}.

The entry in row ii, column jj is:

(JF(x))ij=Fixj(x).(J_F(x))_{ij}=\frac{\partial F_i}{\partial x_j}(x).

Use the sentence output ii with respect to input jj to recover the order.

Shape Discipline

Before differentiating, write the function shape:

F:RnRm.F:\mathbb R^n\to\mathbb R^m.

Then the Jacobian shape follows automatically:

JF(x):m×n.J_F(x):m\times n.

This makes the local approximation type-check:

Δym×1JF(x)m×nΔxn×1.\underbrace{\Delta y}_{m\times1} \approx \underbrace{J_F(x)}_{m\times n} \underbrace{\Delta x}_{n\times1}.

If those inner dimensions do not match, either the Jacobian convention or the multiplication order is wrong.

Worked Example: Two Inputs, Two Outputs

Let:

F(x,y)=[x2+yxy].F(x,y)= \begin{bmatrix} x^2+y\\ xy \end{bmatrix}.

There are two outputs and two inputs, so the Jacobian is 2×22\times2.

Differentiate the first output:

F1(x,y)=[2x1].\nabla F_1(x,y)= \begin{bmatrix} 2x\\1 \end{bmatrix}.

Differentiate the second output:

F2(x,y)=[yx].\nabla F_2(x,y)= \begin{bmatrix} y\\x \end{bmatrix}.

Transpose those output gradients into rows:

JF(x,y)=[2x1yx].J_F(x,y)= \begin{bmatrix} 2x & 1\\ y & x \end{bmatrix}.

At (1,2)(1,2):

JF(1,2)=[2121].J_F(1,2)= \begin{bmatrix} 2 & 1\\ 2 & 1 \end{bmatrix}.

Use The Jacobian For A Local Prediction

Suppose the input moves by:

Δx=[0.010.02].\Delta x= \begin{bmatrix} 0.01\\ -0.02 \end{bmatrix}.

Then:

ΔFJF(1,2)Δx=[2121][0.010.02]=[00].\Delta F \approx J_F(1,2)\Delta x = \begin{bmatrix} 2 & 1\\ 2 & 1 \end{bmatrix} \begin{bmatrix} 0.01\\ -0.02 \end{bmatrix} = \begin{bmatrix} 0\\ 0 \end{bmatrix}.

Both outputs have zero first-order change in this direction at this point. That does not mean the nonlinear function is exactly constant; second-order terms may remain.

Indeed, the exact new input is (1.01,1.98)(1.01,1.98):

F(1.01,1.98)F(1,2)=[0.00010.0002].F(1.01,1.98)-F(1,2) = \begin{bmatrix} 0.0001\\ -0.0002 \end{bmatrix}.

The small discrepancy is the higher-order error that the first-order Jacobian approximation omits.

Reading Rows And Columns

The same Jacobian supports two complementary readings.

A Row Is One Output’s Gradient

Row ii contains the sensitivity of output FiF_i to every input:

rowi(JF)=Fi(x)T.\text{row}_i(J_F)=\nabla F_i(x)^T.

This is the output-centered view: choose one output and ask which inputs influence it.

A Column Is One Input Direction’s Effect

Column jj records how every output changes when only input xjx_j moves:

JF(x)ej=columnj(JF(x)).J_F(x)e_j= \text{column}_j(J_F(x)).

This is the input-centered view: choose one coordinate motion and observe the complete output response.

Rows organize scalar sensitivities by output. Columns organize vector responses by input.

Gradients And Ordinary Derivatives Are Special Cases

The Jacobian unifies earlier derivative objects.

Scalar Output

For

f:RnR,f:\mathbb R^n\to\mathbb R,

the Jacobian has shape 1×n1\times n:

Jf(x)=f(x)T.J_f(x)=\nabla f(x)^T.

The course stores the gradient itself as an n×1n\times1 column vector, so its transpose is the scalar function’s Jacobian under the output-by-input convention.

Scalar Input

For

F:RRm,F:\mathbb R\to\mathbb R^m,

the Jacobian has shape m×1m\times1. It is the column of ordinary derivatives of every output with respect to the one input.

Scalar Input And Scalar Output

For

f:RR,f:\mathbb R\to\mathbb R,

the Jacobian is 1×11\times1: the familiar derivative f(x)f'(x).

Linear Maps Have Constant Jacobians

Consider an affine function:

F(x)=Ax+b,F(x)=Ax+b,

where ARm×nA\in\mathbb R^{m\times n}.

Its Jacobian is:

JF(x)=A.J_F(x)=A.

The bias disappears because a constant does not change with the input. The Jacobian is the same everywhere because the linear part does not bend.

For a nonlinear function, JF(x)J_F(x) depends on xx. The Jacobian is the point-dependent matrix that locally plays the role of AA.

Connection To The Chain Rule

Suppose:

F:RnRmF:\mathbb R^n\to\mathbb R^m

and

G:RmRk.G:\mathbb R^m\to\mathbb R^k.

Their composition has shape:

GF:RnRk.G\circ F:\mathbb R^n\to\mathbb R^k.

The Jacobian shapes predict the chain rule:

JGF(x)k×n=JG(F(x))k×mJF(x)m×n.\underbrace{J_{G\circ F}(x)}_{k\times n} = \underbrace{J_G(F(x))}_{k\times m} \underbrace{J_F(x)}_{m\times n}.

The order matches function composition: FF acts first on the input, and GG acts second on the intermediate output.

Lesson 37 develops this multiplication rule through computation graphs and backpropagation.

Connection To Programming

A vector field accepts a vector and returns a vector:

type VectorField = (x: readonly number[]) => readonly number[];

A centered finite-difference Jacobian can perturb each input coordinate and measure every output response:

function numericalJacobian(
  f: VectorField,
  at: readonly number[],
  h = 1e-5,
): number[][] {
  const outputSize = f(at).length;
  const columns = at.map((_, coordinate) => {
    const plus = at.map((value, index) =>
      index === coordinate ? value + h : value,
    );
    const minus = at.map((value, index) =>
      index === coordinate ? value - h : value,
    );
    const fPlus = f(plus);
    const fMinus = f(minus);

    return fPlus.map((value, output) =>
      (value - fMinus[output]) / (2 * h),
    );
  });

  return Array.from({ length: outputSize }, (_, output) =>
    columns.map((column) => column[output]),
  );
}

The perturbation loop naturally produces columns: each input coordinate is changed once, and all output responses are observed. The final transformation arranges those columns into the course’s output-by-input matrix convention.

Common Mistakes

Swapping Rows And Columns

Write F:RnRmF:\mathbb R^n\to\mathbb R^m first. Under this lesson’s convention, the Jacobian is m×nm\times n: outputs by inputs.

Forgetting That Every Output Is Differentiated

A gradient handles one scalar output. A Jacobian contains one transposed output gradient per row.

Treating The Jacobian As Globally Linear

JF(x)J_F(x) is a local linear approximation at xx. For a nonlinear FF, the matrix changes with the evaluation point.

Multiplying In The Wrong Order

The local input displacement is a column vector, so use JF(x)ΔxJ_F(x)\Delta x. Shape checking exposes the reversed order.

Confusing A Zero First-Order Change With Exact Constancy

If JF(x)Δx=0J_F(x)\Delta x=0, the chosen direction has no first-order effect at that point. Higher-order changes may still occur.

Exercises

  1. In one sentence, explain why the derivative of F:R3R2F:\mathbb R^3\to\mathbb R^2 should be represented by a 2×32\times3 matrix.

  2. For

    F(x,y)=[x+y2x2y],F(x,y)= \begin{bmatrix} x+y^2\\ x^2-y \end{bmatrix},

    compute JF(x,y)J_F(x,y) and evaluate it at (1,2)(1,2).

  3. Use your Jacobian from Problem 2 to predict the first-order output change for

    Δx=[0.010.02].\Delta x= \begin{bmatrix} 0.01\\ -0.02 \end{bmatrix}.
  4. For a general F:RnRmF:\mathbb R^n\to\mathbb R^m, explain what row ii and column jj of JF(x)J_F(x) each mean.

  5. Let

    F(x)=Ax+b,A=[123405].F(x)=Ax+b, \qquad A= \begin{bmatrix} 1 & 2\\ -3 & 4\\ 0 & 5 \end{bmatrix}.

    State the input dimension, output dimension, and Jacobian. Explain why bb does not appear in it.

  6. If f:R4Rf:\mathbb R^4\to\mathbb R, state the shapes of Jf(x)J_f(x) and f(x)\nabla f(x) under this course’s convention, and write their relationship.

  7. Suppose F:R2R3F:\mathbb R^2\to\mathbb R^3 and G:R3R4G:\mathbb R^3\to\mathbb R^4. Without computing entries, give the shapes of JFJ_F, JGJ_G, and JGFJ_{G\circ F}, then write the only multiplication order that type-checks.

  8. Find a nonzero displacement vv such that

    [1224]v=0.\begin{bmatrix} 1 & 2\\ 2 & 4 \end{bmatrix}v=0.

    Explain what this says about the function’s first-order change in direction vv.

  9. Write TypeScript that uses numericalJacobian to check the analytic Jacobian of

    F(x,y)=[x2+yxy]F(x,y)= \begin{bmatrix} x^2+y\\ xy \end{bmatrix}

    at [1, 2] with a small componentwise tolerance.

  10. In your own words, explain how a Jacobian is both a table of partial derivatives and one local linear map. Why are those not two different objects?

Summary

Next Lesson

Lesson 37 uses Jacobians to differentiate compositions. Computation graphs make the chain rule operational: local derivative maps multiply in forward composition order, while backpropagation efficiently moves sensitivity information backward.