3.3.5 · D1Deep Learning Frameworks

Foundations — Training loops from scratch

2,762 words13 min readBack to topic

This page assumes nothing. Before you can read the parent note Training loops from scratch, you must be fluent in a small pile of symbols and pictures. We build each one from zero, in an order where every symbol only uses symbols already defined.


1. The knob: a parameter

The picture. Imagine a mixing desk with hundreds of sliders. Each slider is one number the model can change. is the whole desk — the complete list of slider positions right now.

Figure — Training loops from scratch

Why the topic needs it. Training does exactly one thing: it changes . Everything else — the loss, the gradient, the loop — exists only to decide how to change next. If you don't know what is, nothing after it makes sense.

Recall

What does collect in one symbol? ::: Every weight and bias in the model — the whole "mixing desk" of tunable numbers.


2. The machine: the model function

The semicolon matters. before the semicolon is the data (changes every example). after the semicolon is the knob setting (fixed during a single guess, changed by training). Same machine, two very different roles for what goes in.

The picture. A box. Left arrow in = . Right arrow out = the guess. The dials on top = .

Recall

Why is written after a semicolon and before it? ::: is the data that varies per example; is the knob setting held fixed during one prediction and adjusted by training.


3. Input , true answer , guess

The hat means "estimated". A plain is the truth from the real world. A hat is our machine's attempt at that truth. They are rarely equal — the gap between them is the whole point of training.

Figure — Training loops from scratch

Why the topic needs it. The distance between the orange guess and the teal truth is what we shrink. No hat, no gap, no learning.


4. Subscripts and the dataset: , ,

The picture. A spreadsheet with rows. Row holds one pair. The subscript is "which row".

Recall

In , what does the little mean? ::: It is the row number — the 7th training example — not a power or multiplication.


5. Summation and the average

Read it left to right: "sum, starting at , ending at , of ...". The letter under (here ) is the counter; the number on top () is where to stop.

Why the topic needs it. The parent note measures error as an average over the dataset. Averaging (not just summing) makes the number independent of dataset size — a loss of "5" means the same thing whether you have 100 or a million examples.

Recall

Why average the loss instead of summing it? ::: A raw sum grows with dataset size; dividing by makes the loss magnitude comparable across datasets of any size.


6. The scoreboard: per-example loss and total loss

We need two loss symbols, not one — a small script for one example, and a big script for the whole dataset. Keeping them separate is what makes batches make sense later.

