Chapter 2 · Neural networksPuff 9 of 23
Watch a small network learn
Everything from this chapter, together: neurons, layers that bend, blame passed backwards, knobs nudged downhill. Below is a real neural network, training right here in your browser. Its job: look at where a dot sits and say whether it’s blue or orange.
Guess first
A network with only 2 hidden neurons tries to separate a blob of blue dots from the ring of orange dots around it. What happens?
Play with it
The network starts with random knobs and learns as soon as it’s on screen. Pick a dataset, change the number of neurons, and watch the colours re-form.
How it works
Tap a step to see it in the figure.
Check yourself
With 3 neurons the spiral won’t separate, even after thousands of steps. What is most likely to help?
Go deeper: the mathThe network, its loss, mini-batches, and counting knobs
The network
Each dot’s position (x, y), with both between −1 and 1, goes into a hidden layer of n neurons. Each one computes a weighted sum plus a bias and squashes it with tanh:
hi = tanh(aix + biy + ci)
tanh(z) = ez − e−zez + e−z
tanh is an S-curve like the sigmoid of puff 6, but it runs from −1 to 1 and is centred on zero, which tends to make learning smoother. With the second layer on, another n tanh neurons take the first layer’s outputs as their inputs. Finally one output neuron adds everything up and squashes it with the sigmoid:
p = σ(v1h1 + … + vnhn + d)
p is the network’s probability that the dot is blue. The shading shows p for every spot.
The loss and the nudges
The loss is the cross-entropy from puff 6, −log of the probability given to the right colour, averaged over the dots. Each step uses mini-batch gradient descent: pick 20 dots at random, work out every knob’s slope by backpropagation (puff 8), average the slopes over the 20, and move each knob by η × slope. A small random batch makes each step cheap and a little noisy, which usually doesn’t matter. Right now: not started.
Counting the knobs
Each hidden neuron has 2 weights and a bias, and the output neuron has one weight per hidden neuron plus its own bias: 4n + 1 knobs. A second layer adds n × n weights and n biases. This network has 9.
The same recipe, with many refinements, trains chatbots. GPT-3, for example, had 175 billion knobs.
What this page simplifies
The knobs start at small random values (each weight scaled by 1/√inputs, biases at zero), so two runs can end differently: press Reset to roll new ones. Real training uses cleverer update rules than a plain nudge, far more data, and checks the network on dots it never trained on (puff 5).
Sources
- D. Smilkov and S. Carter, TensorFlow Playground, the inspiration for this figure.
- I. Goodfellow, Y. Bengio and A. Courville, Deep Learning, MIT Press, 2016, chapters 6 and 8.
- M. Nielsen, Neural Networks and Deep Learning, Determination Press, 2015.
- T. Brown and others, “Language models are few-shot learners”, 2020 (GPT-3’s 175 billion parameters).
- The four datasets are generated by this page from made-up shapes.