Skip to content

← Grade 11 Computing

Neural Networks

Unit 8 · Artificial intelligence

Hide the stories, facts, photos, activities and interactives, for revising before an exam
In this unit 11 sections
  1. A neuron: a weighted vote
  2. Layers
  3. Learning by small corrections
  4. Training a network to read digits
  5. Overfitting: learning the examples, not the idea
  6. What did it learn?
  7. Judging the training data
  8. Make a model of your own
  9. Project: a digit reader for your class
  10. In short
  11. Exercise

Write the digit ४ on a piece of paper. Now ask three friends to write one too. No two will look the same: one is round, one is pointed, one leans to the left, one has a long tail. Yet you read every one of them at once. Try to write down the rule you use, a rule a computer could follow, and you will find you can’t. Nobody can. There are too many ways to write ४.

So computers that read handwriting aren’t given rules. They are given thousands of examples, each with its right answer, and they learn. In Grade 9 you built a model that learns from examples, nearest neighbors, and in Grade 10 you saw that a chatbot is billions of numbers tuned by training. In this unit you open up the kind of model behind those chatbots and almost every other AI today: the neural network. You will build one neuron by hand, see why one is not enough, watch a network learn by taking small steps downhill, and then train a real network, on this page, to read handwritten digits, ०–९ as well as 0–9. Then comes the most important part: looking inside it to see what it really learned, and where it fails.

A neuron: a weighted vote

Every Saturday morning, the captain of a school cricket club in Bhaktapur decides whether the match goes ahead. Three things matter to her: are there enough players, is the ground dry, and are there exams on Monday? They don’t matter equally. Enough players matters most; exams on Monday count strongly against playing. In her head she gives each one a weight, adds them up, and decides.

Write that down as numbers. Each question is an input, 1 for yes and 0 for no. Each input has a weight: how much it counts, and which way. A positive weight votes for playing, a negative one against. And one number says how hard it is to say yes at all, whatever the inputs: the bias.

Input Weight
enough players? (1 or 0) 3
ground dry? (1 or 0) 2
exams on Monday? (1 or 0) −4
bias −4

To decide, multiply each input by its weight, add them up, and add the bias. This total is the weighted sum. If it is above 0, the answer is 1, play; otherwise 0, don’t. Enough players and a dry ground, no exams: 3 + 2 + 0 − 4 = 1, above 0: play. Enough players but a wet ground: 3 + 0 + 0 − 4 = −1: don’t play.

That is an artificial neuron: inputs, a weight for each, a bias, the weighted sum, and a rule that turns the sum into the neuron’s output. The rule is called the activation function. The one here, 1 above 0 and 0 otherwise, is called a step, because its graph jumps up like a step.

A neuron is a weighted vote: each input votes, some votes count more than others, some vote against, and the bias sets how many votes it takes to win.

Under a microscope, a dark nerve cell with a small body and many thin, branching threads spreading out from it, on a pale background
Figure 8.1 A real neuron, a nerve cell from a human brain, stained dark to show it, magnified 40 times. Its branching threads gather signals from thousands of other neurons; when enough arrive together, it sends a signal of its own along its one long fiber. Photo: MethoxyRoxy, CC BY-SA 2.5

The name comes from the brain. A real neuron (a nerve cell) collects small signals from thousands of other neurons. Some make it more likely to fire, some less. When enough arrive together, it fires a signal of its own. The artificial neuron borrows that one idea, many weighted signals added up against a threshold, and leaves out almost everything else a living cell does.

In Python, a neuron is a short function. Here is the captain’s:

def neuron(inputs, weights, bias):    """A neuron with a step: 1 if the weighted sum is above 0, else 0."""    total = bias    for x, w in zip(inputs, weights):        total = total + x * w    return 1 if total > 0 else 0 # enough players, ground dry, exams on Monday (1 for yes, 0 for no)weights = [3, 2, -4]bias = -4print(neuron([1, 1, 0], weights, bias))print(neuron([1, 0, 0], weights, bias))print(neuron([1, 1, 1], weights, bias))print(neuron([0, 1, 0], weights, bias))

Output

1000

zip(inputs, weights) takes the inputs and the weights in pairs, the first with the first and so on, so the loop adds up input × weight for each pair. Only the first case, enough players and a dry ground with no exams, gets the match played.

A neuron has inputs 1 and 0, weights 2 and 5, and bias −1, with a step activation. What is its output?

Pick an answer to see why.

In the captain’s neuron, what does the weight −4 on “exams on Monday” mean?

Pick an answer to see why.

A neuron as a logic gate

In Grade 7 you met logic gates Grade 7: Logic gates: an AND gate gives 1 only when both inputs are 1, and an OR gate gives 1 when at least one is. A single neuron with two inputs can act as either one. You only have to find the right weights and bias.

Try it

Make a neuron into a gate

A neuron with two inputs. Move the sliders until the output column matches the wanted column for AND, then for OR. The square shows the four possible inputs: filled points should give 1, empty ones 0. The shaded part is where the neuron says 1.

