Guides / Matrix calculus
Backprop for a one-hidden-layer network: δ₂ = ŷ − y, δ₁ = σ′(z₁) ⊙ W₂ᵀδ₂, and every gradient shaped like its weight
Short answer
The network: z1 = W1x + b1, h = σ(z1), z2 = W2h + b2, ŷ = softmax(z2), cross-entropy against a one-hot y. Output: δ2 = ŷ − y; ∇W2L = δ2hT; ∇b2L = δ2. Hidden: δ1 = σ′(z1) ⊙ W2Tδ2, with σ′(z1) = h ⊙ (1 − h); ∇W1L = δ1xT; ∇b1L = δ1; ∇xL = W1Tδ1. Batch with rows as examples and the mean loss: Δ2 = (Ŷ − Y)/N, ∇W2L = Δ2TH, and each bias gradient sums its delta's rows.
Why it works
Entry W2,ij appears in only one output, z2,i, multiplied by hj, so its gradient is δ2,ihj: the row index belongs to the delta and the column index to the input, which is the outer product δ2hT. Hidden unit j feeds every output through column j of W2, so its gradient collects Σi W2,ijδ2,i, which is W2Tδ2.
That is the gradient at h, not at z1. The sigmoid sits in between, and its Jacobian is diagonal, so one more elementwise product by σ′(z1) gives δ1. From there the first layer repeats the second's pattern with x as the input. In a batch, the parameters are shared by every example, so their gradients add up the examples' contributions, and the 1/N of the mean enters once, in Δ2.
Example 1: the forward pass and the output delta
Take x = (1, −1), W1 = 1121, W2 = 1−102, both biases 0, and target class 1, so y = (1, 0). Find ŷ, L and δ2.
- z1 = W1x = (0, 1)1 − 1 and 2 − 1.
- h = σ(z1) ≈ (0.5, 0.7311)Save h: the backward pass needs it twice.
- z2 = W2h ≈ (−0.2311, 1.4621)0.5 − 0.7311 and 2 × 0.7311.
- ŷ ≈ (0.1554, 0.8446)Softmax of z2. The network prefers the wrong class.
- L = −log ŷ1 ≈ 1.8620Cross-entropy keeps only the target's term.
- δ2 = ŷ − y ≈ (−0.8446, 0.8446)The gradient at z2. It sums to 0, and its negative entry is the class to push up.
Example 2: the output layer's gradients
With the values of Example 1, find ∇W2L, ∇b2L and ∇hL.
- ∇W2L = δ2hT(2 × 1)(1 × 2): the shape of W2, which is k × m.
- ≈ −0.4223−0.61750.42230.6175Row i is δ2,i times hT = (0.5, 0.7311).
- ∇b2L = δ2Each bias enters one output with coefficient 1.
- ∇hL = W2Tδ2The rows of W2T are (1, 0) and (−1, 2).
- ≈ (−0.8446, 2.5339)Unit 2 feeds class 2 with weight 2, so it gets the larger share of the blame. Finite differences of L agree with all three gradients.
Example 3: the hidden delta and the first layer
Continue to δ1, ∇W1L and ∇xL.
- σ′(z1) = h ⊙ (1 − h)From the saved h …
- ≈ (0.25, 0.1966)… with no exponential recomputed.
- δ1 = σ′(z1) ⊙ ∇hLThrough the sigmoid, entry by entry.
- ≈ (−0.2112, 0.4982)0.25 × (−0.8446) and 0.1966 × 2.5339.
- ∇W1L = δ1xTThe pattern of Example 2, one layer down.
- ≈ −0.21120.21120.4982−0.4982Row i is δ1,i times xT = (1, −1); ∇b1L = δ1.
- ∇xL = W1Tδ1 ≈ (0.7852, 0.2870)No update uses it, but it is what a layer below would receive. Every gradient here has the shape of its variable and matches a central finite difference.
Example 4: a batch of two
Add a second example, x′ = (0, 0) in class 2, and use the mean loss. With rows as examples, find Δ2, ∇W2L and ∇b2L.
- h′ = (0.5, 0.5)For x′ = 0, z1 = 0.
- z2′ = (0, 1)W2h′.
- ŷ′ ≈ (0.2689, 0.7311)Softmax of (0, 1), so its delta is (0.2689, −0.2689).
- Δ2 = (Ŷ − Y)/2The two deltas as rows, each divided by N = 2 …
- ≈ −0.42230.42230.1345−0.1345… once, here, and never again.
- ∇W2L = Δ2TH(2 × N)(N × 2): the mean of the two outer products, k × m again.
- ≈ −0.1439−0.24150.14390.2415The second example pulls against the first.
- ∇b2L = Δ2T1 ≈ (−0.2879, 0.2879)The column sums of Δ2: one bias is shared by the batch, so its gradient adds the rows. Finite differences of the mean loss agree.
Common mistakes, and the exact line they happen on
1. The outer product the wrong way round
The weight gradient of Example 2.
- δ2 ≈ (−0.8446, 0.8446)Right so far, with h ≈ (0.5, 0.7311).
- ∇W2L = hδ2TThis is the mistake. Writing the input first, in the order the data flows, gives the transpose: −0.42230.4223−0.61750.6175. Here m = k = 2, so the shape can't warn you. In δ2,ihj the row index is the output, so the delta comes first.
2. Skipping the activation's derivative
The hidden delta of Example 3.
- ∇hL ≈ (−0.8446, 2.5339)Right so far.
- δ1 = W2Tδ2This is the mistake. That is the gradient at h. The sigmoid between h and z1 multiplies each entry by σ′(z1), at most 1/4, so this is 4 and 5.1 times too large. The delta is (−0.2112, 0.4982).
3. A bias gradient that is still a matrix
The batch of Example 4.
- Δ2, 2 × 2Right so far: one row per example.
- ∇b2L = Δ2This is the mistake. For one example the bias gradient is the delta, but in a batch the same b2 is added to every row, so its gradient sums them: (−0.2879, 0.2879). Array code may broadcast b2 − ηΔ2 without complaint and quietly give each example its own bias.
Practice
A network has 784 inputs, 128 hidden units and 10 classes. What are the shapes of W1, W2 and their gradients?
Show answer
W1 and its gradient are 128 × 784; W2 and its gradient are 10 × 128.
The output is ŷ = (0.1, 0.7, 0.2) and the target is class 2. Find δ2.
Show answer
ŷ − y = (0.1, −0.3, 0.2).
Find ∇W2L for δ2 = (0.2, −0.2) and h = (1, 0.5).
Show answer
δ2hT: rows (0.2, 0.1) and (−0.2, −0.1).
With ReLU hidden units, z1 = (1.2, −0.3, 0.5) and W2Tδ2 = (0.4, 0.9, −0.6). Find δ1.
Show answer
(0.4, 0, −0.6): the mask zeroes the unit with negative pre-activation.
The loss adds (λ/2)‖W2‖F2 with λ = 0.01. What does it add to the gradient of an entry of W2 equal to 3?
Show answer
λ × 3 = 0.03: the penalty's gradient is λW2, since the 1/2 cancels the 2.