Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Neural networks / How neural networks learn

Neural network layers: more inputs, more neurons

Why neural networks need non-linear activation functions such as ReLU. Can extra neurons learn a curve that one neuron cannot?

Builds on A neuron that learns.

Last time, one neuron learned to turn cubits into metres.

The cubit neuron from the last articleThe number of cubits goes in, is multiplied by the weight w, and the neuron says the length in metres.cubits4×0.53 × 4metres2.12w = 0.53
Last article’s neuron: cubits in, multiply by the weight, metres out. Hover any part for an explanation.

It worked well. But some problems are harder.

A conversion that does not start at zero

An archaeologist has found a weather report from the United States, full of temperatures in a baffling, archaic unit: degrees Fahrenheit. Nobody is sure how it relates to Celsius. To crack it, the archaeologist matches eight of the tablet’s readings with Celsius ones.

Here they are, and the same neuron from before, ready to learn the conversion. Each Next step sends one reading forward through the neuron and the error back again, then updates the weights. Or press Train.

Train the neuron to convert °C to °F

Interactive

Misses by
Updates

It cannot. The neuron only multiplies, so when the input is 0 its answer is 0. But 0 °C is 32 °F. Its line is stuck at the bottom left corner.

A second input: the bias

The fix is to give the neuron a second input that is always 1. Its weight, the bias bb, is added to every answer.

The neuron with a bias, converting Celsius to FahrenheitA temperature in Celsius and a constant 1 go in, each multiplied by its weight, and the neuron adds them to say the temperature in Fahrenheit.°C20constant1Σ1.8×20 + 32×1°F68w = 1.80b = 32.00
Two inputs: the temperature, and a constant 1. The weight on the 1 is the bias b: it is added to every answer. Shown: the true rule, 1.8 and 32.

Same neuron, same training loop, two weights to learn:

Train the neuron with a bias

Interactive

Misses by
Updates

It finds the rule: about 1.8 times the Celsius temperature, plus 32.

A harder question

Now ten years of Norwich weather. Each dot is the average temperature for one month of one year. It is cold in winter and warm in summer.

The input is the month, and the constant 1, so the neuron can learn a bias.

The same neuron with a second inputThe month and a constant 1 go in, each multiplied by its weight; the neuron adds them and says the temperature.month7constant1Σ0.46×7 + 7.9×1temperature11.1 °Cw = 0.46b = 7.88
The same neuron, for the weather: the month goes in, and so does the constant 1 with its own weight. Shown: the best straight line, for July.

Train it the same way.

Train one neuron on the Norwich weather

Interactive

Thicker connection = bigger weight. Blue is positive, orange is negative. Watch them change as it learns.

Misses by
Best straight line
Steps
Numbers learned

It misses by 4.4 °C, however long you train. A line goes up, or down. The weather does both.

What if we add more neurons?

Put one neuron after another, or several side by side:

Two neurons in a chainThe month feeds a first neuron, whose answer feeds a second neuron, which says the temperature.monthw₁x + b₁w₂(w₁x + b₁)temperaturew₁w₂
A chain: the first neuron’s answer feeds a second one.
A layer of four neurons side by sideThe month feeds four neurons at once, each a straight line, and an output adds up what they say.monthtemperaturew₁w₂w₃w₄
Side by side: four neurons look at the same month; the output adds them up.

Try it. Add neurons and press Train.

Add neurons to the Norwich weather

Interactive

Thicker connection = bigger weight. Blue is positive, orange is negative. Watch them change as it learns.

Misses by
Best straight line
Steps
Numbers learned

Nothing improves. Each neuron only multiplies and adds, and a sum of lines is a line:

w2(w1x+b1)=(w2w1) x+w2b1w_2 (w_1 x + b_1) = (w_2 w_1)\,x + w_2 b_1

That is one weight times xx, plus one bias. A hundred neurons that only multiply and add are still one neuron.

An activation function

Each neuron needs one more step after the adding. An activation function changes the sum before the neuron passes it on. The simplest is the ReLU: keep a positive number, and turn a negative one into zero.

A neuron with an activation functionThe month and a constant 1 go in with their weights. The neuron adds them up and then applies a ReLU activation before it answers.monthconstant1sum, then ReLUtemperaturewb
An activation after the sum: the ReLU keeps a positive sum and turns a negative one into zero.

Now a neuron is not just a line. It switches on in one part of the data and stays off in the rest. Give several neurons different weights and each one learns a different feature of the data. Their sum is the curve.

Same four neurons, now with the activation. Train:

The same neurons, with a ReLU activation

Interactive

Thicker connection = bigger weight. Blue is positive, orange is negative. Watch them change as it learns.

Misses by
Best straight line
Steps
Numbers learned

The miss falls from 4.4 °C to about 1.1 °C. What is left is real year-to-year scatter. Tick “Show what each neuron adds” to see each neuron’s contribution: one for the climb into spring, one for the fall in autumn. Try fewer neurons, and more.

Any curve at all

It is not just weather. Pick a curve or draw your own, and press Train.

Draw a curve and let the network learn it

Interactive

Thicker connection = bigger weight. Blue is positive, orange is negative. Watch them change as it learns.

Misses by
Best straight line
Steps
Numbers learned

Other activation functions

The ReLU is not the only choice. These are the popular ones. Pick one, and slide the input to see what it answers.

Compare activation functions

Interactive
Answer

Why this works

In 1989 George Cybenko proved that a single hidden layer with enough sigmoid neurons can approximate any continuous curve over a bounded range. Leshno and colleagues later showed that, under their theorem’s conditions, the result extends to non-polynomial activations such as ReLU. Minsky and Papert had shown the limits of a single-layer perceptron in Perceptrons (1969). Nair and Hinton studied rectified linear units in restricted Boltzmann machines; AlexNet helped establish ReLU as a practical choice for deep image-classification networks.

Next

A layer is a matrix multiplication followed by an activation. Nearly every layer in this series is that. Next, the input is a picture of 784 numbers, and the weights pile up. That is the problem convolutions solve.


Sources: Minsky and Papert, “Perceptrons” (1969); Cybenko, “Approximation by Superpositions of a Sigmoidal Function” (1989); Leshno et al., “Multilayer Feedforward Networks With a Nonpolynomial Activation Function Can Approximate Any Function” (1993); Nair and Hinton (2010); Krizhevsky, Sutskever and Hinton, “ImageNet Classification with Deep Convolutional Neural Networks” (2012); Kingma and Ba, “Adam” (2014). Temperature data: Open-Meteo, ERA5 reanalysis, Norwich, 2015 to 2024. The best straight line misses by 4.37 °C; with the ReLU, 1 neuron 4.37, 2 neurons 1.37, 4 neurons 1.14, 8 neurons 1.10. The Fahrenheit readings are illustrative, with the true rule 1.8 × °C + 32.

← Browse all subjectsBack to the beginning ↑