Guides / Matrix calculus
Matrix calculus conventions: numerator layout, gradients shaped like the variable, and the derivative read off the differential
Short answer
Vectors are columns. For y ∈ ℝm and x ∈ ℝn, the Jacobian ∂y/∂x is m × n, with entry ∂yi/∂xj. For a scalar f, ∂f/∂x is a 1 × n row and the gradient ∇f is its transpose, an n × 1 column. Read it off the differential: if df = gTdx, then ∇f = g; if df = tr(GTdX), then ∇Xf = G, shaped like X. The standard results: ∂(Ax)/∂x = A, ∇(aTx) = a, ∇(xTAx) = (A + AT)x and ∇W(aTWb) = abT.
Why it works
A derivative is the best linear approximation: a small change dx in the input makes the change dy ≈ J dx in the output. For that product to make sense, J needs one column for each entry of x and one row for each entry of y, which is numerator layout. A scalar f has one output, so its derivative is a single row, and df = (∂f/∂x) dx is a row times a column.
The gradient is that row stood up as a column, so that it has the shape of x and a gradient step x − η∇f makes sense. The same reasoning gives a matrix variable a gradient with its own shape, and the trace turns the sum Σ Gij dXij into matrix form: tr(GTdX) = Σij GijdXij. Once a differential is in one of these forms, the gradient is whatever sits in the slot.
Example 1: a Jacobian from its entries
Let y = (x12 + x2, x1x2, 3x2). Find ∂y/∂x at x = (1, 2), and its shape.
- y ∈ ℝ3, x ∈ ℝ2Three outputs, two inputs, so the Jacobian is 3 × 2.
- row 1: (2x1, 1)The partials of y1 = x12 + x2 in x1, then x2.
- row 2: (x2, x1)The partials of y2 = x1x2.
- row 3: (0, 3)y3 = 3x2 doesn't depend on x1.
- J = 212103At (1, 2). Rows follow the outputs, columns the inputs. A small step dx changes y by about J dx: dx = (0.01, 0) moves y by (0.02, 0.02, 0).
Example 2: a gradient from the differential
Let f(x) = (aTx)2 with a = (1, 2). Find ∇f, then evaluate it at x = (3, 1).
- df = 2(aTx) d(aTx)The scalar chain rule: aTx is a number, and f is its square.
- d(aTx) = aTdxa is constant.
- df = (2(aTx)a)T dxThe scalar 2aTx can sit on either side, so the differential is in the form gTdx.
- ∇f = 2(aTx) aRead g off the slot. It is a column, the shape of x.
- aTx = 3 + 2 = 5At x = (3, 1).
- ∇f = 10a = (10, 20)Check by differences: f(3.001, 1) − f(2.999, 1) is about 0.02, and 0.02/0.002 = 10.
Example 3: a gradient with respect to a matrix
Let f(W) = aTWb with W a 2 × 3 matrix, a = (1, 2) and b = (3, −1, 2). Find ∇Wf.
- df = aT(dW)bf is linear in W, so its differential has the same form.
- = tr(aTdW b)A number is its own trace.
- = tr(baTdW)The trace doesn't change when a product is cycled: move b to the front.
- = tr((abT)TdW)baT = (abT)T, so the differential is in the form tr(GTdW).
- ∇Wf = abTRead G off the slot: a 2 × 1 times a 1 × 3, which is 2 × 3, the shape of W.
- = 3−126−24Entry (i, j) is aibj, the coefficient of Wij in Σ aiWijbj.
Example 4: the same derivative in the other layout
Let A = 123456 and c = (1, 0, −1). Find the Jacobian of y = Ax and the gradient of f = cTAx, in numerator layout and in the denominator layout some textbooks use.
- ∂y/∂x = A, 3 × 2Numerator layout: one row per entry of y.
- AT, 2 × 3Denominator layout lays out the same partials transposed, one row per entry of x.
- ∂f/∂x = cTA, 1 × 2Numerator layout: the row derivative.
- ∇f = ATcThe gradient is the transposed row, a column.
- = (1 − 5, 2 − 6)Rows of AT times c.
- ∇f = (−4, −4)In denominator layout ∂f/∂x is this column already. The two layouts disagree about Jacobians and agree about gradients, which always take the shape of the variable.
Common mistakes, and the exact line they happen on
1. Writing 2Ax for a matrix that isn't symmetric
Find ∇(xTAx) at x = (1, 1) for A = 1201.
- f = xTAxRight so far: f = x12 + 2x1x2 + x22.
- ∇f = 2Ax = (6, 2)This is the mistake. x appears twice, and the two copies give Ax + ATx. They match only if A = AT. Here (A + AT)x = (4, 4), and the partials of the expanded f agree: 2x1 + 2x2 = 4 both times.
2. A matrix gradient in the wrong shape
The gradient of Example 3, f(W) = aTWb.
- df = aT(dW)bRight so far.
- ∇Wf = baTThis is the mistake. baT is what multiplies dW inside the trace, which is GT, not G. It is 3 × 2, and W is 2 × 3, so W − η baT isn't defined. The gradient must have W's shape: abT.
3. Taking a Jacobian from a denominator-layout table
The map y = Ax of Example 4, with A a 3 × 2 matrix.
- ∇(cTAx) = ATcRight so far: a gradient is the same in both layouts.
- ∂(Ax)/∂x = ATThis is the mistake. That is the denominator-layout Jacobian copied into a numerator-layout derivation. It is 2 × 3, so dy ≈ ATdx multiplies a 2 × 3 by a 2 × 1, which isn't defined. In numerator layout the Jacobian is A. Pick one layout and convert every formula you borrow.
Practice
For y ∈ ℝ4 and x ∈ ℝ7, what shape is ∂y/∂x in numerator layout?
Show answer
4 × 7: one row per output.
Find ∇(xTx) at x = (1, −2, 2).
Show answer
2x = (2, −4, 4).
Find ∇(aTx + ‖x‖2).
Show answer
a + 2x.
Find ∇(xTAx) at x = (1, 1) for A = 0130.
Show answer
(A + AT)x = (4, 4). Not 2Ax = (2, 6).
W and C have the same shape. Find ∇W tr(WTC).
Show answer
C: tr(WTC) = Σ WijCij.