Training is a loop (you've written loops)
model.fit(X, y) looks like magic because it's one line. Inside is
a loop you can read — and in this editor, one you can run.
The loop, demystified
Run it. The model is a single parameter w in y = w * x. Each
pass:
- Predict with the current
w. - Measure error — mean squared error between predictions and labels.
- Compute the gradient — which direction (and how steeply) the
error changes as
wchanges. The gradient is just a slope. - Step
wdownhill by a small amount (lr, the learning rate). - Repeat.
Watch w walk from 0 toward 2 while the error falls. That's
gradient descent, and it is the training loop — for this
one-parameter toy, for sklearn's logistic regression, and for the
billion-parameter models in chapter 43. Bigger models change what
gets adjusted, not the shape of the loop. (Chapter 16's agent loop
was "act, observe, adjust" over tool calls; this is the same rhythm
over parameters.)
Prediction is the loop's frozen output
After training, w is fixed. Predicting is one multiply — no loop,
no labels needed. That asymmetry runs all of ML: training is
expensive and rare; prediction is cheap and constant, which is why
serving (chapter 46) and training (chapter 43) are engineered
separately.
The knob you just met: learning rate
Try it in your head (or re-run with edits): lr = 1.0 overshoots
and oscillates or diverges; lr = 0.0001 crawls. Every training
failure you'll ever debug starts with these two suspects. Chapter 43
gives the production version (optimizers, schedules); the intuition
lives here.
Where AI specifically gets this wrong
- Treating
.fit()as unexplainable. Then when training stalls or diverges, the generated "fix" is random hyperparameter shuffling. You now know the loop; debug it like one — print the error curve first. - No convergence check. A loop that runs 30 steps isn't done because it ran; it's done when the error stops improving. Look at the curve, not the step count.
One more, subtle enough to survive code review: refitting at
predict time. Generated code sometimes calls fit inside the
serving path — retraining on every request. Train once, freeze,
predict (chapter 46 hardens this).