x₁x₂× 1.0× 1.0Σ+ 0.0biasstep01x₁01x₂shaded: says 1 x₁x₂× 1.0× 1.0Σ+ 0.0biasstep01x₁01x₂shaded: says 1
Make it:
x₁x₂sumoutputwanted
000.000
011.010
101.010
112.011

Not yet: compare the output column with the wanted column. Which rows are wrong? Raise the bias to say 1 more often, lower it to say 1 less often.

All four rows right. The line now has exactly the filled points on its shaded side.

XOR wants 1 at the top left and bottom right, 0 at the other two corners. Can one straight line put both filled points on one side and both empty ones on the other?

All four right? Check again: that should be impossible.

Hint
Answer

Look at the square as you change the bias. The neuron’s rule, “say 1 where w₁ x₁ + w₂ x₂ + bias is above 0”, always cuts the square with one straight line. Changing the weights turns the line; changing the bias slides it. A neuron can only ever say 1 on one side of a line and 0 on the other.

What one neuron can’t do

Now try XOR in the widget, the “one or the other but not both” gate from Grade 7: 1 for (0, 1) and (1, 0), 0 for (0, 0) and (1, 1). However you move the sliders, at least one row stays wrong.

The square shows why. XOR’s filled points sit at two opposite corners, and its empty points at the other two. To give 1 for both filled points, the shaded side must hold both of them; but any straight line with the two filled corners on one side has at least one empty corner there too. No line can split them, so no single neuron can be an XOR gate, whatever its weights.

An old photograph of a camera on a tripod beside tall cabinets of electronics, with labels such as camera, association units and response panel
Figure 8.2 The Mark I Perceptron, from its operator’s manual of 1960. The camera on the left was its eye, with 400 light sensors; the racks of association units held its neurons, whose weights were knobs turned by small electric motors as it learned. Photo: John C. Hay, Albert E. Murray, Public domain

Why can no single neuron act as an XOR gate?

Pick an answer to see why.

Layers

If one neuron draws one line, two neurons can draw two lines. Put two neurons side by side, both looking at the same inputs, and then a third neuron that looks at their outputs. That is a small neural network, and it can do XOR.

1−1−1111x₁x₂h₁ORbias −0.5h₂NANDbias 1.5outANDbias −1.5= XORinputshidden layeroutput01x₁01x₂h₁’s lineh₂’s lineboth say 1:output 1 1−1−1111x₁x₂h₁ORbias −0.5h₂NANDbias 1.5outANDbias −1.5= XORinputshidden layeroutput01x₁01x₂h₁’s lineh₂’s lineboth say 1:output 1
Figure 8.3 A network of step neurons that works out XOR. The hidden neuron h₁ is an OR gate and h₂ a NAND gate (NOT AND: 0 only when both inputs are 1). The output neuron is an AND of the two. In the square, each hidden neuron draws one line; between the lines is a strip where both say 1, so the output says 1. The strip holds exactly (0, 1) and (1, 0).

The neurons are arranged in layers. The input layer is just the inputs, x₁ and x₂: no neurons, no weights. The output layer gives the network’s answer. Every layer between them is a hidden layer: its outputs aren’t the answer, and nobody tells it what they should be. Here the hidden layer has two neurons. Each layer takes the outputs of the layer before it as its inputs.

Working out a network’s answer means going layer by layer, from the inputs to the output. This is called the forward pass. For Figure 8.3:

x₁ x₂ h₁ = step(x₁ + x₂ − 0.5) h₂ = step(−x₁ − x₂ + 1.5) output = step(h₁ + h₂ − 1.5)
0 0 step(−0.5) = 0 step(1.5) = 1 step(−0.5) = 0
0 1 step(0.5) = 1 step(0.5) = 1 step(0.5) = 1
1 0 step(0.5) = 1 step(0.5) = 1 step(0.5) = 1
1 1 step(1.5) = 1 step(−0.5) = 0 step(−0.5) = 0

The output column is XOR. The same forward pass in Python, reusing the neuron function:

def neuron(inputs, weights, bias):    total = bias    for x, w in zip(inputs, weights):        total = total + x * w    return 1 if total > 0 else 0 def xor_network(x1, x2):    h1 = neuron([x1, x2], [1, 1], -0.5)      # the hidden layer: OR...    h2 = neuron([x1, x2], [-1, -1], 1.5)     # ...and NAND    return neuron([h1, h2], [1, 1], -1.5)    # the output layer: AND of the two for x1 in [0, 1]:    for x2 in [0, 1]:        print(x1, x2, "->", xor_network(x1, x2))

Output

0 0 -> 00 1 -> 11 0 -> 11 1 -> 0

A network with many hidden layers, one after another, is called a deep network, and training one is deep learning. More layers let a network build ideas on ideas: in a network that reads photos, the first layers find edges, later ones shapes made of edges, and later still whole objects made of shapes.

In a forward pass, what does a hidden layer take as its inputs?

Pick an answer to see why.

From a step to a smooth curve

