Mathematics & Logic

Deep learning

Nobody writes the rules. A network of simple calculations tunes itself on examples until its answers come out right.

  • 8min read
  • 10min listen
  • 33questions
Four translucent paper sheets float in a stack, small dots on each joined to the next by fine threads.

Deep Learning Explained

0:00 / 9:54

A question to hold while you read

Nobody can write down the rules for spotting a cat in a photo. So how does a computer get better at it?

Rules or examples

Try writing instructions for spotting a cat in a photo. Pointed ears, whiskers, fur. But cats curl into balls, hide behind chairs and turn away from the camera, and every rule you write meets an exception. For decades programmers tried to spell out by hand what a program should look for, as an algorithm: a step-by-step recipe, named after al-Khwārizmī, the ninth-century mathematician of Baghdad.

Machine learning turns the job around. People gather examples, each photo paired with its right answer, called its label, and the program adjusts itself until its answers match the labels. Deep learning is the branch of machine learning that does this with many layers of simple calculations. The learning method is still an algorithm people write; the rules for recognising a cat are not.

One small calculation

The building block of a deep network is the artificial neuron, and it does one small calculation. It takes in a few numbers, such as the brightness of some pixels. It multiplies each number by a weight, which sets how strongly that input counts, and adds the results into one weighted sum. An activation rule then decides what to pass on. A common rule gives zero whenever the sum is negative, and the sum itself when it is positive.

The design borrows loosely from nerve cells. A real neuron gathers signals from thousands of others, and the strength of each junction, called a synapse, affects how much each signal counts. But a living neuron is a cell full of chemistry and timing, while an artificial one is a line of arithmetic. These networks are not built as realistic models of the brain.

weighted sumoutput
A common activation rule: zero below zero, then a straight rise

Why depth helps

One neuron can do very little, so networks stack many of them in layers. The first layer takes the raw input, such as every pixel of a photo, and the last gives the answer. In between sit hidden layers, whose neurons pass their results only to other neurons. A network with many hidden layers is called deep.

Depth helps because simple features build complex ones, each layer working on what the layer below it found. In 2014 Matthew Zeiler and Rob Fergus looked inside a network trained to sort photos. Its first layer picked out edges and patches of colour, the second corners, the third textures such as mesh, the fourth parts such as dogs' faces, and the fifth whole objects. Nobody programmed those features in; they arose during training.

3 more questions from this passage

Measuring how wrong

Training starts with a network that knows nothing. Its weights are set at random, so show it a photo of a cat and its answer is little better than a guess. Before it can improve, it needs an exact measure of how wrong it was.

That measure is the loss: a single number that is large when the network's answers are far from the labels, and falls toward zero as they come to match. It is averaged over many examples, so a lucky answer on one photo cannot hide poor answers on the rest. Every part of training then serves one aim, to make the loss smaller, and a loss that keeps falling is the first sign that the network is learning.

training stepsloss
Schematic: the loss falling as training goes on

1 more question from this passage

Walking downhill

How does a network know which way to change its weights? Picture the loss as a landscape of hills and valleys. Each spot is one setting of the weights, and the height there is the loss. Training wants to reach low ground, but it can never see the whole landscape, only the slope under its feet.

So it takes a small step in whichever direction runs most steeply downhill, checks the slope again, and repeats, thousands or millions of times. This is gradient descent: gradient is the mathematician's word for slope, and descent means going down. The method is older than computers; the French mathematician Augustin-Louis Cauchy described it in 1847. The steps are kept small, because a big step can overshoot the valley.

one weightloss
Schematic: the loss as one weight changes, lowest in the valley

That answers the question you started with: Nobody can write down the rules for spotting a cat in a photo. So how does a computer get better at it?

2 more questions from this passage

Sharing out the blame

A large network has millions of weights, and gradient descent needs a direction for every one of them: should this weight go up or down to lower the loss? Nudging each weight in turn to find out would take far too long.

Backpropagation finds every direction in one sweep. It starts at the output, where the loss is known, and works backwards through the layers toward the input, calculating how much each weight contributed to the loss. In effect it shares out the blame, layer by layer. Versions of the idea appeared in the 1970s, but a 1986 paper by David Rumelhart, Geoffrey Hinton and Ronald Williams showed it could teach hidden layers to find useful features for themselves, and it became the standard way to train networks.

3 more questions from this passage

The exam it has never seen

A student who memorises last year's exam answers can score full marks on that paper and still fail a new one. Networks face the same trap, so their makers split their examples.

Most go into the training set, the examples the network learns from. A separate share, the test set, is locked away: the network never adjusts a single weight on it. Because those examples are new to it, the score on them is a fair estimate of how the network will do on photos it meets later. The ImageNet contest, for instance, held back 100,000 of its photos for testing. Doing well on examples never seen in training is called generalisation, and it is the whole point of learning.

training set — 90%validation set — 3%test set — 7%
How the ImageNet contest split its photos. Validation photos are for checks during development.

2 more questions from this passage

Memorising instead of learning

