Guides / Matrix calculus
Batched backprop for a dense layer: dW = XᵀdY, db = dYᵀ1, dX = dY Wᵀ, and the 1/N applied once
Short answer
For Y = XW + 1bT with X of size N × d, W of size d × m and b of length m, given dY (N × m) from the layer above: dW = XTdY (d × m), db = dYT1, the column sums of dY (length m), and dX = dY WT (N × d). With a mean loss the 1/N is already inside dY, and nothing after it divides again. With micro-batches of sizes Bj, the full-batch gradient is Σj (Bj/N) gj.
Why it works
In index form, Ynj = Σi XniWij + bj. The weight Wij appears in column j of every row, multiplied by Xni, so its gradient adds up Xni dYnj over the examples n. A sum over a shared index is a matrix product, and the shared index is the batch, so it has to be the inner dimension: XTdY. The same sum, with coefficient 1, gives the bias its column sums.
The input is different. Xni appears only in row n of Y, once per output feature, so its gradient sums over the features, Σj dYnjWij, and every example keeps its own row. That is the only place W is used on the way back, and it runs from m outputs back to d inputs, which is what WT does. Checking that each result has the shape of its variable catches most slips, as long as d ≠ m.
Example 1: the forward pass and its shapes
Take three examples with two features, X = 1201−11, two outputs with W = 1−120, and b = (0, 1). Find Y.
- XW is (3 × 2)(2 × 2) = 3 × 2N = 3, d = 2, m = 2. W has one row per input feature.
- row 1: (1, 2)W = (5, −1)1 × (1, −1) + 2 × (2, 0): a row of X combines the rows of W.
- rows 2 and 3: (2, 0) and (1, 1)(0, 1)W is W's second row; (−1, 1)W = (−1, 1) + (2, 0).
- 1bT adds (0, 1) to every rowThe bias is broadcast: each example uses the same b.
- Y = 502112Row n is x(n)TW + bT, the layer applied to example n alone. The batch is three copies of one layer sharing W and b.
Example 2: dW, db and dX from an upstream gradient
The layer above sends back dY = 100−121. Find dW, db and dX.
- dW = XTdY(2 × 3)(3 × 2) = 2 × 2, the shape of W. The batch index is summed away.
- row 1: (1, 0) − (2, 1)Row i of XTdY weights the rows of dY by column i of X, here (1, 0, −1) …
- dW = −1−140… and row 2 is 2(1, 0) + 1(0, −1) + 1(2, 1) = (4, 0).
- db = dYT1 = (3, 0)The column sums of dY: 1 + 0 + 2 and 0 − 1 + 1.
- dX = dY WT(3 × 2)(2 × 2) = 3 × 2, the shape of X. WT has rows (1, 2) and (−1, 0).
- dX = 121014Row 1 is 1 × (1, 2) + 0 × (−1, 0). Each row uses only its own row of dY. Central finite differences agree with all three gradients.
Example 3: a mean squared loss, with the 1/N in one place
Keep X, W and b, and use L = ‖Y − T‖F2/(2N) with targets T = 402211. Find dY, dW and db, and check dW against the per-example outer products.
- Y − T = 100−101So L = (1 + 1 + 1)/6 = 0.5.
- dY = (Y − T)/N = (Y − T)/3The 1/N of the mean enters here, once.
- dW = XTdYThe rule of Example 2, which never asked where dY came from …
- dW = 1/3−1/32/30… so it carries the 1/3 without applying it again.
- Σn x(n)dy(n)T gives the same matrixExample 1 contributes (1/3)(1, 2)T(1, 0), example 2 contributes (1/3)(0, 1)T(0, −1), example 3 adds the rest.
- db = dYT1 = (1/3, 0)A plain sum: the average is already built in. Finite differences of L agree with dW and db.
Example 4: micro-batches of unequal size
Memory allows only two examples at a time, so the batch of Example 3 is split into examples 1–2 and example 3, each with its own mean loss. Combine the two weight gradients into the full-batch one.
- g1 = 0.501−0.5Example 3's rule on the first two rows, with 1/2 in place of 1/3.
- g2 = 0−101One row, so the mean is the row itself: (−1, 1)T(0, 1).
- L = (2/3)L1 + (1/3)L2Each mean is weighted by its share of the N = 3 examples.
- dW = (2/3)g1 + (1/3)g2The gradient of a weighted sum.
- dW = 1/3−1/32/30Example 3's answer. The plain average (g1 + g2)/2 gives 0.25−0.50.50.25, which overweights the lone example. It is right only when the sizes are equal.
Common mistakes, and the exact line they happen on
1. The weight gradient in the other layout
The weight gradient of Example 2.
- Y = XW + 1bT, W is d × mRight so far.
- dW = dYTXThis is the mistake. That is the rule for W stored outputs by inputs, as in XWT. Here it gives −14−10, the transpose of the answer. With d = m the update runs anyway. Wij multiplies Xni, so X goes on the left: XTdY.
2. Dividing by N twice
The bias gradient of Example 3.
- dY = (Y − T)/3Right so far: the mean's 1/N is inside dY.
- db = dYT1/N = (1/9, 0)This is the mistake. "The bias gradient is the average over the batch" describes the result, not the formula. Averaging a dY that already carries 1/N makes the bias learn N times too slowly. The answer is the plain sum, (1/3, 0).
3. W where Wᵀ belongs
The input gradient of Example 2.
- dX must be 3 × 2, like XRight so far.
- dX = dY WThis is the mistake. The forward pass multiplies by W, but the backward pass runs from outputs to inputs. With a square W the product still runs, giving rows (1, −1), (−2, 0) and (4, −2) instead of (1, 2), (1, 0) and (1, 4). The sum is over W's second index, so it is dY WT.
Practice
A layer maps 784 features to 128 on a batch of 64. What are the shapes of dW, db and dX?
Show answer
dW is 784 × 128, db has length 128 and dX is 64 × 784.
Find dW for X = 1021 and dY = 1−102.
Show answer
XTdY has rows (1, 3) and (0, 2).
With a mean loss over two examples, dY has rows (0.5, 0) and (−0.25, 0.25). Find db.
Show answer
(0.25, 0.25), the column sums. The 1/2 is already inside dY.
W is 2 × 3 with rows (1, 2, 0) and (0, 1, −1). One row of dY is (1, 0, 2). Find that row of dX.
Show answer
(1, −2): the dot product of (1, 0, 2) with each row of W.
A batch of 8 is run as micro-batches of 6 and 2, each with its own mean loss and gradients g1 and g2. What is the full-batch gradient?
Show answer
(3/4)g1 + (1/4)g2, the weights 6/8 and 2/8.