Skip to content

Jacobians and vector–Jacobian products

This page translates the backward calculation into matrix notation. You can understand the main sequence without it. Use it when a lecture writes J, VJP, or a row of derivatives and skips the intermediate arithmetic.

One derivative for each output–input pair

Section titled “One derivative for each output–input pair”

A Jacobian is a table of partial derivatives: one row per output, one column per input. If a function has n inputs and m outputs, its Jacobian has shape m×nm\times n.

Take F(x,y)=(u,v)=(x+y,xy)F(x,y)=(u,v)=(x+y,xy). Its Jacobian is

JF=[u/xu/yv/xv/y]=[11yx].J_F=\begin{bmatrix} \partial u/\partial x&\partial u/\partial y\\ \partial v/\partial x&\partial v/\partial y \end{bmatrix} =\begin{bmatrix}1&1\\y&x\end{bmatrix}.

At (x,y) = (2,3), this is [1132]\begin{bmatrix}1&1\\3&2\end{bmatrix}. An input change (0.01,0.02)(0.01,0.02) predicts output changes

[1132][0.010.02]=[0.030.07].\begin{bmatrix}1&1\\3&2\end{bmatrix} \begin{bmatrix}0.01\\0.02\end{bmatrix} =\begin{bmatrix}0.03\\0.07\end{bmatrix}.

The actual changes are 0.03 and 0.0702. The second output has a small product term that the first-order prediction omits.

Now let the final scalar be L=uvL=uv. At u = 5 and v = 6, its derivatives form the row [L/u,L/v]=[6,5][\partial L/\partial u,\partial L/\partial v]=[6,5].

Multiply that row by JFJ_F:

[65]1×2[1132]2×2=[2116]1×2.\underbrace{\begin{bmatrix}6&5\end{bmatrix}}_{1\times2} \underbrace{\begin{bmatrix}1&1\\3&2\end{bmatrix}}_{2\times2} =\underbrace{\begin{bmatrix}21&16\end{bmatrix}}_{1\times2}.

For x, the two paths contribute 61+53=216\cdot1+5\cdot3=21. For y, they contribute 61+52=166\cdot1+5\cdot2=16. This is the same “multiply along, add across” rule in a matrix product.

A vector–Jacobian product, or VJP, takes a row of downstream derivatives and returns a row of upstream derivatives. Implementations can compute this product directly without constructing the full Jacobian.

So far the loss derivatives are rows. Many texts store the gradient as a column instead. Transpose the relation:

(x,y)L2×1=JFT2×2(u,v)L2×1.\underbrace{\nabla_{(x,y)}L}_{2\times1} =\underbrace{J_F^\mathsf T}_{2\times2} \underbrace{\nabla_{(u,v)}L}_{2\times1}.

Same entries, different orientation. Check the author’s convention and matrix dimensions before multiplying.

For column vectors xRnx\in\mathbb R^n, z=Wx+bRmz=Wx+b\in\mathbb R^m, and downstream gradient gzRmg_z\in\mathbb R^m:

DerivativeCalculationShape
With respect to the inputgx=WTgzg_x=W^\mathsf T g_zn×1n\times1
With respect to the matrixL/W=gzxT\partial L/\partial W=g_zx^\mathsf Tm×nm\times n
With respect to the biasL/b=gz\partial L/\partial b=g_zm×1m\times1

An entry of the weight gradient is (gz)ixj(g_z)_i x_j: downstream derivative at unit i times input j. In the small network, this builds the matrix with rows (−0.95, −1.9) and (0.475, 0.95).

For an elementwise activation, each coordinate multiplies its incoming derivative by its local slope. ReLU uses 1 for positive inputs and 0 for negative inputs, with an explicitly chosen backward convention at zero.

A Jacobian–vector product computes JvJv: the outputs’ response to one input direction. A VJP computes a row times J: how one weighted combination of outputs depends on the inputs.

For a scalar loss and many parameters, reverse mode obtains the whole gradient with one backward sweep. Forward mode is useful for an individual input direction, and obtaining the full Jacobian by forward mode need not involve multiplying full matrices. Which mode is economical depends on the quantities you need.

Return to the reading order.

Sources and further reading

Definition

Read the full glossary entry →