3.1.9 · D3Neural Network Fundamentals

Worked examples — Backpropagation algorithm derivation

2,040 words9 min readBack to topic

We only use the four boxed equations. As a quick reminder (each is derived in the parent):

Here means elementwise multiply (multiply matching entries, no summing). is the pre-activation (before the nonlinearity), is the activation (after it). is the blame — how sensitive the loss is to a neuron's pre-activation. See Chain rule (multivariable), Activation functions and their derivatives, and Loss functions (MSE, cross-entropy) for the pieces we lean on.


The scenario matrix

Every case backprop can throw at you falls into one of these cells. The right column names the example that covers it.

# Case class What's special Covered by
A Positive error, sigmoid prediction too low, Ex 1
B Negative error, sigmoid prediction too high, Ex 2
C Zero error (degenerate) perfect prediction, gradient vanishes Ex 3
D Saturated sigmoid (limiting) huge, , blame dies Ex 4
E ReLU, live neuron gate , blame passes untouched Ex 5
F ReLU, dead neuron () gate , blame blocked Ex 5
G Multi-output + cross-entropy vector , softmax simplification Ex 6
H Real-world word problem translating a story into BP1–BP4 Ex 7
I Exam twist: vs pick the shape that type-checks Ex 8

The sign of is set entirely by BP1's first factor , because for sigmoid the gate is always positive (between and ). Look at the sign map:


Example 1 — Cell A: positive error, sigmoid

Forecast: the prediction will land below , so we are under-shooting. Guess: is negative or positive?

  1. Forward. , so . Why this step? We cannot assign blame before we know what the network actually output.
  2. BP1 first factor. . Why this step? ; this is the raw "how wrong, and which way."
  3. BP1 gate. . Why this step? BP1 requires at ; for sigmoid this equals .
  4. BP1. . Why this step? This is BP1 for a single neuron.
  5. BP3. . Why this step? Gradient = blame input activity.

Verify: ✓ matches "we under-shot, so nudging up lowers loss, meaning ." Gradient descent does = increase , which raises , raises toward . Sane.


Example 2 — Cell B: negative error (over-shoot), sigmoid

Forecast: now the target is but we output — we over-shoot. Sign of ?

  1. Reuse forward: (unchanged, weights identical). Why? The forward pass does not depend on .
  2. BP1 first factor. . Why? Positive because we are above target.
  3. Gate. (same as Ex 1).
  4. BP1. .

Verify: ✓. Descent: decreases , lowers , lowers toward . Correct direction. Compare with Ex 1: same , opposite target, opposite sign — the gate never flips sign, only the error does. This is exactly figure s01's message.


Example 3 — Cell C: zero error (degenerate, gradient vanishes)

Forecast: with nothing to fix, what should the gradient be?

  1. BP1 first factor. . Why? Perfect prediction ⇒ zero raw error.
  2. BP1. . Why? A zero factor kills the product regardless of the gate.
  3. BP3. .

Verify: At the loss minimum the gradient must be ✓. Gradient descent takes a zero step — the weight stays put, as it should at an optimum. Note the degeneracy: the gate is still healthy; the vanishing comes purely from the error, not from saturation. Contrast with Ex 4, where the opposite happens.


Example 4 — Cell D: saturated sigmoid (limiting behaviour)

Forecast: the error is huge — surely the gradient is huge and it fixes fast? Guess yes/no.

  1. Forward. . Why? Large positive pushes sigmoid near .
  2. BP1 first factor. (near-maximal error).
  3. Gate. . Why? Deep in saturation the sigmoid is flat, so its slope is nearly .
  4. BP1. .

Verify: Despite a near-maximal error, is tiny — the flat gate strangled it. This is the mechanism of Vanishing and exploding gradients: a saturated sigmoid learns almost nothing. Sanity check the limit: as , , , so for any bounded error ✓. Compare Ex 3 (zero from zero error) vs here (near-zero from a dead gate) — two very different roads to a vanishing gradient. Cross-entropy loss dodges this; see Ex 6.


Example 5 — Cells E & F: ReLU live vs dead neuron

Recall ReLU: , with derivative if and if (a switch, not a squash). This is why BP2 needs , not — the sign of is the whole story.

Forecast: which of the two blame values survives the ReLU gate?

  1. Gate vector. since , . Why this step? ReLU's derivative is the indicator "is positive?".
  2. BP2 elementwise gate. . Why? multiplies entrywise; the dead neuron's gate is .

Verify: Neuron 1 (live) passes its blame untouched (gate , cell E). Neuron 2 (dead) contributes , so by BP3 all its incoming weight gradients are (cell F) — it learns nothing this step. Units/shape check: length , gate length , length ✓. This is the "dying ReLU" phenomenon: a neuron stuck at receives no gradient and may never recover.


Example 6 — Cell G: multi-output softmax + cross-entropy

Forecast: cross-entropy + softmax is famous for a clean . What do you think it collapses to?

  1. Softmax forward. . Numerators: , , ; sum . So . Why this step? Softmax turns logits into a probability vector (entries positive, sum to 1).
  2. The identity. For softmax + cross-entropy, BP1 collapses to (the messy Jacobian-times-gradient cancels exactly). Why this step? This is the celebrated simplification — no separate gate survives.
  3. Plug in. .

Verify: Entries of sum to : ✓ (softmax outputs sum to 1, one-hot sums to 1, difference sums to 0). Sign check: true class has (push its logit up), wrong classes have (push down). Compare with Ex 4: here there is no vanishing gate — cross-entropy is specifically designed so a confident-but-wrong output still gets a large . That's why it beats MSE for classification.


Example 7 — Cell H: real-world word problem

Forecast: the link-count feature is bigger (3 vs 0.5) — will its gradient dominate?

  1. Forward. ; . Why? Standard weighted sum plus bias, then squash to a "spam probability."
  2. BP1. ; gate ; so . Why? Under-confident on a true-spam email ⇒ negative blame ⇒ we want to raise .
  3. BP3. . Why? Gradient = blame feature value; the bigger feature scales the blame more.

Verify: Both gradients negative ⇒ Gradient descent increases both weights ⇒ raises ⇒ classifies future similar emails as more-spam. Correct. The link-count weight's gradient magnitude () is the caps-ratio's () — exactly the feature ratio ✓. So yes, the larger-valued feature dominates the update, as forecast.


Example 8 — Cell I: exam twist, vs by type-checking

Forecast: guess before reading — which shape produces a length-4 result?

  1. List the shapes. : . : . : . Why this step? Matrix multiply needs the inner dimensions to match and yields .
  2. Test : — inner dims , illegal. Rejected.
  3. Test : — inner dims ✓, result . This is the length-4 vector BP2 wants. Why? Only the transpose type-checks and has the right output length.

Verify: BP2's before-gate factor is , giving length size of layer ✓. This is the adjoint direction: forward maps layer- () → layer- (); backward the transpose maps blame , exactly reversing the arrows. Numeric sanity: with and , — a length-4 vector, as required.


Recall Which cell had a vanishing gradient from the

error, and which from the gate? Zero error ::: Ex 3 (Cell C) — perfect prediction, . Dead gate ::: Ex 4 (Cell D, saturated sigmoid) and Ex 5 F (dead ReLU) — .

Recall What makes softmax+cross-entropy immune to the saturation problem of Ex 4?

::: BP1 collapses to with no surviving gate, so a confident-wrong output still gets a large error signal.