Why squared? The term is the miss for one example — it can be positive (guessed too high) or negative (too low). Squaring does two jobs: it makes every miss positive (so misses can't cancel out), and it punishes big misses far more than small ones (a miss of 4 costs 16; a miss of 1 costs only 1).

Figure — Training loops from scratch

Now stack the per-example losses over the whole dataset and average them. That gives the total loss, and here is the crucial link: since each , the total loss is really a function of the knobs .

The nested picture . Input goes into the machine (which contains ), out pops , which drops into the grader alongside the truth . Loss is wrapped around — written , read " after ".

Why the topic needs it. The total loss is the scoreboard. Without a single -dependent number to shrink, "make the model better" has no meaning. It turns a vague wish into a concrete target: find that makes small.

Recall

What is the difference between and ? ::: grades a single example; is the average of all the 's and is a function of the knobs .

Recall

Why square the errors instead of just adding them raw? ::: Squaring stops positive and negative misses from cancelling, and it penalises large misses much more heavily than small ones.


7. Slope, then the gradient

Everything so far tells us how wrong we are. The gradient tells us which way to turn the knobs.

For a single knob , the slope of the loss is written — read "the change in per tiny change in ". The curly ("partial") just says "we're wiggling one knob while holding the others still".

The picture. On a bowl-shaped loss surface, is the arrow pointing straight up the steepest wall. The negative of it, , points straight downhill toward the bottom of the bowl.

Figure — Training loops from scratch

Why this tool and not another? We want to reduce a number () by changing many knobs () at once. The gradient is the only object that answers "which direction, among all possible directions in this high-dimensional knob-space, drops the loss fastest?" The machinery that actually computes it is backpropagation.

Recall

The gradient points uphill on the loss surface. Which way do we step to learn? ::: The opposite direction, — downhill, toward smaller loss.


8. The step size , the iteration index , and the update rule

The minus sign is the heart of it: subtracting the uphill gradient means we move downhill. Evaluating at the current knobs says "measure the steepest-uphill direction from where we're standing right now", then step .

Why must be "just right". Too big and you leap over the valley and bounce up the far side (loss explodes). Too small and you inch along forever. Choosing and changing over time is its own art — see learning-rate schedules.

Recall

Does the counter tick once per example, once per batch, or once per epoch? ::: Once per batch — i.e. once per parameter update.

Recall

In , what does the minus sign accomplish? ::: It flips the "uphill" gradient into a "downhill" move, so the loss decreases.


9. Batch , batch size , one epoch, and the batch update

Computing the gradient over all examples every single step is slow. So we cheat, cleverly.

The picture. Shuffle the spreadsheet, then slice it into groups of rows. Process each slice, update the knobs after each. When every row has been used once, that's one epoch. See data loaders and batching for how this slicing is organised.

Instead of averaging over all rows, we average it over just the current batch. That gives the batch loss:

Notice this is the same shape as the update rule in §8 — only the whole-dataset loss has been swapped for the batch loss . That single swap is exactly what makes tick per batch.

Why batches? A single example gives a noisy, jittery estimate of the true downhill direction; the full dataset gives a perfect direction but is expensive. A mini-batch is the Goldilocks compromise — cheap enough to compute often, stable enough to point roughly downhill. This is the "S" (Stochastic) in SGD.

Recall

What is the batch loss ? ::: The average per-example loss over just the current batch — used to estimate the gradient cheaply.

Recall

What is the difference between a batch and an epoch? ::: A batch is one small handful of examples for a single update; an epoch is one complete pass through all examples, batch by batch.


How these feed the topic

parameter theta - the knobs

model f of x - the machine

input x and truth y

prediction y-hat

per-example loss ell

total loss L of theta

sum and average

batch loss over B_t

gradient nabla theta L - steepest uphill

stochastic gradient

update rule with eta and step t

TRAINING LOOP

Each box is a symbol you now own. Together they are the training loop: the machine () guesses (), the grader () scores each guess, the average ( or the batch ) becomes the scoreboard, the gradient points the fix, and the update rule turns the knobs () by a step at each tick — over and over, batch by batch.


Equipment checklist

Test yourself — say the answer aloud before revealing.

What single symbol collects every weight and bias in the model?
What does the semicolon in separate?
The data (before) from the knob settings (after).
What does the hat in signify?
An estimate — the model's prediction, as opposed to the true .
In , is the a power, a multiplier, or an index?
An index — the example's row number.
What does tell you to do?
Add the following expression for up to .
Why divide the summed loss by ?
To average it, making the loss size independent of dataset size.
What does the per-example loss output?
One number grading a single guess against a single truth.
How is the total loss built from and ?
— the average per-example loss, wrapped around the model , so it depends on .
Why square the errors in MSE?
To keep misses positive and to punish large misses more heavily.
What does point toward?
The direction of steepest increase of the loss.
Which way do we step to reduce loss?
Opposite the gradient, .
What role does play?
It sets how big each downhill step is (the learning rate).
Does count examples, batches, or epochs?
Batches — it ticks once per parameter update.
What is the batch loss and why use it?
The average over the current batch; its gradient is a cheap noisy estimate of the full-dataset gradient.

Next: with every symbol earned, return to Training loops from scratch and watch these pieces click into the five-step cycle. Related paths worth a glance: 3.3.01-PyTorch-fundamentals, 3.3.06-Custom-loss-functions, 3.4.01-Batch-normalization, and 5.1.01-Overfitting-and-underfitting.