Backpropagation without magic
Backprop becomes ordinary once every edge in the forward graph has a local derivative you can inspect.
A nonlinear sensor classifier defeats every classical baseline.
This chapter moves from name the forward intermediates to check one coordinate numerically. We will make the idea concrete in a small code example before placing it back inside a production system with a declared latency, cost, and operator-capacity budget.
Compare an analytic parameter gradient with a centered finite difference using a small epsilon and float64 values.
Verify the behavior illustrated in backprop_scalar.py, then measure decision utility, slice reliability, and system latency.
An excessive learning rate causes NaNs; dead ReLUs and a BatchNorm train/eval mismatch hide the cause. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
Backpropagation is bookkeeping for the chain rule. It walks the forward computation in reverse, multiplying each incoming sensitivity by the local derivative and accumulating contributions when a value feeds multiple paths.
During the forward pass, store the intermediate values needed by local derivatives. Starting from dL/dprediction, each operation computes gradients for its inputs and parameters. Matrix multiplication follows the same logic at larger scale; automatic differentiation merely records the graph and performs this reverse traversal efficiently.
A finite-difference check perturbs one parameter and compares the resulting loss slope with the analytic gradient. It is slow and numerically approximate, but on a tiny deterministic network it catches transposes, missing reductions, and wrong activation derivatives before they disappear inside a framework.
- cross-entropy
- chain rule
- initialization
- AdamW
Follow the reasoning before touching the code.
- 01
Name the forward intermediates
Write the scalar or tensor produced by every operation and preserve masks or inputs needed to differentiate it.
- 02
Propagate sensitivities backward
Begin with the loss derivative, apply one local chain-rule factor at a time, and sum gradients wherever a value has multiple downstream uses.
- 03
Check one coordinate numerically
Compare an analytic parameter gradient with a centered finite difference using a small epsilon and float64 values.
x, target = 2.0, 3.0
w1, w2 = 0.5, 1.5
z = w1 * x
hidden = max(0.0, z)
prediction = w2 * hidden
loss = 0.5 * (prediction - target) ** 2
d_prediction = prediction - target
d_w2 = d_prediction * hidden
d_hidden = d_prediction * w2
d_w1 = d_hidden * (z > 0) * x
print('loss', loss, 'gradients', d_w1, d_w2)Differentiate a two-weight ReLU network by hand.
What to notice
- The gradient for w1 contains every derivative along the path loss → prediction → hidden → z → w1.
- Because z is positive, the ReLU derivative passes the gradient through; a negative z would gate d_w1 to zero.
YOUR TURN Pause and predict before revealing the answer.
Change w1 to -0.5. What are d_w1 and d_w2, and why does learning stall on this example?
Need a hint?
Follow the ReLU output and its local derivative when z is negative.
Check your reasoning
Both gradients become zero: hidden is zero, so d_w2 is zero, and the ReLU derivative makes d_w1 zero. This example provides no path to revive the unit.
Lessons 1, 2 in this course.
Connect the chapter to the course’s quantitative spine.
Backprop becomes ordinary once every edge in the forward graph has a local derivative you can inspect. The backpropagated error below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
Backpropagated error
| Symbol | Meaning / shape / unit |
|---|---|
δ_l | loss gradient at layer l |
W | weight matrix |
φ′ | activation derivative |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: δ_l, W, φ′.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c09(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
Optimizer stability lab
Sweep learning rate over a deterministic convex-plus-curvature loss.
Assumption: Twenty gradient steps from w=4 on L(w)=w²/2.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| step 1 | 3.5200 | 6.19520 |
| step 5 | 2.1109 | 2.22801 |
| step 10 | 1.1140 | 0.62050 |
| step 15 | 0.5879 | 0.17281 |
| step 20 | 0.3103 | 0.04813 |
Trace the complete operating path.
Carry the chapter’s central idea into the system: Backprop becomes ordinary once every edge in the forward graph has a local derivative you can inspect. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Name the forward intermediates” into a written invariant and a failing test.
- 02
Promote backprop_scalar.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Check one coordinate numerically” as the release checkpoint: Compare an analytic parameter gradient with a centered finite difference using a small epsilon and float64 values.
- 04
Serve or materialize behind explicit a declared latency, cost, and operator-capacity budget.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
An excessive learning rate causes NaNs; dead ReLUs and a BatchNorm train/eval mismatch hide the cause. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Network from first principles
Specify a NumPy MLP, finite-difference checks, PyTorch parity, calibrated inference, and debugging report.
- Forward/backward derivation
- Gradient check plan
- Optimizer/regularization ablations
- Export and batch-one parity
Read primary material with a purpose.
Return to the opening failure.
Package preprocessing, vocabularies, weights, calibration, output semantics, and batch-one parity as one artifact.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a small network built from forward pass through backpropagation and optimization.