The step has a problem, which you will meet properly in the next section. A tiny change to a weight usually changes nothing at all: the sum moves a little, stays on the same side of 0, and the output stays the same. Then the change jumps the sum across 0, and the output leaps from 0 to 1. To learn by small corrections, a network needs small changes in its weights to make small changes in its output.

So networks that learn use a smooth activation function instead. A common one is the sigmoid, an S-shaped curve:

σ(z)=11+e−z \sigma(z) = \frac{1}{1 + e^{-z}}

where z is the weighted sum and e is the number 2.718… (Euler’s number, the same Euler as Unit 5’s method). You don’t need to work it out by hand; what matters is its shape:

sum z −4 −2 0 2 4
sigmoid σ(z) 0.02 0.12 0.5 0.88 0.98

A big negative sum gives nearly 0, a big positive sum nearly 1, like the step; but in between the output changes smoothly. A sigmoid neuron’s output is a number between 0 and 1, which can be read as “how sure it is”. (Large networks today often use an even simpler activation called ReLU: the sum itself if it is above 0, else 0. The idea is the same: a simple rule applied to the weighted sum.)

A network that reads digits

A handwritten digit, for a computer, is a picture: a grid of pixels, each a number. In this unit the pictures are 14 × 14 = 196 pixels, each from 0 (paper) to 1 (ink). Those 196 numbers are the network’s inputs, one per pixel.

14 × 140123456789196inputs32hidden10 outputsone per pixelone per digit

The 4: from MNIST, by Yann LeCun, Corinna Cortes and Christopher J. C. Burges (© Yann LeCun and Corinna Cortes), CC BY-SA 3.0; shrunk to 14 × 14 by Science Playground and shared under the same license

Figure 8.4 The network you will train. A handwritten 4, 14 × 14 pixels, becomes 196 inputs. Every input is joined to each of 32 hidden neurons, and every hidden neuron to each of 10 output neurons, one per digit. Only some of the inputs and hidden neurons are drawn; the dots stand for the rest.

Each of the 32 hidden neurons has 196 weights, one for every pixel, and a bias: 196 × 32 = 6,272 weights and 32 biases. Each of the 10 output neurons has 32 weights, one for every hidden neuron, and a bias: 320 weights and 10 biases. In all, 6,634 numbers, the network’s parameters (Grade 10’s word). Training must find good values for all of them.

There is one output neuron for each digit, 0 to 9. A last step, called softmax, turns the ten outputs into chances that are all positive and add up to 100%. The network’s answer is the digit with the biggest chance. For the 4 in Figure 8.4, the output for 4 should be the largest. (Its error is measured by how small a chance it gave the right digit, rather than by squared misses as in the next section, but it is made smaller in exactly the same way.)

Compare this with Grade 10’s language models Grade 10: Training: from random numbers to a model: they are made of the same parts, neurons with weights in layers, but with billions of parameters instead of 6,634, and built to take in tokens instead of pixels.

A network has 100 inputs, one hidden layer of 20 neurons and 5 outputs. How many weights (not counting biases) does it have?

Pick an answer to see why.

Learning by small corrections

A network’s weights start as random numbers, so at first its answers are nonsense. Training changes the weights, a little at a time, until the answers are good. To see how, start with the smallest model there is: one weight.

One weight: the price of a plate of momos

A friend has four bills from a momo shop, but the menu is lost. How much is one plate? The bills don’t quite agree (maybe a tip was added, maybe the price changed):

Plates Bill (Rs)
1 160
2 300
3 470
4 610

The model is: bill = w × plates, where w, the price of a plate, is its one weight. For any guess of w the model misses each bill by some amount. Square each miss (so misses above and below both count, and big misses count a lot), and take the average. That number is the model’s error (also called its loss): how wrong it is, in one number. Learning means making the error small.

Plot the error for every possible w and you get a bowl (the widget below draws it): a big error far below the best price, a big error far above it, and a lowest point at the best price in between. The model doesn’t know where the bottom is. It only knows where it is now, and it can feel the ground under its feet: the slope of the bowl at its current w, how fast the error changes as w grows.

  • If the slope is negative, the error goes down as w goes up: make w bigger.
  • If the slope is positive, make w smaller.
  • The steeper the slope, the farther from the bottom, so the bigger the step can be.

All three fit into one rule, repeated:

wnew=w−learning rate×slope w_{\text{new}} = w - \text{learning rate} \times \text{slope}

The learning rate is a small number we choose: how big a step to take for each unit of slope. Each repeat is one small correction, downhill. This is gradient descent: “gradient” is another word for slope, and “descent” means going down.

Try it

One weight, rolling downhill

The curve is the error for each price w. The model starts at Rs 100 a plate. Each press takes one step: the learning rate × the slope (the black line touching the curve is the slope). Try each learning rate.

025507510050100150200250price per plate, w (Rs) →↑ error (thousands)
Learning rate:
Steps
0
Price w
Rs 100.0
Slope
−805.0
Error
21,650

The model guesses Rs 100 a plate, far too low, so the error is big. The slope is steep and negative: making w bigger makes the error smaller. Take a step.