Why keep a test set at all? Because a network with millions of weights can do what the student did, and memorise. Trained long enough, it can fit every quirk of its training photos, even a stray smudge or a wrongly labelled picture, and score almost perfectly on them while doing worse on anything new. This is overfitting.

It shows up as a telltale split. As training goes on, the error on the training set keeps falling. The test error falls at first too, then bottoms out and starts to climb, because the network is now learning accidents of its own examples rather than patterns that hold in general. Two common remedies are more varied examples, and stopping training once error on the separate validation photos stops improving.

3 more questions from this passage

Networks built for pictures

A photo a thousand pixels wide and a thousand high holds a million pixels, three numbers each for colour. If every neuron in the first layer had its own weight for every pixel, the network would need an enormous number of weights, and a pattern learned in one corner would have to be learned again everywhere else.

A convolutional network avoids both problems. Its layers use filters: small grids of weights, a few pixels across, each slid over the whole image to test every patch for one pattern, such as an upright edge. Reusing the same weights everywhere, one filter finds that edge wherever it appears. The idea echoes work by David Hubel and Torsten Wiesel, who found cells in a cat's brain that respond to edges at particular angles. In 1989 Yann LeCun's team trained such a network to read handwritten zip codes on US mail.

2 more questions from this passage

A false start

The idea is older than it looks. In 1943 Warren McCulloch and Walter Pitts described a mathematical neuron that added up its inputs and fired when they passed a threshold, but a person had to set its weights. In 1958 the psychologist Frank Rosenblatt demonstrated the perceptron, a single layer of such units that could learn its own weights from examples. After fifty tries it could tell cards marked on the left from cards marked on the right, and newspapers reported the US Navy's hope that such machines might one day walk, talk and be conscious.

Then the mood soured. In 1969 Marvin Minsky and Seymour Papert proved that a single layer cannot learn some very simple patterns. Interest and funding dwindled for years, until backpropagation made training hidden layers practical.

2 more questions from this passage

The 2012 turning point

For years computers were poor at naming what a photo showed. The ImageNet challenge measured their progress: programs sorted test photos into 1,000 categories, and an answer counted as wrong unless the right category was among its five best guesses. The winning error was 28 per cent in 2010 and 26 per cent in 2011.

In 2012 a deep convolutional network from the University of Toronto, built by Alex Krizhevsky, Ilya Sutskever and Geoffrey Hinton and now known as AlexNet, cut the error to 16 per cent, using features it had learned from the training photos. The next-best entry, built on features designed by people, stayed at 26. After a gap that size, most of the next year's entries used deep networks, and by 2014 the winning error was below 7 per cent.

28.2 %201025.8 %201116.4 %201211.7 %20136.7 %2014
Winning error in the ImageNet challenge, by year

2 more questions from this passage

Why it worked at last

Most of AlexNet's ideas were decades old, so why did they work only in 2012? Three things had arrived together.

The first was data. ImageNet's makers had paid tens of thousands of online workers to sort millions of photos, giving the contest over 1.2 million labelled training photos, far more than earlier networks had. The second was hardware. Graphics chips, designed to draw video games, do thousands of sums in parallel, which is exactly the work a network needs. Using two gaming graphics cards, AlexNet trained in five to six days. The third was better methods: an activation rule with which networks learn several times faster than with older ones, and dropout, which switches off random neurons during training and so reduces overfitting.

3 more questions from this passage

Only as good as its examples

A network knows only what its examples show it, and that has two costs. The first is hunger for data. AlexNet learned from about a thousand labelled photos of every category it knew, while a person can often grasp a new handwritten letter from just one example.

The second is bias. Whatever gaps its examples have, the network inherits. In 2018 Joy Buolamwini and Timnit Gebru tested three commercial systems that judge gender from a face. All three made the most mistakes on darker-skinned women; the worst got about one in three of them wrong, but fewer than one in a hundred lighter-skinned men. They also found that two collections widely used to test such systems were about 80 and 86 per cent lighter-skinned, so gaps like this were easy to miss. A network learns the balance of whatever it is shown.

34.7 %darker-skinned women12 %darker-skinned men7.1 %lighter-skinned women0.3 %lighter-skinned men
One commercial system's error when judging gender from faces, 2018

2 more questions from this passage

Right for the wrong reason

When a network gets an answer right, can it tell us why? Not in any form a person can read. AlexNet's answer comes from some 60 million numbers acting together, and nowhere among them is a reason written down. That is why deep networks are often called a black box: we see what goes in and what comes out, but no reason.

This matters because a network learns any pattern matching the label, even a correlation that has nothing to do with the thing itself. In 2021 researchers found that networks built to detect COVID-19 on chest X-rays leaned on text markers and on how patients were positioned, clues specific to each collection of scans. On X-rays from other hospitals, about half their predictive performance vanished. Matching patterns like this is not understanding: the network has tuned numbers until its outputs fit.

2 more questions from this passage

33 questions came out of this reading. Answer them out loud on your phone, and EdenMind schedules each one for the day you’re about to forget it.

Add to my practice