Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Matrix and vector calculus

Layout Conventions: Numerator vs Denominator

When derivatives involve vectors and matrices, you must decide how to arrange the numbers. Rows for outputs or rows for inputs? Mixing the two conventions is one of the most common sources of bugs in hand-derived gradients. Learn both, a shape check that catches errors, and the layout-free differential method.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Agreeing which way the table goes

Suppose you write down how each of 3 outputs changes when each of 2 inputs is nudged. That’s 6 numbers in a table. But does each row belong to an output, or to an input? Both choices work, like driving on the left or on the right, as long as everyone agrees.

The two choices are called numerator and denominator layout. Swapping rows and columns turns one into the other. That swap is called a transpose.

Matrix-calculus explorer: formula vs finite differences
shapes: A: 3×2, x: 2, y: 3
rule: ∂y/∂x = A (numerator) or Aᵀ (denominator) · showing numerator (m×n) layout
input (x or X) · 2×1
-0.300
0.500
closed-form derivative · 3×2
-0.8001.800
-1.7000.600
-1.1001.600
finite-difference check · 3×2
-0.8001.800
-1.7000.600
-1.1001.600
largest difference: 8.8e-11 ✔ the formula is right
A vector-valued map: its derivative is a full matrix (the Jacobian).
Why engineers care

A robot-learning engineer might copy one formula from a textbook that uses one convention and another formula from a paper that uses the other. Multiplied together, they give nonsense, and the robot learns nothing. Knowing both conventions, and checking shapes, prevents this.

📝 Quick check (Level 0)
0/3 answered
  1. 1. 3 outputs, 2 inputs: how many numbers in the table?
  2. 2. Swapping rows and columns is called…
  3. 3. Do both layouts describe the same derivative?

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The two layouts

For y∈Rm\mathbf y\in\mathbb R^m depending on x∈Rn\mathbf x\in\mathbb R^n:

Layout Shape of ∂y/∂x\partial\mathbf y/\partial\mathbf x Entry (i,j)(i,j) y=Ax\mathbf y = A\mathbf x gives
numerator (Jacobian) m×nm\times n ∂yi/∂xj\partial y_i/\partial x_j AA
denominator (gradient) n×mn\times m ∂yj/∂xi\partial y_j/\partial x_i A⊤A^\top

For a scalar ff, the numerator layout gives a row (1×n1\times n) and the denominator layout gives a column (n×1n\times1). In this course, ∇f\nabla f is a column (the same shape as x\mathbf x), and Jacobians are numerator layout (A3.2-5).

Example. f=a⊤xf = \mathbf a^\top\mathbf x gives ∇f=a\nabla f = \mathbf a.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Numerator layout ∂y/∂x for y ∈ ℝ⁴, x ∈ ℝ² has how many rows?
  2. 2. Denominator layout for the same?
  3. 3. ∇(aᵀx) = ?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The chain rule in each layout

For z(y(x))z(\mathbf y(\mathbf x)):

∂z∂x⏟numerator=∂z∂y∂y∂x,∇xz⏟denominator=(∂y∂x)⊤∇yz\underbrace{\frac{\partial z}{\partial\mathbf x}}_{\text{numerator}} = \frac{\partial z}{\partial\mathbf y}\frac{\partial\mathbf y}{\partial\mathbf x}, \qquad \underbrace{\nabla_{\mathbf x}z}_{\text{denominator}} = \Big(\frac{\partial\mathbf y}{\partial\mathbf x}\Big)^\top\nabla_{\mathbf y}z

Numerator layout multiplies left to right. Denominator layout multiplies transposes in reverse order. Backpropagation (A3.3-5) is exactly the denominator form, read from the loss backwards.

The shape test

Before trusting a derivation, check the shapes. For f=∥Ax−b∥2f = \lVert A\mathbf x - \mathbf b\rVert^2 with A∈R3×2A\in\mathbb R^{3\times2}, the gradient must be a 2-vector. The rule 2A⊤(Ax−b)2A^\top(A\mathbf x - \mathbf b) is (2×3)(3×1)=2×1(2\times3)(3\times1) = 2\times1, which passes. A wrong guess like 2(Ax−b)A2(A\mathbf x - \mathbf b)A fails to multiply.

Derivative of with respect to shape (this course)
scalar vector x∈Rn\mathbf x\in\mathbb R^n nn (like x\mathbf x)
vector y∈Rm\mathbf y\in\mathbb R^m vector x∈Rn\mathbf x\in\mathbb R^n m×nm\times n (Jacobian)
scalar matrix X∈Rp×qX\in\mathbb R^{p\times q} p×qp\times q (like XX)
🐍 Exercise 1: both layouts, numerically
Build numerator-layout Jacobians by finite differences, and get the denominator layout by transposing. Check y = Ax gives A and Aᵀ, and that a scalar gives a row or a column.
📝 Quick check (Level 2)
0/3 answered
  1. 1. ∇ of ‖Ax − b‖² w.r.t. x is…
  2. 2. Denominator-layout chain rule multiplies…
  3. 3. The gradient of a scalar w.r.t. X ∈ ℝ^(3×4) has how many entries?

