Lesson 5 of 13

Networks of neurons

One neuron can only draw a straight line. Connect a few and they can carve out curves. Train a tiny neural network and watch it happen.

Novice18 min

Builds on The artificial neuron

In this lesson you will

  • Explain why a single neuron cannot separate data arranged in a ring
  • Describe how a hidden layer combines straight lines into curved boundaries
  • Watch training adjust every weight in a network at once
  • Explain how backpropagation finds the nudge for every weight in one backward pass

A single neuron draws a straight line and says “this side yes, that side no”. That was enough for deciding on a bike ride. Now picture data where the yes examples sit in a cluster in the middle and the no examples surround them in a ring. No straight line can separate a center from the ring around it. A single neuron is stuck.

The fix is the idea behind every Neural networkA model built from layers of artificial neurons, where each layer's outputs become the next layer's inputs. Training adjusts all of the weights together.Open in glossary: use several neurons, and feed their outputs into another neuron.

Lines, combined

In the network below, the two inputs (how far across and how far up a point is) go into a row of neurons called a Hidden layerA layer of neurons between a network's inputs and its output. Its values are not given or read directly; the network works them out as intermediate steps.Open in glossary. Each hidden neuron draws its own straight line, shown dashed. Their outputs go into one final neuron, which combines them into the network’s answer. The shading shows that answer across the whole plane.

A tiny neural network

Two inputs, a layer of hidden neurons, and an output. Train it and watch the shading: one neuron can only draw a straight line, but several together can bend.

Hidden neurons
Data
Training steps0
Lossn/a
Points classified correctly0%
Numbers the network learns7

Challenges

0 of 3 complete

  1. Train 1 hidden neuron on Circle until it stops at 2,500 steps. How high does it get? (not yet)
  2. Get 95% or more of the Circle points right with as few hidden neurons as possible. (not yet)
  3. Solve XOR: get 95% or more right. Try 2 hidden neurons first. (not yet)

Try this

  • Start with 1 hidden neuron on Circle and press Train. The single dashed line wanders around but can never surround the middle. Accuracy stalls near 68%.
  • Switch to 3 hidden neurons and train again. Watch the three lines arrange themselves into a triangle around the center, and the shading bend to match.
  • Try 8. The boundary gets rounder, because more lines can approximate a curve more closely.
  • Load XOR, where opposite corners belong together, and compare 2 hidden neurons with 3.
Optional: the same idea in codePython with NumPy
import numpy as np

# Blue points (label 0) in a small circle in the middle,
# orange points (label 1) in a ring around them.
angle = np.linspace(0, 2 * np.pi, 12, endpoint=False)
circle = np.column_stack([np.cos(angle), np.sin(angle)])
X = np.vstack([0.3 * circle, 1.0 * circle])  # each row: across, up
y = np.array([0] * 12 + [1] * 12)

def train(hidden, steps=3000, learning_rate=1.0):
    rng = np.random.default_rng(1)
    # Each hidden neuron has 2 weights (one per input) and a bias.
    W1, b1 = rng.normal(size=(2, hidden)), np.zeros(hidden)
    # The output neuron has one weight per hidden neuron, and a bias.
    w2, b2 = rng.normal(size=hidden), 0.0
    for _ in range(steps):
        h = np.tanh(X @ W1 + b1)              # each hidden neuron's answer
        p = 1 / (1 + np.exp(-(h @ w2 + b2)))  # chance of orange, 0 to 1
        # Backpropagation: start from the error at the output (d_out), pass
        # it back through the output weights to get each hidden neuron's
        # share (d_hidden), then nudge every weight against its blame.
        # The demo uses a smarter nudging rule called Adam; the idea is the same.
        d_out = (p - y) / len(y)
        d_hidden = np.outer(d_out, w2) * (1 - h**2)
        w2 -= learning_rate * (h.T @ d_out)
        b2 -= learning_rate * d_out.sum()
        W1 -= learning_rate * (X.T @ d_hidden)
        b1 -= learning_rate * d_hidden.sum(axis=0)
    return np.mean((p > 0.5) == y)

