AI / ML Theory
Chapter 0 · 22 min

0.2 — Matrix calculus: derivatives that matter for backprop

Goal: Derive ∂(Wx+b)/∂x and ∂(Wx+b)/∂W with numerator convention, no 'it follows that'.

1. Convention: numerator vs. denominator

Before any derivative, fix the convention. For f:Rn→Rmf : \mathbb{R}^n \to \mathbb{R}^m given by y=f(x)y = f(x), numerator layout declares

∂y∂x∈Rm×n,(∂y∂x)ij=∂yi∂xj.\frac{\partial y}{\partial x} \in \mathbb{R}^{m \times n}, \qquad \Big(\frac{\partial y}{\partial x}\Big)_{ij} = \frac{\partial y_i}{\partial x_j}.

Denominator layout flips it: ∂y/∂x∈Rn×m\partial y / \partial x \in \mathbb{R}^{n \times m}. I pick numerator because it makes the chain rule stack matrices in the natural multiplication order. Every derivation in this course uses numerator layout. Mix them and you suffer.[1]

2. Derivative of Wx+bWx + b with respect to xx

Let f(x)=Wx+bf(x) = Wx + b with W∈Rm×nW \in \mathbb{R}^{m\times n}, b∈Rmb \in \mathbb{R}^m. Component-wise,

yi=∑k=1nWik xk+bi.y_i = \sum_{k=1}^{n} W_{ik}\, x_k + b_i.

Differentiating yiy_i with respect to xjx_j, every term with k≠jk \neq j vanishes:

∂yi∂xj=∂∂xj(∑kWik xk+bi)=∑kWik δkj=Wij.\frac{\partial y_i}{\partial x_j} = \frac{\partial}{\partial x_j} \Big( \sum_{k} W_{ik}\, x_k + b_i \Big) = \sum_{k} W_{ik}\, \delta_{kj} = W_{ij}.

Stacking over every (i,j)(i, j) pair,

  ∂(Wx+b)∂x=W  ∈Rm×n.  \boxed{\;\frac{\partial (Wx + b)}{\partial x} = W \;\in \mathbb{R}^{m\times n}.\;}

The intuition is direct: differentiating an affine map with respect to its input recovers its linear part.

3. Derivative with respect to WW

Here WW is a matrix, so ∂y/∂W\partial y / \partial W is in principle a rank-3 tensor: it has indices (i,j,k)(i, j, k) with ii ranging over yy and (j,k)(j, k) ranging over WW. The cleanest route is to differentiate index by index; the storage shape will fall out at the end.

∂yi∂Wjk=∂∂Wjk(∑lWil xl+bi)=∑lδij δlk xl=δij xk.\frac{\partial y_i}{\partial W_{jk}} = \frac{\partial}{\partial W_{jk}} \Big( \sum_{l} W_{il}\, x_l + b_i \Big) = \sum_{l} \delta_{ij}\, \delta_{lk}\, x_l = \delta_{ij}\, x_k.

Note the δij\delta_{ij}: the derivative of the ii-th output with respect to the element WjkW_{jk} is non-zero only when j=ij = i. In other words, the weight WjkW_{jk} affects only the output yjy_j.

4. Why this shows up as an outer product in backprop

At training time, what we need is not the full tensor but the gradient of a scalar loss LL with respect to WW. By the chain rule:

∂L∂Wjk=∑i∂L∂yi ∂yi∂Wjk=∑i∂L∂yi δij xk=∂L∂yj xk.\frac{\partial L}{\partial W_{jk}} = \sum_i \frac{\partial L}{\partial y_i}\, \frac{\partial y_i}{\partial W_{jk}} = \sum_i \frac{\partial L}{\partial y_i}\, \delta_{ij}\, x_k = \frac{\partial L}{\partial y_j}\, x_k.

Define δ:=∂L/∂y∈Rm\delta := \partial L / \partial y \in \mathbb{R}^{m}. In matrix form, ∂L/∂W\partial L / \partial W is the m×nm \times n matrix whose (j,k)(j,k) entry is δj xk\delta_j\, x_k — the outer product:

  ∂L∂W=δ x⊤  ∈Rm×n.  \boxed{\;\frac{\partial L}{\partial W} = \delta\, x^\top \;\in \mathbb{R}^{m\times n}.\;}

And for the bias, ∂L/∂b=δ\partial L/\partial b = \delta (each bib_i contributes 11 to yiy_i). This completes the local update rule of a linear layer; chained across all layers, it is PyTorch's backward().

5. Quick check (do it with a pen)

For W=(1234)W = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} and x=(5,6)⊤x = (5, 6)^\top, compute y=Wxy = Wx, then ∂y/∂x\partial y / \partial x, and finally — assuming L=12∥y∥2L = \tfrac{1}{2}\|y\|^2 — obtain δ\delta and the outer product δ x⊤\delta\, x^\top. If W⊤y x⊤W^\top y\, x^\top shows up in the slot where δ x⊤\delta\, x^\top should be, the numerator convention from §1 was crossed.


References

  1. [1]

    Magnus, J. R., & Neudecker, H. (2019). Matrix Differential Calculus with Applications in Statistics and Econometrics (3rd ed.). Wiley. — Standard reference for the numerator-convention matrix calculus of 0.2.

    ↩