AI from scratch · Start 1 · Stage 0: start here
What machine learning is
You don't need any math or code for this lesson. By the end you'll have trained a model twice, once with your own hands and once by letting the computer do it, and you'll know what an ML engineer actually does all day. Everything else on this path builds on the one idea here: a model is a function with knobs, and learning means turning the knobs until the model agrees with the examples.
1. Rules you write vs rules you learn
A normal program follows rules a person wrote: "sales tax is 8.875% of the price". That works when someone can write the rule. Now try writing the rule for "this photo contains a cat". Pointy ears? Some cats have folded ears. Whiskers? So do dogs. Every rule breaks.
Machine learning flips it around. Instead of the rule, you give the computer examples with answers: ten thousand photos, each labeled "cat" or "not cat". The computer finds a rule that matches the examples, and you hope it works on photos it has never seen. That hope, and making sure it's justified, is most of the job.
Which of these is a job for machine learning rather than a hand-written rule?
- Working out the sales tax on a receipt
- Reading a handwritten street address off an envelope
- Sorting a list of names alphabetically
A normal program follows rules a person wrote; a model finds its rule from examples that come with answers.
2. A model is a function with knobs
Here's a small, made-up example: 40 taxi rides, each a dot. Across is how far the ride went, up is how many minutes it took. We want to predict the time for a ride we haven't taken yet. The simplest guess is a straight line:
\[ \text{minutes} = a \times \text{km} + b \]
That formula is the model, and \(a\) and \(b\) are its knobs (ML people call them parameters or weights). \(a\) is minutes per kilometer; \(b\) is the time every ride costs no matter how short: getting in, the first red light. Turn the knobs and try to make the blue line run through the middle of the dots. Each red stick is one ride's mistake: how far the line's guess is from what really happened.
How do we say how good a line is, with one number? Square each red stick (so misses above and below both count, and big misses count a lot) and average them. That's the mean squared error, the readout above. Getting it small is the whole goal. A good fit here gets it down to about 8.5; you can't reach zero, because two rides of the same length don't take the same time. Traffic is noise no line can predict.
With \(a = 3\) and \(b = 5\), how many minutes does the model predict for a 4 km ride?
A model is a formula with knobs, and one number, the error, says how good a setting of the knobs is.
Why square the misses?
- Some rides take longer than the line says (a miss above it), some shorter (a miss below). If we just averaged the misses, the ups and downs would cancel: a badly tilted line could still average out to zero.
- Squaring makes every miss count as positive, so nothing cancels.
- It also makes big misses count much more than small ones: one 4-minute miss counts 16, while two 2-minute misses count 4 + 4 = 8. A line that is a little off everywhere beats one that is way off somewhere.
- And as you turn a knob, the squared error changes smoothly, in a bowl shape with one lowest point. That's what lets the computer find "downhill" in the next section.
- It's a choice, not a law. Averaging the plain sizes of the misses (ignoring up or down) is another good choice, and stage 1 shows that the two lead to different "best" lines.
3. Training: let the computer turn the knobs
Press Let the computer train it. The computer does what you did, but carefully: for each knob it asks "if I nudge you up a tiny bit, does the error go up or down?", then moves every knob a small step in the direction that lowers the error, and repeats. The strip under the picture shows the error after each step. That loop is training, and the method is called gradient descent. Stage 1 shows exactly how the computer gets the "up or down" answer (it's a derivative), and stage 2 shows the same loop training a neural network.
After training, the error stops falling at about 8.5 and never reaches zero. Why?
- The rides really vary: traffic and lights aren't in the data, so no line can hit every dot
- Training stopped too early: more steps would reach zero
- There's a bug in the training
One honest warning: the real test of a model is rides it hasn't seen. A model that fits its examples perfectly but fails on new ones is useless, and that trap, called overfitting, gets a whole lesson in stage 3.
Training is turning every knob a little in the direction that lowers the error, over and over.
4. Three ways to learn
| kind | what you give it | examples |
|---|---|---|
| Supervised | examples with the right answers | ride → minutes; photo → "cat"; email → spam or not; sentence → next word (that's how chatbots are trained) |
| Unsupervised | examples with no answers; find structure | group customers who behave alike; spot an unusual night on the subway |
| Reinforcement | a world to act in and a score (reward) | a game-playing program; a robot learning to walk; tuning a chatbot from people's thumbs up |
The taxi model was supervised: every ride came with its true time. Most ML at work is supervised.
You have a year of hourly crowd counts for every subway station, with no labels, and want to find stations whose days look alike. Which kind is that?
- Supervised
- Unsupervised
- Reinforcement
Supervised learning learns from answers; the other kinds find structure without answers, or learn from rewards.
Where it breaks: when learning from examples goes wrong
- New cases unlike the examples. A taxi model learned from ordinary days knows nothing about a snowstorm. A model can only repeat patterns its examples contained.
- Wrong answers in the examples. If the people labeling photos mistook some foxes for dogs, the model learns their mistake as faithfully as everything else.
- A pattern that isn't the reason. In one well-known experiment, a model told wolves from huskies almost perfectly, by looking for snow in the background: most wolf photos had snow. It found a rule that matched the examples, just not the rule anyone meant. Checking what a model actually looks at is part of the job.
5. What an ML engineer does
Training is maybe a tenth of the job. The rest is a loop, and this path teaches every step of it:
| step | the question | where you learn it |
|---|---|---|
| Frame | What exactly are we predicting, and how will we know it's good enough? | stage 3 |
| Data | Where do examples come from? Are the answers right? Is anything leaking the answer? | stages 3–4 |
| Baseline | What does the simplest possible model score? | stage 3 |
| Model | Which model, which knobs, and how to train it | stages 1, 2, 4, 5 |
| Evaluate | How good is it on data it never saw, and where does it fail? | stage 3 |
| Ship and watch | Make it fast and cheap, put it in front of users, notice when the world changes | stages 6–7 |
Training is a small part of the job; the rest is getting good data, measuring honestly, and keeping the model working.
6. How these pages work
Boxes marked Your turn ask you to predict before you're told: guessing first, even wrong, is how it sticks. Sliders and dots in pictures can be dragged. And code boxes run right in the page. Try this one: press Run, then change a number and run it again.
Don't worry if the code looks foreign: the next lesson teaches exactly enough to read and change everything on this path. And Blip, the companion in the corner, can answer questions about whatever page you're on.
7. What to carry forward
- Machine learning finds a rule from examples, for problems where nobody can write the rule.
- A model is a formula with knobs (parameters); running it means putting an input in and reading the output.
- Training means turning the knobs to make one number, the error (loss), as small as possible. Gradient descent does it by repeatedly nudging every knob downhill.
- Noise sets a floor: a model can't predict what isn't in its inputs.
- What counts is how the model does on examples it hasn't seen.
All lessons: AI from scratch.
Last updated