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?

Accuracy is what users notice; speed and size only matter until they're good enough. That's the "optimizing plus satisficing" setup: one number to beat, the rest as limits.

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:

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?

Avoidable bias is 0.2 − 1 < 0 (already at human level); variance is 2.8 − 0.2 = 2.6 points. The fixes for variance are the ones to try.

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?

Falling validation error plus a training/validation gap is the variance picture, and data is variance's most reliable cure.

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
  1. 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.
  2. 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).
  3. As data grows, training error rises and validation error falls, both toward the floor: the two learning curves squeeze together.
  4. 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.
  5. 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.
  6. 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.)

Variance is train-dev − training = 2.2 points; data mismatch is dev − train-dev ≈ 50 points. More clean data would barely help; making training look like real use cut dev error to about 12%.

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?

Catching blocked lanes (recall) is the one number to beat; the false-alarm rate is a limit. Among models under the limit, the highest recall wins.

2. First results. Two inspectors looking at the same frames disagree on 2%. Your model: training error 9%, dev error 10%. What next?

Avoidable bias 9 − 2 = 7 points, variance 10 − 9 = 1. The model can't even fit what it has seen; more data won't fix that.

3. A month later. A bigger network gets training error to 2.5% but dev error 9%. Now what?

Avoidable bias 0.5 points, variance 6.5 points. The fixes for variance come next.

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?

This is data mismatch (25 − 4 = 21 points). The fix is training data that looks like the conditions the model will meet, as with the phone scans above.

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?

Ceilings: trucks 8 × (1 − 0.40) = 4.8%, night rain 6%, labels 7.2%. The trucks are both the biggest and a single problem with a clear fix (more truck examples, or a "stopped with hazards" label).

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

7. What to carry forward

The numbers sayTryDon't bother with
Training error far above human level (avoidable bias)Bigger model, train longer, better features, less regularizationMore data
Train-dev far above training (variance)More data, augmentation, regularization, simpler modelA 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 sizeMore data will likely help
A category of mistakes is a small shareSpending weeks on it: its ceiling is low

All lessons: AI from scratch. Andrew Ng's free "Machine Learning Yearning" covers these ideas at book length.

Last updated