Each step goes the right way, but a tiny learning rate means tiny steps. It will get there, slowly.

Big steps where the bowl is steep, smaller ones as it flattens near the bottom, because each step is the rate × the slope.

Too big: each step jumps right over the bottom to the other side. It zigzags, and still closes in, slowly.

Far too big: each jump lands farther up the other side than it started. The error grows instead of shrinking.

The weight has jumped off the chart. With this learning rate, training never settles: it gets worse every step.

At the bottom: Rs 153.67 a plate. The slope is 0, so the steps stop. The error isn’t 0, because no one price fits all four bills exactly.

What you should see

How does the program know the slope? The simplest way needs no calculus: nudge w by a tiny amount and see how much the error changes. The change in error divided by the nudge is the slope. Here is the whole of gradient descent for one weight:

# The price of a plate of momos, learned from four billsbills = [(1, 160), (2, 300), (3, 470), (4, 610)]   # (plates, total in rupees) def error(w):    """The average of the squared misses, if a plate costs w rupees."""    total = 0    for plates, paid in bills:        miss = w * plates - paid        total = total + miss * miss    return total / len(bills) def slope(w):    """How fast the error changes as w grows: nudge w a tiny bit and see."""    tiny = 0.001    return (error(w + tiny) - error(w)) / tiny w = 100                  # a first guessrate = 0.05              # the learning ratefor step in range(1, 7):    s = slope(w)    w = w - rate * s     # a small step downhill    print(f"step {step}: slope {s:8.1f}, w = {w:6.2f}, error {error(w):8.1f}")

Output

step 1: slope   -805.0, w = 140.25, error   1399.3step 2: slope   -201.2, w = 150.31, error    133.6step 3: slope    -50.3, w = 152.83, error     54.4step 4: slope    -12.6, w = 153.46, error     49.5step 5: slope     -3.1, w = 153.61, error     49.2step 6: slope     -0.8, w = 153.65, error     49.2

The slope shrinks as the model nears the bottom, so the steps shrink by themselves. The error stops at about 49, not 0: no single price fits all four bills exactly. The best w is the one with the smallest error, about Rs 153.67.

Thousands of weights at once

A network has many weights, but the idea doesn’t change. The error now depends on all of them. For each weight, find the slope of the error for that weight alone (how the error changes if only that weight is nudged), and move every weight a small step downhill, all at once. The list of all these slopes is the gradient. Picture standing on a hillside in fog: you can’t see the valley, but you can feel which way the ground slopes under your feet, and take a step that way.

Here is a small network learning XOR by itself, from random weights. It has two hidden sigmoid neurons and one output neuron: 9 weights and biases, kept in one list w. Every round, it finds each one’s slope by nudging it, then moves them all downhill:

import mathimport random def sigmoid(z):    return 1 / (1 + math.exp(-z)) def network(w, x1, x2):    """Two hidden neurons and one output neuron: 9 weights and biases in w."""    h1 = sigmoid(w[0] * x1 + w[1] * x2 + w[2])    h2 = sigmoid(w[3] * x1 + w[4] * x2 + w[5])    return sigmoid(w[6] * h1 + w[7] * h2 + w[8]) examples = [(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)]   # XOR def error(w):    total = 0    for x1, x2, want in examples:        miss = network(w, x1, x2) - want        total = total + miss * miss    return total / len(examples) random.seed(11)w = [random.uniform(-1, 1) for i in range(9)]   # a random startrate = 2.0tiny = 0.0001for rnd in range(1, 3001):    e = error(w)    slopes = []    for i in range(9):                  # the slope for each weight in turn        w[i] = w[i] + tiny        slopes.append((error(w) - e) / tiny)        w[i] = w[i] - tiny    for i in range(9):                  # then a small step downhill for all of them        w[i] = w[i] - rate * slopes[i]    if rnd in (1, 500, 1000, 2000, 3000):        print(f"round {rnd:4}: error {error(w):.4f}") for x1, x2, want in examples:    print(x1, x2, "->", round(network(w, x1, x2), 2), "wanted", want)

Output

round    1: error 0.2505round  500: error 0.2169round 1000: error 0.0045round 2000: error 0.0011round 3000: error 0.00060 0 -> 0.02 wanted 00 1 -> 0.98 wanted 11 0 -> 0.97 wanted 11 1 -> 0.02 wanted 0

Nobody told it to make one hidden neuron an OR and the other a NAND. It found weights that work by itself. Look at round 500, too: for hundreds of rounds the error hardly moved, as if the model were crossing a flat plain in the fog, and then it found the way down. Training often goes like that.

Nudging each weight in turn works, but it is slow: for a network with 6,634 parameters, every step would need 6,635 forward passes over all the examples. Real networks find every slope in one go, with a method called backpropagation: after a forward pass, it works backwards from the error at the output, layer by layer, sharing out the blame to each weight. It gives the same slopes as nudging (exactly, where nudging only comes close), far faster. How it does that is calculus you may meet after school; what it does is exactly what you have just seen.