for hidden in (1, 2, 3):
    print(f"Hidden neurons: {hidden}, points right: {train(hidden):.0%}")
Hidden neurons: 1, points right: 71%
Hidden neurons: 2, points right: 92%
Hidden neurons: 3, points right: 100%

Each hidden neuron answers one simple question: which side of my line is this point on? On its own that is not much. But the output neuron can combine the answers: “inside all three lines” picks out a triangle. With more hidden neurons, the combined region can take almost any shape.

That is the core trick of neural networks. Simple pieces, each drawing a straight line, combine into something that can follow curves. Stack more layers, and the second layer can combine the first layer’s shapes into even more complex ones. A network with many layers is called deep, which is where the name Deep learningMachine learning with neural networks that have many layers. It powers modern image recognition, speech recognition, translation, and chatbots.Taught in What is AI, really?Open in glossary comes from.

Training sets every weight at once

Nobody told the network where to put its lines. It started with random weights, so its first guesses were poor. Each training step measured the loss over all the points and nudged every weight and bias in the direction that lowered it, all at the same time. Over a few hundred steps, the lines drift into useful positions.

Which way to nudge each weight

To step downhill, as in Getting less wrong, training needs to know, for every weight, whether raising it a little would raise the loss or lower it, and by how much. How does it find out?

There is a slow way that works. Nudge one weight up by a tiny amount, run the network again on the same points, and see whether the loss went up or down. The trouble is the count: one extra run of the network for every weight. This network has 17 numbers to learn, so 17 extra runs per step is fine. A large language model has billions, so every single step would need billions of runs.

BackpropagationHow a network works out which way to nudge every weight: one backward pass carries the error from the output back through the layers, giving each weight its share of the blame (its gradient, found with the chain rule).Taught in BackpropagationOpen in glossary gets all of these answers with far less work. Running the network from input to output to get its answer is called the forward pass. Backpropagation adds one backward pass. It starts from the error at the output and passes it back through the network, layer by layer, toward the input. On the way, every weight gets its share of the blame for the error.

That share is a number: how much the loss would change if that weight changed a little. Its sign says which way to nudge the weight, and its size says how strongly.

Why does the blame have to travel backward? A weight in the hidden layer never touches the loss directly. It changes its hidden neuron’s answer, which changes the output, which changes the loss. So its share is a product of those links: how much the weight moves its neuron, times how much that neuron moves the output, times how much the output moves the loss. Multiplying effects along a path like this is the Chain ruleThe calculus rule for nested functions: if L depends on a and a depends on z, then dL/dz = dL/da times da/dz. Backpropagation applies it over and over.Taught in BackpropagationOpen in glossary from calculus. (When a weight reaches the loss along more than one path, the products for each path are added.) The links near the output are shared by every weight behind them, so the backward pass works each one out once and reuses it as the blame flows back.

Blame flowing back through a tiny network to one hidden weightForward pass, left to right: the input, 2, goes to two hidden neurons through weights 0.5 and −0.25, giving 1 and −0.5. They reach the output through weights 3 and 2, giving 2. Against a target of 1 the loss is 1. The weight from the input to the top hidden neuron, w, is drawn thick. Backward pass, right to left: a dashed blame path runs from the loss, past the output and the top hidden neuron, to w, multiplying by 2, then 3, then 2 on the way, so the blame for w is 12.Forward pass: compute the answer and the lossw = 0.5−0.253221−0.52loss 1inputhidden layeroutputtarget 1×2×3×2Backward pass: pass the blame back
  1. ×2Loss from output. The output is 2 and the target is 1. Raise the output a little and the loss rises about 2 times as much.
  2. ×3Output from hidden neuron. The top hidden neuron's answer is multiplied by 3 on its way to the output.
  3. ×2Hidden neuron from w. w multiplies the input, 2, so raising w raises the neuron's answer 2 times as much.
  4. =12Blame for w: 2 × 3 × 2 = 12. Raise w by 0.001 and the loss rises by about 0.012, so training nudges w down.
