Shelves / AI and ML Foundations
AI·00
Start Here: Your First Model
For programmers who have never trained a model: what it means for a program to learn from data, one small model trained from scratch in twenty lines of NumPy (predicting lunch orders from the temperature), watching gradient descent find the rule, testing on unseen days, overfitting made visible, the path from this to large language models, the vocabulary of the volume, and how to study it.
Start here: how to use this volume
Two reading paths
This volume builds AI and machine learning from the ground up: the field's map, Python for AI, the mathematics (linear algebra, calculus, probability), classical machine learning, evaluation, data, deep learning, PyTorch, neural architectures, Transformers, tokenization, large language models, decoding, and the foundation-model lifecycle. Every chapter has the same shape: a plain-language Start here section with small, verified examples; a How deep to go ladder; the full-depth sections; a Lab of real measurements; and a Revision sheet.
- New to AI? Read this chapter, then the chapters in order; the maths chapters assume only school algebra. Everything runs on a laptop CPU with NumPy, scikit-learn and PyTorch.
- Experienced? Skip this chapter. In each chapter read the revision sheet first, take its quiz, then use How deep to go to choose the sections and lab experiments to study.
A note on the labs: the lab machine can't download pretrained models, so the language-model chapters train tiny models from scratch on this textbook's own text. They're small enough to train on a CPU in minutes and show the same mechanisms (tokenization, attention, sampling, scaling) that large models use; they are not substitutes for large models' abilities, and the chapters say where that matters.
Mastery levels
Every chapter uses the same six levels:
| Level | Name | You can… | Example for gradient descent |
|---|---|---|---|
| L1 | Recognition | define it and spot it | "the loss is going down, so it's learning" |
| L2 | Working | use it correctly | train a model with a sensible learning rate |
| L3 | Internals | explain why it works | derive the gradient of mean squared error |
| L4 | Debugging | find and fix problems | diagnose a diverging or stalled training run |
| L5 | Design | choose for a whole system | choose optimiser, schedule and batch size for a project |
| L6 | Production | run it at scale | run training infrastructure for many models |
What it means for a program to learn
Ordinary programs follow rules a person wrote. A machine learning model instead has adjustable numbers (parameters), and a training procedure that adjusts them until the model's outputs match examples. You supply data and a way to measure mistakes; the computer finds the rule.
Twenty lines that learn
The tiffin centre suspects hot days mean fewer lunch orders. Sixty days of (temperature, orders) were recorded; 45 are used for training and 15 kept aside. The model is a straight line, orders = w × temperature + b, starting with w = b = 0. Gradient descent repeatedly nudges w and b in the direction that reduces the mean squared error (MSE) on the training days:
step 0: w= 0.00 b= 0.00 training MSE 109,632.8
step 10: w= -32.13 b= 293.42 training MSE 1,546.1
step 50: w= -36.00 b= 328.71 training MSE 285.5
step 200: w= -36.00 b= 328.71 training MSE 285.5 (converged)
learned: orders ≈ 526 − 6.15 × temperature (the data was generated from 520 − 6.00 × temperature)
(The w and b shown are for temperature rescaled to mean 0 and spread 1, which makes training faster; the last line converts back.) Nobody told the program "about 6 fewer orders per degree"; it found that from the data, close to the true rule hidden in the data's noise.
The real test: days it hasn't seen
test MSE on the 15 held-out days: 463.9 (the data's own noise has variance 18² = 324)
A model is only useful if it works on new data. The test error is near the irreducible noise, so the line captured most of what can be predicted. Now make the model more flexible, fitting polynomials of higher degree:
degree 1: train MSE 285.5 test MSE 463.9
degree 3: train MSE 264.5 test MSE 479.1
degree 9: train MSE 210.1 test MSE 434.5
degree 15: train MSE 182.3 test MSE 2,384.0
Training error kept falling as the model got more flexible; test error exploded at degree 15. The model had started memorising the noise in 45 particular days instead of learning the pattern: overfitting. (With only 15 test days, small differences like degree 9's are noise; the degree-15 blow-up is not.) Measuring on held-out data is the habit everything else in this volume rests on.
From here to large language models
A large language model is the same recipe at enormous scale: parameters (billions instead of two), a loss measuring mistakes (predicting the next token instead of an order count), gradient descent (with refinements), and held-out evaluation. The chapters climb from this line to that.
How to study AI and ML
A routine that works
- Run every example and change one thing at a time (learning rate, data size, model size) to see what moves.
- Always hold data out and look at both training and held-out numbers.
- Derive the small things by hand once (a gradient, a softmax), then trust the library.
- Plot: losses over time, predictions against truth, errors by group.
- Read the data before modelling it; most failures are data failures.
Warm-up exercises
- Copy the twenty-line model and set the learning rate to 1.1. What happens to the loss, and why?
- Add a second input (day of the week) and see whether test error improves.
- Shrink the training set to 10 days and rerun the polynomial comparison.
- Explain to a non-programmer why "it fits the training data perfectly" is not good news.
Glossary
- Model / parameters: a function with adjustable numbers / those numbers.
- Loss: a number measuring how wrong the model is (e.g. mean squared error).
- Gradient descent: repeatedly stepping parameters against the gradient of the loss.
- Learning rate: step size of gradient descent.
- Training / validation / test sets: data to fit, to tune choices, to estimate final performance.
- Overfitting / underfitting: memorising noise / too simple to capture the pattern.
- Generalisation: performing well on unseen data.