Epochs and batches

Real training goes through its examples many times. One pass through every training example is called an epoch. Training usually takes many epochs, and the examples are shuffled into a new order each time, so the network doesn’t learn anything from their order.

It is also wasteful to change the weights after every single example, and slow to wait for all of them. So the examples are taken a few at a time, in a batch (the network in this unit uses batches of 10): the slopes for the batch are averaged, and the weights take one step. A training run is then: random weights; for each epoch, shuffle the examples; for each batch, a forward pass, backpropagation, and a small step downhill.

What does gradient descent do at each step?

Pick an answer to see why.

Training a network, a student sees the error jump up and down, getting bigger. What is the most likely fix?

Pick an answer to see why.

Training a network to read digits

Now the real thing. To learn digits, a network needs labeled examples: pictures of handwritten digits, each with the digit it shows. This page has two sets of them, both shared freely by the people who made them:

0–9 ०–९
Comes from MNIST, a famous set published in 1998 by Yann LeCun, Corinna Cortes and Christopher Burges in the United States the Devanagari Handwritten Character Dataset, by Shailesh Acharya, Ashok Kumar Pant and Prashnna Kumar Gyawali, presented at a conference in Kathmandu in 2015
Whose handwriting employees of the United States Census Bureau and American high school students, about 250 people for the training set; the test set was written by others cut from handwritten documents; the set’s description doesn’t say who wrote them
Size of the full set 70,000 digits 92,000 characters: 36 letters and 10 digits, 2,000 of each
License CC BY-SA 3.0, as stated in the Keras documentation CC BY 4.0, as stated by the UCI Machine Learning Repository
On this page 2,500 of them: 200 of each digit to train on, 50 to test the same

To keep what your phone downloads small (70 to 85 KB for each set), every digit was shrunk to 14 × 14 pixels on our own computer before this page was made, and the page loads them only when you scroll near the widgets. Both licenses let anyone share and change the data as long as they credit its makers (the credit is under each widget); CC BY-SA also asks that changed copies be shared under the same license, so our shrunk MNIST digits are CC BY-SA 3.0 too. The network itself is trained here, in your browser: nothing is sent anywhere.

Before shrinking, each digit was tidied the same way: cut down to the box around its ink, scaled so that its longer side is 10 pixels, and placed in the 14 × 14 square with the center of its ink in the middle. So every training example is about the same size, in the same place.

Try it

Train a digit reader

The row at the top shows one training example of each digit. Press Train. The network starts from random weights and trains for 15 epochs; after each epoch, the chart shows how many digits it gets right. The dashed line is the training examples, the ones it learns from. The solid line is the test examples, 500 digits written by other people that it never trains on. Then try the Devanagari digits.

0%25%50%75%100%051015epochs →training examplestest examples
Digits:
Epoch
0 of 15
Training accuracy
–
Test accuracy
–

The digits didn’t load. Check the internet connection and reload the page.

Loading 2,500 handwritten digits…

The network starts with random weights, so at first it guesses (about 10% right). Press Train.

Training: each epoch, every training example once, every weight nudged downhill.

Trained. Test accuracy is the honest score: these 500 digits were written by other people, and the network never trained on them.

Choose how many examples it learns from, then press Train.

Only 100 examples: it gets every one of them right, but only about three test digits in four. It has memorized its examples, not learned digits.

500 examples: the gap between training and test accuracy is smaller.

2,000 examples: training accuracy is still close to 100%, but test accuracy is far higher than with 100. More data is the surest cure for overfitting.

What you should see

Digits 0–9: from MNIST, by Yann LeCun, Corinna Cortes and Christopher J. C. Burges (© Yann LeCun and Corinna Cortes), CC BY-SA 3.0; shrunk to 14 × 14 by Science Playground and shared under the same license

Notice the gap between the two lines. The network is always better at the examples it trained on than at new ones. That is why the score that counts is the test accuracy, measured on examples the network never learned from, exactly as you did with nearest neighbors in Grade 9 Grade 9: Training, testing and accuracy.

Read your own handwriting

The network trained above can now read digits it has never seen, including yours. The drawing is tidied just as the training digits were, and the network is given the 14 × 14 result.

Try it

Read my handwriting

Draw one digit, big, in the dashed square. The picture beside it is what the network is given; the bars are its chance for each digit. Try every digit, in both scripts. Then turn Tidy the drawing off and draw a small digit in a corner.

You draw
It is given
14 × 14
0123456789
Digits:
It reads
–
Its test accuracy
–

The digits didn’t load, so there is no network to read with. Check the internet connection and reload.

No network trained above yet, so one is being trained now, with the same settings…

Draw one digit, big, in the dashed square, with a finger or the mouse.

Reading…

The tallest bar is its answer. It is very sure, but sure is not the same as right: check it.

Its answer is the tallest bar, but other digits get a fair chance too: your drawing is like more than one kind it has seen.

No digit gets a big chance: this drawing is unlike anything it trained on.

