A question about cats
You have to write a function that takes a photograph and returns true if there is a cat in it. You may not use any machine learning. You must write ordinary rules by hand.
Spend a minute actually trying. Where do you start?
Perhaps you look for fur texture, or pointed ears, or whiskers. Then you remember cats can be curled up, photographed from behind, in shadow, black on a black sofa, or partly hidden behind a chair leg. Every rule you add breaks on some photograph. The rules never stop coming.
Two ways to make a program behave correctly
The first way is the one you already know. You work out the rule and write it down.
function isEven(n) {
return n % 2 === 0;
}
This is a good use of rules. The rule is short, exact, and never wrong. Writing a machine learning model to decide whether numbers are even would be a poor decision.
The second way is to supply examples and let the program work out the rule.
const examples = [
[photo1, true],
[photo2, false],
...
];
You never state what a cat looks like. You state what the answer is for cases you have already seen, and the program has to find something that reproduces those answers and also works on photographs it has never seen.
How to tell which one a problem needs
Ask yourself: can I write the rule down?
For converting miles to kilometers, yes. Multiply by 1.609. Use a rule.
For validating an email address, mostly yes, and the standard is written down. Use a rule.
For deciding whether a photograph contains a cat, no. You recognize cats reliably, but you cannot state the procedure you use. You know more than you can say. That gap is where machine learning belongs.
This is worth stating plainly, because it is often skipped. Machine learning is not better than ordinary programming. It is what you reach for when writing the rule is impractical, and it comes with real costs: you need data, the result is approximate, and it is much harder to debug.
The cost you are accepting
The even-number function is right for every input, forever, and you can prove it by reading it.
A learned cat detector is right most of the time, on inputs that resemble what it was shown, and you cannot prove much about it at all. When it fails you often cannot say why.
You are trading certainty for reach. Sometimes that trade is obviously worth it. Sometimes people make it without noticing they made it, and that is where most bad machine learning comes from.
Try this before the next lesson
- For each of these, decide rules or examples, and say why: converting Celsius to Fahrenheit, detecting spam email, calculating VAT, recognizing handwriting, sorting a list, recommending a film.
- Pick the hardest one and try writing three rules for it. Then find a case each rule gets wrong.
- Can you think of a problem where you would start with rules and later switch to examples? What would make you switch?