Level 3: Advanced

★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.

The derivative is layout-free: use differentials

The derivative itself is a linear map dx↦dyd\mathbf x\mapsto d\mathbf y. Layouts are just two ways of writing its matrix. The differential method (Magnus and Neudecker, 1988) avoids the choice entirely:

  1. Compute the differential dfdf with ordinary rules: d(AX)=A dXd(AX) = A\,dX, d(X⊤)=(dX)⊤d(X^\top) = (dX)^\top, d(tr⁡X)=tr⁡(dX)d(\operatorname{tr}X) = \operatorname{tr}(dX).
  2. Rearrange into df=tr⁡(G⊤dX)=⟨G,dX⟩df = \operatorname{tr}(G^\top dX) = \langle G, dX\rangle.
  3. Then ∂f/∂X=G\partial f/\partial X = G, with the same shape as XX.

Example. For f=tr⁡(AX)f = \operatorname{tr}(AX): df=tr⁡(A dX)=⟨A⊤,dX⟩df = \operatorname{tr}(A\,dX) = \langle A^\top, dX\rangle, so ∂f/∂X=A⊤\partial f/\partial X = A^\top.

Example. For f=x⊤xf = \mathbf x^\top\mathbf x: df=2x⊤dxdf = 2\mathbf x^\top d\mathbf x, so ∇f=2x\nabla f = 2\mathbf x.

Why the reversal in denominator layout

Transposing the numerator chain rule gives (∂z∂y∂y∂x)⊤=(∂y∂x)⊤(∂z∂y)⊤\Big(\frac{\partial z}{\partial\mathbf y}\frac{\partial\mathbf y}{\partial\mathbf x}\Big)^\top = \Big(\frac{\partial\mathbf y}{\partial\mathbf x}\Big)^\top\Big(\frac{\partial z}{\partial\mathbf y}\Big)^\top, because (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top. ∎

Exercise 3.1: Use differentials to find ∂f/∂W for f = ½‖WX − Y‖_F²

Let R=WX−YR = WX - Y. Then df=tr⁡(R⊤dR)=tr⁡(R⊤dW X)=tr⁡(XR⊤dW)=⟨RX⊤,dW⟩df = \operatorname{tr}(R^\top dR) = \operatorname{tr}(R^\top dW\,X) = \operatorname{tr}(XR^\top dW) = \langle RX^\top, dW\rangle. So ∂f/∂W=(WX−Y)X⊤\partial f/\partial W = (WX - Y)X^\top, which has the same shape as WW.

📝 Quick check (Level 3)
0/3 answered
  1. 1. ∂tr(AX)/∂X = ?
  2. 2. Is the derivative (as a linear map) layout-dependent?
  3. 3. The reversal in denominator layout comes from…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Conventions in real software

  • Autodiff frameworks. torch.autograd.functional.jacobian and jax.jacrev/jacfwd return arrays of shape (output shape…, input shape…), which is numerator layout. But .grad / jax.grad of a scalar loss has the same shape as the parameter, the denominator-like convention used throughout this course.
  • References differ. The Matrix Cookbook (Petersen and Pedersen, 2012) mostly uses numerator layout. Many ML papers use denominator layout. Always check a source’s convention before combining formulas.
  • Shape bugs are a leading cause of silent errors in research code. They can broadcast without crashing. Assertions on shapes, einops-style named dimensions, and gradient checks (A3.1-2) catch them.
🐍 Exercise 2 (research): one gradient, two layouts
For L = aᵀ W₂ tanh(W₁x), compute ∂L/∂x in numerator layout (a row, multiplied left to right) and denominator layout (a column, transposes in reverse). Check both against finite differences.

References

  1. J. Magnus, H. Neudecker (1988; 3rd ed. 2019). Matrix Differential Calculus with Applications in Statistics and Econometrics. Wiley.
  2. K. B. Petersen, M. S. Pedersen (2012). The Matrix Cookbook. Technical University of Denmark.
  3. T. Minka (2000). “Old and new matrix algebra useful for statistics.” MIT Media Lab note.
  4. A. G. Baydin, B. Pearlmutter, A. Radul, J. Siskind (2018). “Automatic differentiation in machine learning: a survey.” JMLR, 18.
  5. J. Bradbury et al. (2018). JAX: composable transformations of Python+NumPy programs. (Documentation of jacfwd/jacrev conventions.)