Tidy is off: the picture keeps your digit where you drew it and at its size. Draw a small digit in a corner and see what happens.

What you should see

Digits 0–9: from MNIST, by Yann LeCun, Corinna Cortes and Christopher J. C. Burges (© Yann LeCun and Corinna Cortes), CC BY-SA 3.0; shrunk to 14 × 14 by Science Playground and shared under the same license

Overfitting: learning the examples, not the idea

A student who memorizes last year’s question paper word for word may score full marks on that paper, and fail this year’s, where the questions are worded differently. A network can do the same: learn its training examples so exactly that it gets every one right, without learning what makes a 7 a 7. This is called overfitting: the network fits its training examples too closely to do well on new ones. Its sign is a big gap between training accuracy and test accuracy.

Try it

Too few examples

The same network, trained for 20 epochs, on 100, 500 or all 2,000 training examples of 0–9. Train it on each, and compare the two lines and the table.

0%25%50%75%100%05101520epochs →training examplestest examples
Examples to learn from:
ExamplesTraining accuracyTest accuracy
100––
500––
2,000––

The digits didn’t load. Check the internet connection and reload the page.

Loading 2,500 handwritten digits…

The network starts with random weights, so at first it guesses (about 10% right). Press Train.

Training: each epoch, every training example once, every weight nudged downhill.

Trained. Test accuracy is the honest score: these 500 digits were written by other people, and the network never trained on them.

Choose how many examples it learns from, then press Train.

Only 100 examples: it gets every one of them right, but only about three test digits in four. It has memorized its examples, not learned digits.

500 examples: the gap between training and test accuracy is smaller.

2,000 examples: training accuracy is still close to 100%, but test accuracy is far higher than with 100. More data is the surest cure for overfitting.

What you should see

Digits 0–9: from MNIST, by Yann LeCun, Corinna Cortes and Christopher J. C. Burges (© Yann LeCun and Corinna Cortes), CC BY-SA 3.0; shrunk to 14 × 14 by Science Playground and shared under the same license

Three ways to fight overfitting:

  • More data. The surest cure, as the widget shows. With many varied examples, memorizing is no longer the easy way to a small error.
  • A smaller network. Fewer parameters can memorize less, so they are pushed to learn the general pattern. Too small, though, and the network can’t learn the pattern either.
  • Stop early. Watch the accuracy on examples the network doesn’t train on, epoch by epoch. When it stops rising, stop training: from then on, the extra epochs mostly memorize. In the widget with 100 examples, from about epoch 12 on, training accuracy climbs from about 93% to 100% while test accuracy gains only about 3 points.

A network gets 99% of its training examples right but only 60% of its test examples. What is happening, and what would help most?

Pick an answer to see why.

What did it learn?

A network is just numbers, but some of them can be looked at. The easiest to look at belong to the simplest network: one with no hidden layer at all, where each of the ten output neurons looks straight at the 196 pixels. Each output neuron has one weight per pixel, so its weights can be drawn as a 14 × 14 picture of their own: blue where ink makes the neuron say “this is my digit”, red where ink makes it say “not mine”.

Try it

What each neuron looks for

The weights of a network with no hidden layer, trained on the 2,000 training digits for 5 epochs with a small learning rate. One picture per output neuron. Compare 0–9 with ०–९.

ink here: more likely this digit ink here: less likely

Digits:
Test accuracy
–
Test digits wrong
–

The digits didn’t load. Check the internet connection and reload the page.

Working…

Each picture is one neuron’s weights. The 0 neuron wants ink in a ring (blue) and none in the middle (red); the 1 neuron wants a stroke down the middle and nothing either side of it.

Each picture is one neuron’s weights. Look for the shape of each digit in blue, and the red places where ink would mean a different digit.

The network’s mistakes. Many are digits a person would also stop at; some look plain to you, and show it learned where ink usually is, not what a digit is.

Nearest neighbors, on the same examples: it gives each test digit the label of the most similar training picture. On this small set it makes fewer mistakes than the network.

What you should see

Digits 0–9: from MNIST, by Yann LeCun, Corinna Cortes and Christopher J. C. Burges (© Yann LeCun and Corinna Cortes), CC BY-SA 3.0; shrunk to 14 × 14 by Science Playground and shared under the same license

So what has it learned? Something like a stencil for each digit: where ink usually is, and where it usually isn’t. That works surprisingly well when digits are written in the usual way, at the usual size, in the middle. It is not what you do when you read. You know a 4 has a closed or open corner and a line down the side, wherever it is and however big. The network has no idea of a line, a corner or a loop. It knows pixels.

A network with a hidden layer is harder to read: each hidden neuron looks for some pattern of pixels, and the outputs combine them, so its parts don’t have simple meanings. Finding out what large networks have learned is a whole field of research, called interpretability.

Where it fails

A model’s mistakes show what it really learned. Here are the test digits the network gets wrong.

Try it

Its mistakes

The test digits the network gets wrong (up to 30, spread over the digits), with what was written and what it read. It uses the network you trained above, or the same one trained now if you didn’t. Then switch to Nearest neighbor to compare.

