35 / 90

Day 35: Backpropagation by Hand and Gradient Flow

Derive every parameter gradient of the 2-2-1 network N1 with the chain rule, match each value against the scalar autograd engine, measure saturation and depth effects on gradient magnitude, and consolidate Days 31-34 with spaced retrieval.

Models, Pretraining & LoRA 480 active minutes (8 hours focused study)
01 120 min

Backpropagation by Hand: Output-Layer Error Term and Parameter Gradients

Agent-Assisted Engineering Check

Prerequisites

Day 34 forward ledger for network N1 ($o = 3.1255$, $L = 4.5179$).

Learning Objective

Apply the chain rule in staged form to the output layer of N1: define the error term $\delta = \partial L / \partial o$, then compute $\partial L / \partial w_{o1}$, $\partial L / \partial w_{o2}$, and $\partial L / \partial b_o$ as upstream-times-local products.

Instructions

Work the derivation symbolically first, then substitute numbers.

For the linear output neuron $o = w_{o1} a_1 + w_{o2} a_2 + b_o$ and loss $L = (o - y)^2$:

1. Output error term: $\delta_o = \frac{\partial L}{\partial o} = 2(o - y)$.
2. Local input gradients: $\frac{\partial o}{\partial a_1} = w_{o1}$, $\frac{\partial o}{\partial a_2} = w_{o2}$.
3. Local parameter gradients: $\frac{\partial o}{\partial w_{o1}} = a_1$, $\frac{\partial o}{\partial w_{o2}} = a_2$, $\frac{\partial o}{\partial b_o} = 1$.
4. Chain rule: every parameter gradient equals upstream $\delta_o$ times the local derivative: $\frac{\partial L}{\partial w_{o1}} = \delta_o \cdot a_1$.

Substitute the Day 34 ledger values $o = 3.1255$, $a_1 = 0.8741$, $a_2 = -0.9757$ and predict the three output-layer gradients before checking the answer.

Practice & Prediction Log

1. Derive $\delta_o$ symbolically for $L = (o - y)^2$.
2. Compute the numeric values of $\partial L / \partial w_{o1}$, $\partial L / \partial w_{o2}$, $\partial L / \partial b_o$.
3. Explain why the bias gradient always equals $\delta_o$ for a linear unit.
4. State which activation the gradient into hidden neuron $h_1$ must still pass through.

Independent Mastery Criterion

The learner reproduces all three output-layer gradients within $10^{-3}$ of the engine values ($3.7157$, $-4.1480$, $4.2511$) and explains the upstream-times-local structure without looking at the answer.

Authoritative Source Resources & Provenance

linking CC BY-NC 3.0 noncommercial

Neural Networks and Deep Learning: How the backpropagation algorithm works

Author: Michael Nielsen | Original Source Link ↗

Relevant Section / Range: The four fundamental equations of backpropagation

Verified Version & Date: Online book (Verified: 2026-10-30)

Selection Rationale:

Clear four-equation derivation of backpropagation with scalar and matrix notation.

Source Prerequisites:

Matrix multiplication and the multivariable chain rule from Days 19-24.

Safe Fallback (Offline Resilience):

All four backprop equations and derivations are self-contained within the lesson text.

Compared Candidate Sources & Selection Analysis (2)
Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

Modular computational graph view; used as secondary source on Days 35-36.

linking MIT

CS231n: Backpropagation and Intuitions

Author: Andrej Karpathy / Stanford University | Original Source Link ↗

Relevant Section / Range: Patterns in backward flow

Verified Version & Date: Course notes (Verified: 2026-10-30)

Selection Rationale:

Modular local-gradient interpretation (gate operations: add, multiply, max) and gradient flow intuition.

Source Prerequisites:

Multivariable calculus and basic Python.

Safe Fallback (Offline Resilience):

Local gate definitions and circuit flow diagrams in the lesson provide full coverage.

Compared Candidate Sources & Selection Analysis (2)

Raw circuit view; CS231n notes are more structured with explicit gate derivations.

Fast.ai Practical Deep Learning for Coders ↗ compared_alternative [GPL-3.0]

