Section 4.2: The Training Loop, Built by Hand
Training is five steps repeated: predict, measure the loss, run backward(), move every parameter a small step against its gradient inside torch.no_grad(), and clear the gradients before the next pass. Section 4.1 wrote each step once. This section assembles them into the cycle, gradient descent (fitting by repeating a step against the gradient), on one neuron (one unit that computes a weighted sum of its inputs plus a bias) pricing a listing from its standardized area, and watches the cycle reach in 70 steps the answer least squares computes in one solve, by the search without a grid that Lesson 3.1.3 promised, because a scan is hopeless for thousands of parameters. Then every hand-written piece is replaced by PyTorch's own class, nn.Linear for the neuron, nn.MSELoss for the loss and optim.SGD for the step and the clearing, with the numbers proved unchanged, and the loop is run the way Module 3 taught, rows held back, a validation loss read beside the training loss every epoch, and the test rows scored once.
The failures start where the convenience does. A raw, unstandardized input sends the loss to inf in five epochs at one learning rate and leaves the intercept crawling after a hundred thousand at another. A learning rate of 1.0 stands still forever and 1.1 explodes. The loop with its fifth step left out prints a loss every epoch, exits without a word, and never settles, because each step moves by the sum of every gradient so far. A loss handed an output of shape (n, 1) and a target of shape (n,) warns, broadcasts them into a table and returns a number several times too large. And two switches that change no number on this model look like decoration, and a validation loss that happens to sit below the training loss looks like a bug, when neither reading is right. None of them stops the program.
Lessons
- One Neuron - Fit by Hand - the neuron and the five steps, standardizing the input first, the loop epoch by epoch, the learning rate from a crawl to an explosion, the loop without the zeroing, and the fit read back in square meters.
nn.Moduleandoptim- the Honest Refactor -nn.Linearas the neuron,nn.MSELossand the shape it wants,optim.SGDas the step and the zeroing, the same numbers proved from the same start, and a module class of your own.- Train and Validation Discipline, and the Loss Curve - fit, validation and test rows,
no_gradandevalas two switches, the baseline and the tiny-batch check, the loss curve drawn and checked, the two curves read together, and the test rows scored once.
By the end you will be able to:
- Write the five-step loop on raw tensors, standardize its input with the fit rows' statistics, choose a learning rate by what the loss prints, and read the fitted weight back in the input's own units.
- Replace each hand-written piece with
nn.Linear,nn.MSELossandoptim.SGD, prove the numbers unchanged from the same seeded start, and write a model as a subclass ofnn.Module. - Run a loop with fit, validation and test rows, pass the mean baseline and the tiny-batch check, draw and verify the two loss curves, and name what a rising validation curve means.