Model:
Digits:

written → read as

Test accuracy
–
Test digits wrong
–

The digits didn’t load. Check the internet connection and reload the page.

Working…

Each picture is one neuron’s weights. The 0 neuron wants ink in a ring (blue) and none in the middle (red); the 1 neuron wants a stroke down the middle and nothing either side of it.

Each picture is one neuron’s weights. Look for the shape of each digit in blue, and the red places where ink would mean a different digit.

The network’s mistakes. Many are digits a person would also stop at; some look plain to you, and show it learned where ink usually is, not what a digit is.

Nearest neighbors, on the same examples: it gives each test digit the label of the most similar training picture. On this small set it makes fewer mistakes than the network.

What you should see

Digits 0–9: from MNIST, by Yann LeCun, Corinna Cortes and Christopher J. C. Burges (© Yann LeCun and Corinna Cortes), CC BY-SA 3.0; shrunk to 14 × 14 by Science Playground and shared under the same license

Your own tests in Read my handwriting showed more failures, of three kinds:

  • Things it never saw. Drawn small in a corner, with Tidy off, a digit lands on pixels where the training digits had no ink, so the weights there were never trained, and the answer is nonsense.
  • Styles it never saw. A digit written in a way that wasn’t in its examples, like a 7 with a bar across it if few of its examples had one.
  • Confidently wrong. The network always gives an answer, and often gives a wrong one with a high chance. A chance of 98% means “this looks very like the 7s I trained on”, not “I am 98% sure, and I am right 98% of the time”.

Network or nearest neighbors?

On the same 2,000 training digits, the nearest-neighbor model of Grade 9 (with k = 1: each test digit gets the label of the single most similar training picture, measured by the distance between their 196 pixel values) did better than our network. The numbers, from the widgets on this page:

Nearest neighbor Neural network (32 hidden neurons)
Test accuracy, 0–9 91.8% 89.2%
Test accuracy, ०–९ 97.2% 93.0%
Training none: it keeps the examples 15 epochs of gradient descent
What it keeps all 2,000 pictures: 392,000 numbers 6,634 parameters
Work to read one digit compare with all 2,000 pictures: 392,000 subtractions about 6,600 multiplications
Can it say why? yes: “it looks most like this training digit” not easily

So which is better? It depends on the task, and that is how to choose a kind of model: by what the task needs, not by what is newest.

  • On a small set of small, tidy pictures, nearest neighbors is accurate, needs no training, and can show the training picture it matched. Here it wins.
  • As the data grows, nearest neighbors grows with it: every new example is one more picture to keep and to compare with every time it answers. A network with a million training examples is no bigger, and no slower to answer, than one with a thousand. A phone that reads digits must answer quickly, without keeping every example ever written.
  • With much more data, networks win on accuracy too. On the full MNIST set of 60,000 training digits, nearest neighbors gets about 97% of the test digits right and a well-trained network with one bigger hidden layer, like ours but with hundreds of hidden neurons, about 98%, and networks designed for pictures (called convolutional networks, which look for the same small shapes at every place in the picture) make fewer than 1 mistake in 100.

Judging the training data

A network can only learn what is in its examples. So before trusting a model, judge the data it was trained on. Five questions to ask of any training data:

  1. Source. Where did it come from, who collected it, and why?
  2. Quality. Are the labels right? Are there blurred, cut-off or mislabeled examples?
  3. Representativeness. Is it like the data the model will meet in use? Does it cover all the kinds of people, places and situations it will meet?
  4. Bias. Is any group left out or under-represented, so that the model works worse for them? (Grade 7 called this one-sided examples: One-sided examples: bias.)
  5. Privacy. Whose data is it? Did they agree to it being used this way? Could it identify them?

Ask them of the 0–9 digits. The source is clear and published. Its quality is good, but not perfect: researchers have found a few digits in MNIST that even people can’t read, and some that look mislabeled. Its representativeness is the big question for us. The digits were written in the United States, by about 250 people for the training part: Census Bureau employees and high school students. Handwriting differs from place to place: in much of Europe a 7 is often written with a bar across it and a 1 with a long flag, while most Americans write both plainly. How does your class write them? If differently from the training digits, a network trained only on them may read your handwriting worse than an American student’s: a bias that comes from where the examples came from, not from anything wrong in the code. And privacy: handwriting is personal. Those people filled in forms for a research collection around 1990; they could not have imagined their digits being used by millions of students, researchers and companies thirty years later.

A company in Kathmandu wants to read handwritten amounts on forms filled in all over Nepal, in Devanagari digits. It trains a network only on digits written by its own office staff in Kathmandu. Which problem is it most likely to have?

Pick an answer to see why.

Make a model of your own

You have trained a network on data someone else collected. The real work of building a model starts earlier: choosing the task and collecting the examples. Try the whole process on the page.

Try it

Teach it your own three

