Part II · Chapter 5
Learning: Finding the Rules Instead of Writing Them
If nobody can write enough rules, perhaps the machine can find them. Give it many examples of messages, each marked “urgent” or “can wait”, and let it search for whatever rule sorts them correctly. That is learning, and it turns out to need nothing more than the rule-following of Chapter 1 and a way to measure mistakes.
If you cannot write the rule, write a way to measure mistakes, and let the machine adjust itself until it makes fewer of them.
A sorter with knobs
Suppose a children’s helpline receives thousands of messages a day, and we want a machine to put each one in one of two piles: urgent or can wait. Chapter 4 showed that we can write useful rules, but not practically enough of them to cover real messages. But we do have something else: last year’s messages, each one marked afterward by an experienced nurse as urgent or not.
Here is a very simple machine that could learn from them. Give every word a knob: a number that says how much that word pushes a message toward “urgent”. To judge a message, add up the knobs for its words. If the total is above zero, call it urgent. At the start every knob is set at random, so the sorting is no better than chance.
Now show the machine last year’s messages one at a time.
- If it called an urgent message “can wait”, turn up a little the knobs of the words in that message.
- If it called a routine message “urgent”, turn them down a little.
- If it was right, leave the knobs alone.
After thousands of messages, the knob for “purple” has drifted up, and so have those for “floppy”, “spots” and “glass”. The knob for “diaper” has drifted down. Nobody set them. They were found. And the machine now handles messages it has never seen, including phrasings no rule-writer thought of, as long as they use words like those in its examples.
This idea is old. A machine of nearly this kind, the perceptron, was built in the late 1950s. On its own it is far too simple for the nurse. It can learn a weight for “don’t”. But “the spots don’t fade” should raise concern, while “the spots don’t look purple” can lower it. One fixed weight per word cannot capture how a word’s effect changes with the words around it. But it contains every ingredient of modern machine learning.
Three ingredients
1. A flexible machine with adjustable numbers. Modern versions are called neural networks, loosely inspired by brain cells. A network is built in layers. Each unit in a layer takes the numbers coming in, multiplies each by its own adjustable weight and adds them up. It then passes the result on after a small bend: for example, it turns negative totals into zero. That bend matters. Without it, stacking layers would add nothing; with it, each layer can build new features out of the previous layer’s. One unit might learn to respond to “spots + don’t fade”, which the simple word-adder could never represent. A large network has millions or billions of these weights.
One catch: layers can combine whatever information the network is given, but they cannot recover information it was never given. If the input is only a tally of which words appeared, no number of layers can tell “the dog bit the child” from “the child bit the dog”: the words are the same. A network that reads language must also be told where each word sits in the sentence. Chapter 8 shows how modern language models do this.
2. A score for being wrong. We need a single number that says how badly the machine is doing on the examples. It is called the loss. For sorting it is usually based on how much probability the machine gave to the right answer: confident and right scores near zero, confident and wrong scores high. Everything the machine learns is shaped by this choice. Measure the wrong thing and it will learn the wrong thing, a warning that returns in Chapters 10 and 12.
3. A way to improve. Picture the loss as a landscape, with every possible setting of the weights as a place and the loss as the height. Learning means walking downhill. You are in thick fog and can only feel the slope under your feet. So you take a small step in the steepest downward direction, feel the slope again, and repeat. This is gradient descent. (The gradient is the list of slopes, one for each weight.)
Figure 5.1. Gradient descent: from a random start, small steps down the slope of the loss toward fewer mistakes. A low point is not always the lowest.
A practical problem arises: with a billion weights, how do you find every slope? Trying each weight separately would take a billion trials per step. Backpropagation solves this. It works backward from the loss through the layers, using a rule from school calculus (the chain rule). It finds every slope at once, for roughly the cost of one extra pass through the network. Without it, training large networks would be hopeless.
Training is a path, running is rule-following
Look at this with Chapter 1’s eyes.
- Training is a path through a space of states. The state is the full set of weights. The rule is “take a small step downhill on the loss”. Millions of small steps trace a path from a random starting point to a setting that sorts messages well.
- Running the trained network is plain rule-following: multiply, add, bend, pass on. This is arithmetic, done one fixed step after another, exactly what a universal machine does.
So nothing new has been added to computation itself. What is new is where the rulebook comes from. In Chapter 4 people wrote it. Here the rulebook is the final setting of the weights, found by search.
Figure 5.2. Training is a loop that changes the weights; use only reads them.
At this step, the machine is an email spam filter, which learned from marked examples to sort messages into two piles.