3.3.5 · D3Deep Learning Frameworks

Worked examples — Training loops from scratch

2,667 words12 min readBack to topic

Before any symbol is used, here is the plain-words dictionary.

Why and not something else? Because we chose squared loss. The derivative of with respect to is by the power rule. The quantity is the signed error — positive if we overshot, negative if we undershot. Its sign is the whole story of which direction to move, so we track it carefully in every case below.


The scenario matrix

Every training-loop situation you can meet is one of these cells. The examples afterward each fill one (or more) cell(s).

# Cell class What is special Example
A Overshoot (, error ) gradient positive → weight decreases Ex 1
B Undershoot (, error ) gradient negative → weight increases Ex 2
C Zero-gradient / already correct update does nothing Ex 3
D Degenerate input frozen, only moves Ex 4
E Dead ReLU neuron gradient blocked to zero Ex 5
F Learning rate too large (divergence) loss explodes Ex 6
G Mini-batch averaging over mixed signs opposing errors partly cancel Ex 7
H Real-world word problem (multi-epoch) loop drives loss down over time Ex 8
I Exam twist: forgotten zero_grad gradients accumulate Ex 9

Worked examples


Recall Self-check (reveal after guessing)

Overshoot () makes the weight go which way? ::: Down — positive error gives a positive gradient, so subtracting it shrinks . When the input , which parameter cannot learn? ::: The weight , because its gradient carries the factor . A ReLU neuron with pre-activation passes what fraction of the backward signal? ::: Zero — the ReLU derivative is for (a dead neuron). Why does too-large diverge? ::: Each step overshoots the parabola's minimum and lands higher on the opposite wall, so loss grows. Forgetting zero_grad does what to gradients? ::: It accumulates (adds) them across batches, over-stepping the update.


Connections: Training loops from scratch · 3.1.02-Backpropagation-algorithm · 3.3.01-PyTorch-fundamentals · 3.3.06-Custom-loss-functions · 3.4.03-Learning-rate-schedules · 3.4.01-Batch-normalization · 3.5.02-Data-loaders-and-batching · 4.2.01-Gradient-descent-variants · 5.1.01-Overfitting-and-underfitting