Choose three things to tell apart: three Devanagari letters (the boxes start with क, ख and ग), three symbols, three shapes. Draw an example in the square, then press Add beside its name. Add at least 3 of each (5 or more is better), different each time. Press Train, then draw new ones and see what it says.

कखग
0
0
0
Examples
0 + 0 + 0 = 0
It reads
–

Choose three things to tell apart, and name them in the boxes. Draw one in the square, then press its Add button.

Keep going: at least 3 examples of each, and more is better. Vary them: big and small, neat and quick.

Enough to start. Press Train, or add more examples first.

Training…

You have added examples since training. Train again so the model learns from them too.

Trained. Now draw new examples, ones it hasn’t seen, and watch its chances. Better still, ask a friend to draw.

Its answer is the tallest bar. Is it right? Count how many new drawings out of ten it gets right: that is its test accuracy.

It isn’t sure: this drawing is unlike its examples. Add drawings like it, to the right class, and train again.

What you should see

The steps you just followed are the steps of building any model:

  1. Choose the task, and check that learning from examples is the right tool: is there a rule you could write instead?
  2. Collect examples that are like what the model will meet: many people, many styles, all the classes in fair numbers. Label them carefully.
  3. Train a model, and keep some examples aside, never trained on.
  4. Test it on those, and on new people, and look at the mistakes, not only the accuracy.
  5. Improve the data where it fails (Grade 7’s Fixing a model by fixing its data), and train again.
A woman with shoulder-length dark hair speaking at a lectern with two microphones
Figure 8.5 Fei-Fei Li speaking in 2017. ImageNet, the set of labeled photos her team built, showed how much a network can learn when it is given enough examples. Photo: ITU Pictures, CC BY 2.0

Project: a digit reader for your class

In short

Exercise

What is the output of a step neuron with inputs 1 and 1, weights 0.5 and −2, and bias 1?

Pick an answer to see why.

Which weights and bias make a two-input step neuron act as an OR gate?

Pick an answer to see why.

What is a hidden layer?

Pick an answer to see why.

In gradient descent, the slope of the error for a weight is +30 and the learning rate is 0.1. What happens to the weight?

Pick an answer to see why.

What is an epoch?

Pick an answer to see why.

Which of these is the clearest sign of overfitting?

Pick an answer to see why.

Why is the nearest-neighbor model slower to answer than a trained network when it has a million training examples?

Pick an answer to see why.

a.

Explain these words, each in a sentence or two: weight, bias, activation function, hidden layer, forward pass, learning rate, epoch.

Answer
b.

A shop’s neuron decides whether to give a customer a discount. Inputs: a regular customer (1 or 0), spending over Rs 2,000 (1 or 0), paying by phone (1 or 0). Weights 2, 3 and 1; bias −3.5; step activation. Make a table of the output for all eight combinations of inputs. In words, what rule has the neuron learned?

Answer
c.

Explain, with a drawing of the square of inputs, why no single neuron can act as an XOR gate, and show how a network of three neurons can.

Answer
d.

Work out the forward pass of the XOR network in Figure 8.3 for the input (1, 0): the outputs of h₁, h₂ and the output neuron, showing each weighted sum.

Answer
e.

A network reads 28 × 28 pixel pictures, has one hidden layer of 64 neurons and 10 outputs. How many parameters (weights and biases) does it have? How many would it have with 128 hidden neurons? Which is more likely to overfit on 500 training examples, and why?

Answer
f.

In the momo example, the model starts at w = Rs 100 and the slope there is −805. With a learning rate of 0.05, what is w after one step? Why does the step get smaller as w nears the best price, even though the learning rate stays the same?

Answer
g.

Explain gradient descent to a friend who has never studied calculus, using the picture of walking down a hill in fog. In your explanation, say what the hill, your position, the slope and the size of your step stand for.

Answer
h.

A student trains a network on 100 handwritten digits for 50 epochs and reports “my model is 100% accurate”. Give two things wrong with this claim and say how to measure the model properly.

Answer
i.

The digit network’s ten output chances for a drawing are 0.97 for 7 and small numbers for the rest, but the drawing was a 1. Explain how the network can be so sure and still wrong. What does this mean for using such networks where mistakes matter?

Answer
j.

Evaluate this training data for a network that will read the handwritten marks teachers write on SEE answer sheets all over Nepal: 5,000 digits written by 20 teachers at one school in Lalitpur, collected by asking them to fill in practice sheets. Use the five questions of “Judging the training data”, and suggest how to make the data better.

Answer
k.

For each task, choose a set of rules written by people, a nearest-neighbor model, or a neural network, and justify your choice: (i) reading the handwritten amount on a bank deposit slip; (ii) working out the late fee on a library book from the number of days late, at Rs 5 a day; (iii) guessing which of five kinds of tea leaf a sample is, from four measurements, with 200 labeled samples, where the tea board wants to see why each guess was made.

Answer
l.

Our digit network had 89.2% test accuracy and the nearest-neighbor model 91.8%, on the same examples. A friend says: “So neural networks are worse than nearest neighbors.” Do you agree? Explain, using what changes when there are many more training examples.

Answer