AI from scratch · ML strategy · Stage 3: classic ML and judging models
ML strategy: what to fix next
A model works, but not well enough. You could collect more data, train longer, try a bigger network, add regularization, clean labels, or try a different idea entirely. Each can take weeks, and most won't help this particular model. The skill this lesson teaches is reading a few numbers to know which one will: the difference between teams that improve every week and teams that spend months on the wrong thing. The examples use scikit-learn's handwritten digits (1,797 tiny 8×8 scans) because their mistakes are easy to look at.
1. One number to beat, plus limits
You can only compare ideas quickly with one number that says better or worse: accuracy, F1, mean error. Everything else you care about becomes a limit the model must meet, not a second number to weigh: "answers within 100 ms", "under 50 MB", "at most one false alarm per camera per hour". Among the models that meet every limit, pick the best on the one number. Two numbers with no rule for trading them ("precision 92%, recall 85%" versus "precision 88%, recall 90%") leave every decision to a meeting.
A phone keyboard's next-word model: you care about how often its top suggestion is right, how fast it answers, and app size. How do you set it up?
- Average the three into one score
- Maximize top-suggestion accuracy, subject to answering within, say, 30 ms and fitting in 20 MB
- Optimize all three separately and pick by judgment
One number decides better or worse; everything else you care about is a limit to meet.
2. Read the gaps
Four error rates tell you where the trouble is. Human-level error stands in for the best possible (some inputs are truly ambiguous, so nothing gets 0%). Training error is on the data the model learned from. Train-dev error is on held-out examples from the same source as training. Dev error is on examples like the ones the model will meet in use. The gaps between them each have their own fixes:
- Avoidable bias = training − human: the model can't even fit what it was shown. Bigger model, train longer, better features, less regularization.
- Variance = train-dev − training: it fits its examples but doesn't generalize. More data, regularization, augmentation, a simpler model (chapter 11).
- Data mismatch = dev − train-dev: real use looks different from training. Get or synthesize training data that looks like real use (section 5).
Fix the biggest gap first. Drag the sliders:
The same numbers on the digits: regularization strength C moves a logistic regression from too simple (high bias) to fitting its training set almost perfectly (variance).
With C = 1: training error 0.2%, test error 2.8%. Suppose careful people misread 1% of these tiny scans. What's the biggest gap, and what would you try?
- Variance (2.6 points): more training data, augmentation, or a bit more regularization
- Avoidable bias: a bigger model
- Data mismatch: collect different scans
Each gap between the four error rates points at a different fix: work on the biggest one first.
3. Error analysis: look at the mistakes
Before building anything, look at about a hundred mistakes on the dev set and sort them into categories. It takes an hour, and it tells you the most each idea could ever gain: if only 5% of mistakes are blurry images, a perfect deblurring step can cut the error by at most 5% of itself. This ceiling kills many plans before they cost a month. Error rates per group (per digit here; per neighborhood, age group or device elsewhere) are the same idea, and they're also how you check that a model isn't much worse for some people than for others.
Look at the pictures, not only the counts: some of these 8×8 scans would fool a person too. A mistake that's really a wrong label is worth counting as its own category; fixing labels only pays off when that category is big.
A category of mistakes caps what fixing it can win, so look at a hundred mistakes before building anything.
4. Will more data help? Learning curves
Data is often the most expensive fix, so check first: train on growing slices of the training set and plot training and validation error. If validation error is still falling at full size and there's a gap between the curves, more data will likely help. If both curves have flattened close together, more of the same data won't: the model needs to change.
Note the shuffled folds. Without shuffle=True, scikit-learn's folds follow the file's order, and this file is grouped by the people who wrote the digits: each fold would test on new handwriting, and the validation error at full size would read about 8% instead of 3%. Whether that's "wrong" depends on the question: if the model must read handwriting it has never seen, splitting by writer is the honest test (features and leakage, group splits).
On the digits, validation error falls from 4.7% to 3.1% between 1,077 and 1,437 examples, while training error stays near 0%. Would more scans help?
- Probably: validation error is still falling at full size and there's a gap to training
- No: the curves have flattened
- Can't tell without more experiments
A learning curve tells whether data will help: still falling with a gap means yes, flat and close together means change the model.
Why more data can't fix bias
- A fixed kind of model (a line, a depth-3 tree, a given network) has a best error it could reach on this problem even with endless data: its floor.
- With few examples, its training error sits below the floor (it fits those particular points) and its validation error above it (they don't generalize).
- As data grows, training error rises and validation error falls, both toward the floor: the two learning curves squeeze together.
- So data shrinks the gap between the curves, which is the variance. It can't lower the floor, and the floor minus human level is the avoidable bias.
- Chapter 11's straight line on the sine-shaped data: with 20 points its bias² is 0.050 and its variance 0.013; with 2,000 points, bias² 0.050 and variance 0.0001. A hundred times the data removed the variance and left the bias untouched.
- Lowering the floor takes a different model: more flexible, better features, or less regularization.
5. When real use looks different
Training data is often whatever is easy to get, and real use is something else: web photos versus phone photos, daytime cameras versus night, last year's customers versus this year's. Then a dev set from real use is the only honest one, and you need one more set, train-dev, carved from the training source, to tell variance from mismatch. Below, the model learns from clean scans and is then used on "phone" scans with sensor noise.
Training 0.0%, train-dev 2.2%, dev 52%. How many points of the dev error are variance? (Round to a whole number.)
When real use looks different, only a dev set from real use is honest, and a train-dev set tells variance from mismatch.
6. A case study: the bike-lane camera
You lead a small team building a model that watches city street cameras and flags cars blocking bike lanes, so the city can send an officer. Five decisions, each with consequences.
1. The metric. The city says: catch as many blocked lanes as possible, but officers will ignore the system if it cries wolf more than once per camera per hour. Which setup?
- Accuracy on all camera frames
- Recall, subject to at most one false alarm per camera per hour
- Precision
2. First results. Two inspectors looking at the same frames disagree on 2%. Your model: training error 9%, dev error 10%. What next?
- A bigger model or better features: training error is 7 points above human level
- Label 50,000 more frames
- Add regularization
3. A month later. A bigger network gets training error to 2.5% but dev error 9%. Now what?
- An even bigger network
- Re-measure human level
- More data or augmentation (flips, brightness, crops), or regularization: the gap is variance now
4. Night falls. Train-dev error is 4%, daytime dev 4.5%, but night-time cameras: 25%. Your training frames are 95% daytime. What do you do?
- Collect more daytime frames
- Get or make night-like training frames (label some night frames; darken and add noise to day frames), and add a night dev set to measure it
- Report the daytime number
5. Error analysis. Of 100 dev mistakes: 40 are delivery trucks with hazard lights on, 25 are rain at night, 10 have wrong labels, 25 are miscellaneous. Dev error is 8%. Which is worth a week first?
- The delivery trucks: fixing them all could bring dev error from 8% to about 4.8%
- Re-labeling the dataset
- The miscellaneous mistakes
At every stage of a project, read the gaps again: the right fix changes as the project moves.
One last trap, from the end of such projects: after tuning on the dev set fifty times, its score is a little optimistic, because you chose what worked on it. That's what the untouched test set is for. If dev says 4% and test says 6%, you've overfit the dev set: get a fresh dev set rather than tuning further.
Where it breaks: when the gaps mislead
- Models that beat people. Human-level stands in for the best possible error, but nobody can judge a table of payments or sensor readings well, so models pass human level easily there. Then avoidable bias is unknown: use the best error anyone has reached (a strong ensemble, a published result) as the reference, and lean more on error analysis.
- Categories that overlap. One mistake can be night, rain and a truck at once. Then the ceilings overlap too: fixing trucks (40%) and night rain (25%) can't remove 65% of the mistakes if some are both. Tag each mistake with every category it fits, and treat each ceiling as a separate maximum, not a sum.
- A number that gets gamed. Once a team pushes one number, the model finds the cheapest way to raise it: a recommender tuned for clicks learns clickbait, a call-center model tuned for short calls learns to hang up. Keep limits for what the number leaves out, and keep reading real examples.
7. What to carry forward
| The numbers say | Try | Don't bother with |
|---|---|---|
| Training error far above human level (avoidable bias) | Bigger model, train longer, better features, less regularization | More data |
| Train-dev far above training (variance) | More data, augmentation, regularization, simpler model | A bigger model alone |
| Dev far above train-dev (data mismatch) | Training data that looks like real use (collect, or make it) | More of the same training data |
| Validation error still falling at full size | More data will likely help | |
| A category of mistakes is a small share | Spending weeks on it: its ceiling is low |
- One number to beat; everything else is a limit.
- Dev and test sets come from the conditions the model will meet; train-dev comes from the training source.
- Look at a hundred mistakes before building anything, and compute each idea's ceiling.
All lessons: AI from scratch. Andrew Ng's free "Machine Learning Yearning" covers these ideas at book length.
Last updated