Top-down approach; CS231n bottom-up modular view matches our first-principles curriculum.

02 120 min

Hidden-Layer Backprop: Completing All Nine Parameter Gradients by Hand

Independent Mastery Check

Prerequisites

Output-layer deltas from Activity 35-01.

Learning Objective

Propagate the error term through the hidden layer: compute $\delta_{z_1} = (\delta_o \cdot w_{o1}) \cdot (1 - a_1^2)$ and $\delta_{z_2} = (\delta_o \cdot w_{o2}) \cdot (1 - a_2^2)$, then finish all six remaining parameter gradients and verify the complete set against `loss.backward()`.

Instructions

Continue the staged derivation into the hidden layer without assistance.

The chain rule for hidden neuron $h_1$:

1. Gradient into the activation: $\frac{\partial L}{\partial a_1} = \delta_o \cdot w_{o1} = 4.2511 \times 1.0 = 4.2511$.
2. Through the activation: $\delta_{z_1} = \frac{\partial L}{\partial a_1} \cdot \frac{\partial a_1}{\partial z_1} = 4.2511 \times (1 - 0.8741^2) = 4.2511 \times 0.2360$.
3. Hidden weight gradients: $\frac{\partial L}{\partial w_{11}} = \delta_{z_1} \cdot x_1$, $\frac{\partial L}{\partial w_{12}} = \delta_{z_1} \cdot x_2$, $\frac{\partial L}{\partial b_1} = \delta_{z_1}$.

Repeat for $h_2$ with $w_{o2} = -2.0$ and $1 - a_2^2 = 0.0479$.

Then verify all nine values:

python3 - <<'EOF'
import sys; sys.path.insert(0, '.')
from projects.autograd.engine import Value
x1, x2 = Value(2.0), Value(-1.0)
W = [Value(v) for v in (0.5,-0.25,0.1, -0.5,1.0,-0.2, 1.0,-2.0,0.3)]
a1 = (x1*W[0] + x2*W[1] + W[2]).tanh()
a2 = (x1*W[3] + x2*W[4] + W[5]).tanh()
loss = (a1*W[6] + a2*W[7] + W[8] - 1.0)**2
loss.backward()
print([round(w.grad, 4) for w in W])
EOF

Practice & Prediction Log

Produce the complete nine-gradient table for N1 ($w_{11}, w_{12}, b_1, w_{21}, w_{22}, b_2, w_{o1}, w_{o2}, b_o$), showing for each hidden weight the three factors upstream $\times$ activation-derivative $\times$ local-input, and mark every value the engine confirmed.

Independent Mastery Criterion

All nine hand-derived gradients match the engine within $10^{-3}$, the three-factor structure is shown for hidden weights, and the sign of $w_{12}$ and $w_{22}$ is correct.

Authoritative Source Resources & Provenance

linking MIT

CS231n: Backpropagation and Intuitions

Author: Andrej Karpathy / Stanford University | Original Source Link ↗

Relevant Section / Range: Patterns in backward flow

Verified Version & Date: Course notes (Verified: 2026-10-30)

Selection Rationale:

Modular local-gradient interpretation (gate operations: add, multiply, max) and gradient flow intuition.

Source Prerequisites:

Multivariable calculus and basic Python.

Safe Fallback (Offline Resilience):

Local gate definitions and circuit flow diagrams in the lesson provide full coverage.

Compared Candidate Sources & Selection Analysis (2)

Raw circuit view; CS231n notes are more structured with explicit gate derivations.

Fast.ai Practical Deep Learning for Coders ↗ compared_alternative [GPL-3.0]

Top-down approach; CS231n bottom-up modular view matches our first-principles curriculum.

linking MIT

micrograd: A tiny scalar-valued autograd engine

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

Verified Version & Date: Git commit master (Verified: 2026-10-30)

Selection Rationale:

Minimal, readable implementation of scalar autograd and MLP layers directly matched to our project.

Source Prerequisites:

Python object-oriented programming and operator overloading from Day 31.

Safe Fallback (Offline Resilience):

