Gradients of Vector-Valued Functions
A neural layer takes a vector in and gives a vector out. Its derivative is not a single row but a matrix of partial derivatives, the Jacobian. Jacobians make the chain rule for vector functions a matrix product, which is exactly how backpropagation is organized.
The Jacobian
For with components , stack the gradients of the components as rows:
Rows index outputs, columns index inputs. The Jacobian is the best linear approximation: .
Linear map. For , . For an affine map , also .
Elementwise functions. For , output depends only on input , so the Jacobian is diagonal: .
The chain rule as matrix multiplication
If and , then
with shapes . A one-layer network therefore has Jacobian with respect to , where .
import numpy as np
def sigmoid(a): return 1 / (1 + np.exp(-a))
rng = np.random.default_rng(0)
W, b = rng.normal(size=(2, 3)), rng.normal(size=2)
x = np.array([0.5, -1.0, 2.0])
f = lambda v: sigmoid(W @ v + b)
a = W @ x + b
J_formula = np.diag(sigmoid(a) * (1 - sigmoid(a))) @ W
h = 1e-6
J_numeric = np.column_stack([(f(x + h * e) - f(x - h * e)) / (2 * h) for e in np.eye(3)])
print("Jacobian shape:", J_formula.shape)
print(np.round(J_formula, 6))
print("matches numeric:", np.allclose(J_formula, J_numeric, atol=1e-8))Jacobian shape: (2, 3)
[[ 0.006917 -0.007267 0.03523 ]
[ 0.008987 -0.045893 0.030979]]
matches numeric: True
The softmax Jacobian
The softmax turns scores into probabilities . Each output depends on every input, and
( is 1 if and 0 otherwise). Its rows sum to 0, because the probabilities always sum to 1 whatever the scores do.
import numpy as np
z = np.array([2.0, 1.0, -1.0])
p = np.exp(z - z.max()); p /= p.sum()
J = np.diag(p) - np.outer(p, p)
h = 1e-6
def softmax(v): e = np.exp(v - v.max()); return e / e.sum()
J_num = np.column_stack([(softmax(z + h * e) - softmax(z - h * e)) / (2 * h) for e in np.eye(3)])
print("p:", np.round(p, 4))
print("rows sum to 0:", np.allclose(J.sum(axis=1), 0), " matches numeric:", np.allclose(J, J_num, atol=1e-8))p: [0.7054 0.2595 0.0351]
rows sum to 0: True matches numeric: True
Jacobian determinant
For , is the local factor by which scales volume. It is what the change-of-variables formula for densities needs.
In software
torch.autograd.functional.jacobianandjax.jacfwd/jax.jacrevcompute full Jacobians.- Backprop never builds big Jacobians explicitly: it multiplies a gradient row vector by each Jacobian (a vector-Jacobian product), which for elementwise layers is just an elementwise product.
- The softmax Jacobian shows up when you combine softmax with cross-entropy, where it simplifies to (Softmax and Cross-Entropy Gradient).
Exercises
Exercise 1: A Jacobian
Find the Jacobian of .
Answer
.
Solution
Row 1 is the gradient of : . Row 2 is the gradient of : .
Exercise 2: Shapes in the chain rule
and . What are the shapes of , and ?
Answer
, , .
Solution
A scalar's gradient with respect to a 4-vector is a row; 's Jacobian has one row per output and one column per input; the product is , the gradient with respect to .
Exercise 3: Softmax Jacobian by hand (code)
For , write and confirm with NumPy.
Answer
.
Solution
Diagonal: . Off-diagonal: .
import numpy as np
p = np.array([0.5, 0.5])
print(np.diag(p) - np.outer(p, p))[[ 0.25 -0.25]
[-0.25 0.25]]
Test yourself
The Jacobian of has shape…
The Jacobian of an elementwise activation is…
- full
- diagonal
- zero
- the identity
The rows of the softmax Jacobian sum to…
- 1
- 0
- the number of classes