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?

Nobody can write down the rules for every person's handwriting, but it's easy to collect millions of envelopes along with the address that turned out to be right. When the rule is hard to write and examples are easy to get, that's machine learning's territory.

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?

\(3 \times 4 + 5 = 17\). That's all "running a model" means: put the input into the formula and read off the output. A huge language model does the same thing, with billions of knobs instead of two.

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?
  1. 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.
  2. Squaring makes every miss count as positive, so nothing cancels.
  3. 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.
  4. 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.
  5. 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?

A model can only use what it's given. With just the distance, about 8.5 is the best any line can do. To do better you'd add information (time of day, weather, borough), which is called feature engineering, or use a more flexible model. Telling "my model is bad" apart from "the data can't say more" is a core skill on this path (stage 3).

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

kindwhat you give itexamples
Supervisedexamples with the right answersride → minutes; photo → "cat"; email → spam or not; sentence → next word (that's how chatbots are trained)
Unsupervisedexamples with no answers; find structuregroup customers who behave alike; spot an unusual night on the subway
Reinforcementa 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?

No answers, just looking for groups: unsupervised learning (clustering, in stage 3).

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

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:

stepthe questionwhere you learn it
FrameWhat exactly are we predicting, and how will we know it's good enough?stage 3
DataWhere do examples come from? Are the answers right? Is anything leaking the answer?stages 3–4
BaselineWhat does the simplest possible model score?stage 3
ModelWhich model, which knobs, and how to train itstages 1, 2, 4, 5
EvaluateHow good is it on data it never saw, and where does it fail?stage 3
Ship and watchMake it fast and cheap, put it in front of users, notice when the world changesstages 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

All lessons: AI from scratch.

Last updated