1.2.5Calculus & Optimization Basics

The Jacobian matrix

1,843 words8 min readdifficulty · medium4 backlinks

WHAT it is

J=[f1x1f1xnfmx1fmxn]J = \begin{bmatrix} \dfrac{\partial f_1}{\partial x_1} & \cdots & \dfrac{\partial f_1}{\partial x_n} \\[6pt] \vdots & \ddots & \vdots \\[6pt] \dfrac{\partial f_m}{\partial x_1} & \cdots & \dfrac{\partial f_m}{\partial x_n} \end{bmatrix}

HOW we derive it (from first principles)

Step 1 — Look at one output at a time. Take component fif_i. It's a scalar function of a vector, so its first-order Taylor expansion is

fi(x+h)fi(x)+j=1nfixjhj.f_i(\mathbf{x}+\mathbf{h}) \approx f_i(\mathbf{x}) + \sum_{j=1}^{n}\frac{\partial f_i}{\partial x_j}\,h_j.

Why this step? This is just the multivariable Taylor / total-differential rule: perturbing each input xjx_j by hjh_j changes fif_i at rate fi/xj\partial f_i/\partial x_j, and small effects add up.

Step 2 — Recognize the sum as a dot product.

jfixjhj=[fix1  fixn]row ih=(fi)h.\sum_{j}\frac{\partial f_i}{\partial x_j}h_j = \underbrace{\Big[\tfrac{\partial f_i}{\partial x_1}\ \cdots\ \tfrac{\partial f_i}{\partial x_n}\Big]}_{\text{row }i}\,\mathbf{h} = (\nabla f_i)^\top \mathbf{h}.

Why this step? A weighted sum of hjh_j is exactly a matrix row times h\mathbf{h}. That row is the gradient of fif_i.

Step 3 — Stack all mm rows. Doing this for every output simultaneously:

f(x+h)f(x)+Jh,J=[(f1)(fm)].\mathbf{f}(\mathbf{x}+\mathbf{h}) \approx \mathbf{f}(\mathbf{x}) + J\mathbf{h}, \qquad J = \begin{bmatrix}(\nabla f_1)^\top\\ \vdots \\ (\nabla f_m)^\top\end{bmatrix}.

Why this step? Stacking the row-approximations is the same as one matrix equation. The matrix whose rows are the gradients is the Jacobian. ∎


Worked examples


Common mistakes (Steel-man + fix)


Recall Feynman: explain it to a 12-year-old

Imagine a machine that takes a few dials as input and lights up a few bulbs as output. If you nudge one dial a tiny bit, each bulb gets a little brighter or dimmer. The Jacobian is just the table of all those "how much brighter per nudge" numbers — one number for each (bulb, dial) pair. If you know this table, you can predict what happens for any small twist of the dials without running the machine: just multiply your twist by the table.


Active-recall flashcards

What is the shape of the Jacobian of f:RnRm\mathbf f:\mathbb R^n\to\mathbb R^m?
m×nm\times n — rows = outputs, columns = inputs.
Define the (i,j)(i,j) entry of the Jacobian.
Jij=fi/xjJ_{ij}=\partial f_i/\partial x_j.
What is row ii of the Jacobian?
The transpose of the gradient of output fif_i, i.e. (fi)(\nabla f_i)^\top.
For a scalar function (m=1m=1), how do Jacobian and gradient relate?
The Jacobian is a row vector equal to (f)(\nabla f)^\top; the gradient is its transpose (a column).
State the first-order linear approximation using the Jacobian.
f(x+h)f(x)+Jh\mathbf f(\mathbf x+\mathbf h)\approx \mathbf f(\mathbf x)+J\mathbf h.
Chain rule for h=gf\mathbf h=\mathbf g\circ\mathbf f in Jacobian form?
Jh=JgJfJ_{\mathbf h}=J_{\mathbf g}\,J_{\mathbf f} (outer on the left; order matters).
What does detJ|\det J| mean, and when is it defined?
Local volume-scaling factor of the map; defined only when JJ is square (m=nm=n).
Jacobian of z=Wx\mathbf z=W\mathbf x w.r.t. x\mathbf x?
WW itself.
Jacobian of elementwise a=σ(z)\mathbf a=\sigma(\mathbf z) w.r.t. z\mathbf z?
diag(σ(z1),,σ(zm))\operatorname{diag}(\sigma'(z_1),\dots,\sigma'(z_m)).
detJ\det J for polar map (rcosθ,rsinθ)(r\cos\theta,r\sin\theta)?
rr.

Connections

Concept Map

generalizes to

has slope table

defined as

shape

rows equal outputs

columns equal inputs

built from

Taylor expansion

sum as dot product

stack m rows

used in

Scalar derivative f prime x

Jacobian matrix J

Vector function f R^n to R^m

Partial derivatives dfi dxj

m x n matrix

Row i is gradient of fi

Column j is input xj response

Linear approximation requirement

Per-output first order expansion

Row times h equals grad fi dot h

ML backprop and optimization

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek normal function f(x)f(x) ka toh sirf ek derivative hota hai — ek number jo slope batata hai. Lekin ML me functions aksar vector in, vector out hote hain: nn inputs andar jaate hain, mm outputs bahar aate hain. Ab ek slope se kaam nahi chalega — humein har (output, input) pair ke liye ek partial derivative chahiye. In sab partials ki jo table banti hai, wahi hai Jacobian matrix, size m×nm \times n (rows = outputs, columns = inputs — yaad rakho "ROC").

Iska asli funda ye hai: Jacobian function ka best linear approximation hai kisi point ke aas-paas. Matlab f(x+h)f(x)+Jh\mathbf{f}(\mathbf{x}+\mathbf{h}) \approx \mathbf{f}(\mathbf{x}) + J\mathbf{h}. Jaise scalar case me f(x+h)f(x)+f(x)hf(x+h)\approx f(x)+f'(x)h, waise hi yahan ff' ki jagah matrix JJ aa gaya. Har row actually ek output ka gradient hai — bas sab gradients ko upar-neeche stack kar do, Jacobian ban gaya.

ML me iski importance? Backpropagation basically Jacobians ka multiplication hi hai. Linear layer z=Wx\mathbf{z}=W\mathbf{x} ka Jacobian seedha WW hai, aur elementwise activation σ\sigma ka Jacobian ek diagonal matrix diag(σ(zi))\operatorname{diag}(\sigma'(z_i)) hai. Chain rule ka matlab hai in Jacobians ko multiply karo — lekin dhyan rakho, order matter karta hai: outer function ka Jacobian left me aata hai, JgJfJ_{\mathbf g} J_{\mathbf f}.

Do galtiyan jo sab karte hain: (1) Jacobian ko gradient samajh lena — gradient toh sirf scalar output (m=1m=1) ke liye hota hai, aur wo Jacobian ka transpose hai. (2) Determinant har jagah lagana — detJ\det J sirf tab valid hai jab matrix square ho (m=nm=n), aur tab detJ|\det J| batata hai ki chhota sa area/volume kitna stretch hua.

Go deeper — visual, from zero

Test yourself — Calculus & Optimization Basics

Connections