TExcelTAD
Home › Math for Machine Learning › Gradients of Vector-Valued Functions

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 f:Rn→Rm\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m with components f1,…,fmf_1, \ldots, f_m, stack the gradients of the components as rows:

J=dfdx=[∂f1∂x1⋯∂f1∂xn⋮⋮∂fm∂x1⋯∂fm∂xn]∈Rm×n,Jij=∂fi∂xjJ = \frac{d\mathbf{f}}{d\mathbf{x}} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \vdots & & \vdots \\ \frac{\partial f_m}{\partial x_1} & \cdots & \frac{\partial f_m}{\partial x_n} \end{bmatrix} \in \mathbb{R}^{m \times n}, \qquad J_{ij} = \frac{\partial f_i}{\partial x_j}

Rows index outputs, columns index inputs. The Jacobian is the best linear approximation: f(x+δ)≈f(x)+Jδ\mathbf{f}(\mathbf{x} + \boldsymbol{\delta}) \approx \mathbf{f}(\mathbf{x}) + J\boldsymbol{\delta}.

Linear map. For f(x)=Ax\mathbf{f}(\mathbf{x}) = A\mathbf{x}, J=AJ = A. For an affine map Ax+bA\mathbf{x} + \mathbf{b}, also J=AJ = A.

Elementwise functions. For f(x)=(σ(x1),…,σ(xn))\mathbf{f}(\mathbf{x}) = (\sigma(x_1), \ldots, \sigma(x_n)), output ii depends only on input ii, so the Jacobian is diagonal: J=diag⁡(σ′(x1),…,σ′(xn))J = \operatorname{diag}(\sigma'(x_1), \ldots, \sigma'(x_n)).

The chain rule as matrix multiplication

If y=g(x)\mathbf{y} = \mathbf{g}(\mathbf{x}) and z=f(y)\mathbf{z} = \mathbf{f}(\mathbf{y}), then

dzdx=dzdy dydx\frac{d\mathbf{z}}{d\mathbf{x}} = \frac{d\mathbf{z}}{d\mathbf{y}}\,\frac{d\mathbf{y}}{d\mathbf{x}}

with shapes (k×m)(m×n)=k×n(k \times m)(m \times n) = k \times n. A one-layer network h=σ(Wx+b)\mathbf{h} = \sigma(W\mathbf{x} + \mathbf{b}) therefore has Jacobian diag⁡(σ′(a)) W\operatorname{diag}(\sigma'(\mathbf{a}))\,W with respect to x\mathbf{x}, where a=Wx+b\mathbf{a} = W\mathbf{x} + \mathbf{b}.

python · try it yourself
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 z\mathbf{z} into probabilities pi=ezi/∑kezkp_i = e^{z_i}/\sum_k e^{z_k}. Each output depends on every input, and

∂pi∂zj=pi(δij−pj)  ⟹  J=diag⁡(p)−ppT\frac{\partial p_i}{\partial z_j} = p_i(\delta_{ij} - p_j) \implies J = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^T

(δij\delta_{ij} is 1 if i=ji = j and 0 otherwise). Its rows sum to 0, because the probabilities always sum to 1 whatever the scores do.

python · try it yourself
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 f:Rn→Rn\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^n, ∣det⁡J∣\lvert\det J\rvert is the local factor by which f\mathbf{f} scales volume. It is what the change-of-variables formula for densities needs.

In software

  • torch.autograd.functional.jacobian and jax.jacfwd / jax.jacrev compute 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 p−y\mathbf{p} - \mathbf{y} (Softmax and Cross-Entropy Gradient).

Exercises

Exercise 1: A 2×22 \times 2 Jacobian

Find the Jacobian of f(x,y)=(x2y,sin⁡x+y)\mathbf{f}(x, y) = (x^2y, \sin x + y).

Answer

[2xyx2cos⁡x1]\begin{bmatrix} 2xy & x^2 \\ \cos x & 1 \end{bmatrix}.

Solution

Row 1 is the gradient of x2yx^2y: (2xy,x2)(2xy, x^2). Row 2 is the gradient of sin⁡x+y\sin x + y: (cos⁡x,1)(\cos x, 1).

Exercise 2: Shapes in the chain rule

g:R10→R4\mathbf{g}: \mathbb{R}^{10} \to \mathbb{R}^{4} and f:R4→Rf: \mathbb{R}^4 \to \mathbb{R}. What are the shapes of dfdy\frac{df}{d\mathbf{y}}, dydx\frac{d\mathbf{y}}{d\mathbf{x}} and dfdx\frac{df}{d\mathbf{x}}?

Answer

1×41 \times 4, 4×104 \times 10, 1×101 \times 10.

Solution

A scalar's gradient with respect to a 4-vector is a 1×41 \times 4 row; g\mathbf{g}'s Jacobian has one row per output and one column per input; the product is 1×101 \times 10, the gradient with respect to x\mathbf{x}.

Exercise 3: Softmax Jacobian by hand (code)

For p=(0.5,0.5)\mathbf{p} = (0.5, 0.5), write diag⁡(p)−ppT\operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^T and confirm with NumPy.

Answer

[0.25−0.25−0.250.25]\begin{bmatrix} 0.25 & -0.25 \\ -0.25 & 0.25 \end{bmatrix}.

Solution

Diagonal: 0.5−0.25=0.250.5 - 0.25 = 0.25. Off-diagonal: −0.5⋅0.5=−0.25-0.5 \cdot 0.5 = -0.25.

python · try it yourself
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 f:Rn→Rm\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m has shape…

  1. n×mn \times m
  2. m×nm \times n
  3. m×mm \times m
  4. 1×n1 \times n

The Jacobian of an elementwise activation σ(x)\sigma(\mathbf{x}) is…

  1. full
  2. diagonal
  3. zero
  4. the identity

The rows of the softmax Jacobian sum to…

  1. 1
  2. 0
  3. pip_i
  4. the number of classes