1. Forward pass
Use batch-first notation:
X [B,D]
W [D,H]
b [H]
Z [B,H]
Suppose the next part of the network sends us
We need gradients for , , and .
2. Weight gradient
Each weight connects input feature to output feature . Its contribution accumulates over all examples:
Shape Check:
[D,B] @ [B,H] → [D,H]
Exactly the shape of W.
3. Bias gradient
The same bias is added to every example, so its gradient sums across the batch:
Shape:
[B,H] sum over B → [H]
4. Input gradient
Earlier layers need to know how the loss changes with :
[B,H] @ [H,D] → [B,D]
Again, exactly the shape of X.
5. Small scalar check
For one example with and upstream gradient :
The matrix rules are just all scalar paths organized efficiently.
6. Activation joins the chain
If and upstream gradient is ,
Then feed dZ into the three equations above.
What to Remember
dW = Xᵀ dZ
db = sum_batch(dZ)
dX = dZ Wᵀ
Do not memorize blindly: derive them from dependency paths and verify every shape.
Next → Matrix Backpropagation: how do these local rules compose across an entire multilayer network?