The forward pass runs left to right, from the input to the loss. The backward pass starts at the loss and runs right to left, multiplying by one local effect per link. The ×2 at the loss is shared by all four weights, so it is worked out once and reused. Activations are left out to keep the arithmetic simple; with them, each neuron adds one more factor, the slope of its activation.

The saving is large. The backward pass costs about twice the arithmetic of the forward pass, so a whole training step is about three forward passes of work, whether the network has 17 numbers to learn or billions. In their 2020 study of language model training, Kaplan and colleagues count about 2 arithmetic operations per weight for each token (a word or part of a word) the model reads going forward, and about 6 for training, forward and backward together. The nudge-one-weight method is still useful for checking that backpropagation’s answers are right.

Backpropagation became widely used after a 1986 paper by David Rumelhart, Geoffrey Hinton, and Ronald Williams showed that networks trained this way learn useful features in their hidden layers. The method is older: Seppo Linnainmaa described this backward way of computing effects in 1970, and Paul Werbos discussed using it for neural networks in 1974. Nearly every neural network today is trained with it, from this one to the largest language models, and the neural networks track walks through it step by step.

One thing you may have noticed with XOR: two hidden neurons are enough to solve it in principle, yet with two this network gets stuck around 78%. With three or more, it solves the problem. Training is a search, and it can get stuck. Giving a network more neurons than it strictly needs often makes the search easier, and that turns out to be true of very large networks too.

Counting the numbers a network learnsOptional

Each hidden neuron has two weights (one per input) and a bias: 3 numbers. The output side of this demo has 2 output numbers, one per class, each with a weight from every hidden neuron plus a bias; a softmax turns them into probabilities. (That is mathematically the same as one sigmoid output neuron, just written the way larger networks do it.)

With HH hidden neurons that makes 3H+2H+2=5H+23H + 2H + 2 = 5H + 2 numbers: 7 for one hidden neuron, 17 for three, 42 for eight. The hidden neurons use the tanh activation, an S-shaped curve like the sigmoid but ranging from −1-1 to 11.

For comparison, a modern large language model learns billions of numbers, but each one is adjusted by the same kind of step you just watched.

Key ideas

  • A single neuron’s boundary is a straight line, so it cannot separate a center from the ring around it.
  • A hidden layer gives several neurons, each drawing its own line; the next neuron combines them into curves and enclosed regions.
  • Stacking many layers is what makes a network “deep”.
  • Training nudges every weight at once. Backpropagation finds every nudge with one backward pass that gives each weight its share of the blame, about three forward passes of work per step instead of one extra run per weight.
  • Training is a search that can get stuck; extra neurons often make it easier to find a good solution.

Check yourself

Pick an answer to see why it is right or wrong. Nothing is graded. Your first answer is saved in this browser so the question can come back for review.

1Why does one hidden neuron get stuck at about 68% on the circle data?
2What does a hidden layer add?
3The network with three hidden neurons has 17 numbers to learn. How does training set them?
4Why does training use backpropagation instead of nudging each weight a little and rerunning the network to see what happens?

Progress is saved in this browser only.

Up nextHow a chatbot writes
Next
AI from zero
  1. 1What is AI, really?
  2. 2Turning things into numbers
  3. 3Getting less wrong
  4. 4The artificial neuron
  5. 5Networks of neurons
  6. 6How a chatbot writes
  7. 7What AI gets wrong
  8. 8AI that takes actions
  9. 9Where your AI runs
  10. 10Safety in AI products
  11. 11Open and closed models
  12. 12How AI sees, hears, and makes pictures
  13. 13When someone attacks an AI app

Try "embedding", "softmax", "overfitting", or "backpropagation".