The local projects/autograd engine is runnable offline and matches micrograd semantics.

Compared Candidate Sources & Selection Analysis (2)
PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

Production scale, but too large and complex for tracing by hand in introductory lessons.

Autograd (HIPS) ↗ compared_alternative [MIT]

Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

03 120 min

Gradient Flow, Saturation, Vanishing Intuition, and Spaced Retrieval

Agent-Assisted Engineering Check

Prerequisites

Completed nine-gradient derivation from Activity 35-02.

Learning Objective

Measure how activation saturation and network depth shrink gradients: quantify $\tanh'(z)$ at large $|z|$, bound the gradient factor of a 10-layer $\tanh$ chain with weights $0.5$, and connect both observations to the vanishing-gradient phenomenon.

Instructions

Run two measurements, then do closed-book retrieval of Days 31-34.

Measurement A — saturation:

python3 - <<'EOF'
import sys; sys.path.insert(0, '.')
from projects.autograd.engine import Value
for z0 in (4.0, 8.0):
z = Value(z0); a = z.tanh(); a.backward()
print(z0, round(a.data, 6), z.grad)
EOF

Each extra saturated layer multiplies the flowing gradient by $\tanh'(z) = 1 - \tanh^2(z)$.

Measurement B — depth: a chain of ten $\tanh$ neurons with weight $w = 0.5$ contributes a per-layer factor $w \cdot \tanh'(z_i) \le 0.5$, so the gradient reaching the first layer is bounded by $0.5^{10}$.

Retrieval (closed book, then check the earlier days): state why += is required in _backward, what a topological sort guarantees, the shared-node gradient $\partial f/\partial a$ for $f = (a+b)a$ at $(3,4)$, and N1's forward values.

Practice & Prediction Log

1. Record $\tanh'(4)$ and $\tanh'(8)$ from Measurement A and explain the implication for a neuron whose pre-activation drifts to $|z| = 8$.
2. Compute the numeric bound $0.5^{10}$ and interpret it as the gradient reaching the earliest layer of a ten-layer chain.
3. Retrieval: without notes, write the four facts listed in the instructions; mark any you had to look up for spaced-review planning.

Independent Mastery Criterion

The learner reports both measured factors with correct orders of magnitude, states the $0.5^{10}$ bound, and completes retrieval of the four earlier-day facts with any gaps honestly marked for review.

Authoritative Source Resources & Provenance

linking 3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions No video, audio, transcript or frame redistribution permitted

What is backpropagation really doing? (Chapter 3, Deep Learning)

Author: Grant Sanderson (3Blue1Brown) | Original Source Link ↗

Relevant Section / Range: The Intuition for Backpropagation

Verified Version & Date: Online video and article (Verified: 2026-10-30)

Selection Rationale:

Visual and geometric intuition for how nudging weights impacts the final cost function.

Source Prerequisites:

Basic derivative intuition from Days 16-18.

Safe Fallback (Offline Resilience):

The lesson derives the network gradient equations step by step without requiring video playback.

Compared Candidate Sources & Selection Analysis (2)
StatQuest Neural Networks Part 3 ↗ compared_alternative [Proprietary]

Step-by-step arithmetic without calculus notation; 3Blue1Brown provides the calculus-visual link needed.

Khan Academy Multivariable Chain Rule ↗ compared_alternative [CC BY-NC-SA 3.0]

Pure math focus; 3Blue1Brown contextualizes the chain rule specifically for neural network graphs.

linking CC BY-NC 3.0 noncommercial

Neural Networks and Deep Learning: How the backpropagation algorithm works

Author: Michael Nielsen | Original Source Link ↗

Relevant Section / Range: The vanishing gradient problem

Verified Version & Date: Online book (Verified: 2026-10-30)

Selection Rationale:

Clear four-equation derivation of backpropagation with scalar and matrix notation.

Source Prerequisites:

Matrix multiplication and the multivariable chain rule from Days 19-24.

Safe Fallback (Offline Resilience):

All four backprop equations and derivations are self-contained within the lesson text.

Compared Candidate Sources & Selection Analysis (2)
Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

