Scalar graphs are fine for understanding, but networks work with matrices. Happily, a whole dense layer has a compact backward rule. Let with of shape , of shape . Suppose the layer above has handed us , shape . Then:
A reliable way to remember these is shape matching. must have 's shape ; the only things available are and , and the only product with the right shape is . The bias was broadcast to every row in the forward pass, so its gradient sums over rows (the "used many times, so sum" rule again).
ReLU backward
For , the gradient passes through where and is blocked elsewhere: , where is element-wise multiplication. Keep (or the mask) from the forward pass.
Softmax and cross-entropy together
At the top of a classifier, softmax and cross-entropy combine into the cleanest gradient in deep learning. With one-hot labels and the loss averaged over the batch:
So a full backward pass through a one-hidden-layer classifier is five lines: start with , push it through the output layer's rule to get , , , gate by the ReLU mask, then apply the dense rule again for , .
Always check your gradients
Hand-written backward passes are bug magnets: a missing transpose, a forgotten sum, a mask from the wrong layer. Gradient checking compares analytic gradients with central differences on a few parameters, using the relative error . With float64, a correct implementation typically gives around or smaller; around means a bug. Check on a tiny network and a handful of examples, then switch it off: it's far too slow for training.
For : , row-sum of , . Shapes tell you the formulas; gradient checks tell you they're right.