How machines learn¶
30 min Concepts Read before the project review
Machine learning is one idea. Instead of writing the rules, you show the machine examples and let it work the rules out. Everything else on this page is detail.
The switch¶
For fifty years, getting a computer to do something meant writing down how. Rules in, data in, answers out. This works beautifully when you can state the rule. Payroll, tax, sorting a list.
Now try to write the rule for telling a photo of rock from a photo of paper. Not the idea of it, the actual rule, in terms of pixel values. You cannot. You know the answer instantly and you cannot say how you know.
That is the gap machine learning fills. You supply the examples and the answers, and the machine produces the rule.
Read the second row again. The answers are an input. This is why data work is most of the job, and why a model is only ever as good as the examples it was shown.
The formal version¶
Tom Mitchell put this precisely in 1997, and his definition is still the one textbooks use. A program learns if its performance at some task improves with experience, where three things have to be named: the task, the experience it learns from, and the performance measure that says whether it is getting better.
Name all three and you have a machine learning problem. Miss one and you have an intention. There is a whole page on using this as a checklist later in this module.
The words you need¶
Every term below is something you will physically do in the first project. Keep this section open while you work.
| Term | What it means | In the project |
|---|---|---|
| Class | One of the categories you are sorting into | Rock, paper, scissors |
| Example | One item of data | One photo of a hand |
| Label | The correct answer for that example | "This one is paper" |
| Feature | Something measurable about an example | Shape, edges, colour |
| Training | Adjusting the model until it fits the examples | Clicking Train Model |
| Model | The rules the machine ended up with | The file you export |
| Prediction | The model's answer on something new | "Scissors" |
| Confidence | How sure the model is, from 0 to 100 | The bar under each class |
| Training set | The examples used to build the model | Your collected photos |
| Validation set | Held back examples used to tune your choices | Not used in project one |
| Test set | Held back examples, never used in training | The 20 photos you test with |
| Generalisation | Working on data it has never seen | The whole point |
| Overfitting | Memorising the training set instead of learning the pattern | 100 percent in the room, 50 percent elsewhere |
Two of those deserve more than a table row.
Confidence is not correctness. A model gives you a number for how well the input matches what it learned. It has no way to say "I have never seen anything like this". Show a rock, paper, scissors model a photograph of a dog and it will confidently tell you it is paper. This trips up people building real systems, not just students.
Generalisation is the only thing that matters. A model that is perfect on its training data and useless on anything else has learned nothing. It is a lookup table. The whole discipline exists to close the gap between those two numbers.
The loop¶
This is the diagram you will see for the rest of the year. Every module we do is a deeper pass at one of these five stages.
Beginners expect the interesting work to be in Train. In practice that is the step that takes the least of your time and the least of your judgement. Collect and Prepare decide whether the project succeeds.
Three ways to learn¶
Almost everything you will meet falls into one of three setups.
-
Supervised
You give examples and the right answers. The model learns the mapping. Spam or not spam, price of a flat, rock or paper or scissors.
This is most of what you will do this year, and it is what the first project uses.
-
Unsupervised
You give examples and no answers. The model finds structure on its own, usually groups.
Customer segments, topics in a pile of documents, spotting the transaction that looks unlike the rest.
-
Reinforcement
No examples. The model tries things, gets a reward or a penalty, and adjusts.
Game playing, robot control. This is how a system learns to beat humans at Go without being shown how a human plays.
Each of these has flavours that change what data you need, and the next page goes through them properly.
Knowing whether it works¶
Accuracy is the number everyone quotes and the number that hides the most.
Say your model is right on 27 of 30 test images. That is 90 percent. Now ask a better question: what were the three mistakes? A grid of actual against predicted answers it, and it takes two minutes to fill in by hand.
This model is 77 percent accurate overall. The average hides the interesting part: scissors gets called paper three times out of ten, and nothing else is badly broken. Now you have a specific problem to fix rather than a vague wish for a better number.
Ask yourself why that confusion exists. Probably because a hand showing scissors and a hand showing paper share most of their outline, and because scissors was photographed at an angle where two fingers looked like four. That sentence is machine learning. Not the training, the explaining.
Accuracy also lies when your classes are uneven
Suppose one in a hundred transactions is fraud. A model that says "not fraud" every single time scores 99 percent accuracy and catches nothing. Whenever someone quotes an accuracy figure, ask what the split of the data was.
What goes wrong¶
Three failures cause most of the trouble, and you will probably produce at least two of them this week.
Overfitting. The model learns your specific examples rather than the pattern. Signs: near perfect on training data, much worse on anything new. If every photo was taken in the same room, the model may have learned the room.
Biased data. The model learns exactly what you showed it, including what you did not intend. If every hand in your training photos belongs to one person, the model has partly learned that hand. Hiring tools trained on past hires repeat the past. A model cannot want to be fair. It can only reflect the examples.
Distribution shift. The world stops matching the training data. Lighting changes, cameras change, people change. A model that worked in March quietly stops working in September, and nobody notices because it still returns confident answers.
Notice what all three have in common. None of them is a problem with the algorithm. They are all problems with data.
Questions to sit with¶
Bring answers to the project review.
- Your model is 95 percent accurate on your own photos and 60 percent on another group's. Nothing changed but the photographer. What did it actually learn?
- Would you rather deploy a model that is 95 percent accurate and impossible to explain, or 80 percent accurate and fully explainable? Does your answer change if it is sorting photos, approving loans, or reading X-rays?
- Your model is confidently wrong on one image. Confidence 92 percent, answer incorrect. What does that tell you about what confidence measures?
- The examples you collected encode a set of choices: who was in frame, what the light was, what counted as a valid gesture. Who made those choices, and who was left out by them?
- If a model trained on the past predicts the future, what happens when the past contains something we want to change?
Next¶
You have the vocabulary. Now the method for turning a vague request into something you can actually build.