Modular computational graph view; used as secondary source on Days 35-36.

04 120 min

Consolidation Review: Independent Backprop Derivation and Block Retrieval

Independent Mastery Check

Prerequisites

Activities 35-01 through 35-03.

Learning Objective

Consolidate Days 31-35: re-derive the complete N1 gradient set on a fresh example input without notes, connect the scalar derivation to the vector-Jacobian product view used by production frameworks, and pass the objective quiz unassisted.

Instructions

Independent review protocol: (a) closed-book, redo the full nine-gradient derivation for N1 on input $x = (1.0, 1.0)$ (forward values from Day 34 act-04: $a_1 = 0.3364$, $a_2 = 0.2913$, $o = 0.0538$); (b) write the VJP formulation $\mathbf{v}^T J$ and name its scalar counterpart; (c) take the objective quiz in Independent mode; (d) plan one recovery link per missed idea.

Practice & Prediction Log

1. Recompute all nine N1 gradients for input $(1.0, 1.0)$: start from $\delta_o = 2(o - 1.0) = 2(0.0538 - 1.0) = -1.8925$.
2. State the four fundamental backpropagation equations in delta form ($\delta^L$, $\delta^l$, $\partial L/\partial b$, $\partial L/\partial w$) in your own notation.
3. Complete the 3-question objective quiz at 100% in Independent mode.

Objective Knowledge Check: Backprop Derivation & Gradient Flow

Answer the following questions to verify your understanding. Select your declared assistance condition before submitting.

Declared Assistance Condition

Note: AI-free work is an honest declared condition, not claimed surveillance.

1.

In the hidden-layer recurrence $\delta^l = (W^{l+1})^T \delta^{l+1} \odot \sigma'(z^l)$, what does the operator $\odot$ denote?

2.

Measured on the engine, $\tanh'(8) = 1 - \tanh^2(8)$ is approximately:

3.

Why must a gradient reaching a shared node accumulate contributions from every downstream path?

Attempt History & Evidence

    Independent Mastery Criterion

    Independent re-derivation on the new input matches engine gradients within $10^{-3}$ (spot values: $b_o = -1.8925$, $b_1 = -1.6784$, $b_2 = 3.4638$), the four delta equations are stated correctly, and the quiz is passed at 100% independently.

    Authoritative Source Resources & Provenance

    linking CC BY-NC 3.0 noncommercial

    Neural Networks and Deep Learning: How the backpropagation algorithm works

    Author: Michael Nielsen | Original Source Link ↗

    Relevant Section / Range: Proof of the four fundamental equations

    Verified Version & Date: Online book (Verified: 2026-10-30)

    Selection Rationale:

    Clear four-equation derivation of backpropagation with scalar and matrix notation.

    Source Prerequisites:

    Matrix multiplication and the multivariable chain rule from Days 19-24.

    Safe Fallback (Offline Resilience):

    All four backprop equations and derivations are self-contained within the lesson text.

    Compared Candidate Sources & Selection Analysis (2)
    Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

    Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

    Modular computational graph view; used as secondary source on Days 35-36.

    linking Modified BSD

    PyTorch Documentation: Autograd Mechanics

    Author: PyTorch Contributors | Original Source Link ↗

    Relevant Section / Range: How autograd encodes the history

    Verified Version & Date: PyTorch 2.5 Documentation (Verified: 2026-10-30)

    Selection Rationale:

    Official reference for computational graph construction, backward pass execution, and in-place mutation pitfalls.

    Source Prerequisites:

    Basic understanding of computational graphs and tensors.

    Safe Fallback (Offline Resilience):

    The training loop mechanics and zero_grad requirements are illustrated with runnable Python code.

    Compared Candidate Sources & Selection Analysis (2)
    TensorFlow tf.GradientTape Documentation ↗ compared_alternative [Apache 2.0]

    Alternative tape-based execution; PyTorch dynamic graph semantics are standard in modern LLM research.

    JAX autodiff cookbook ↗ compared_alternative [Apache 2.0]

    Functional transformations (grad, vjp); PyTorch object-based graph is closer to the beginner micrograd model.