Rúben GalvãoThe Rabbit Hole

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

A neuron that learns

Teach one neuron from its mistakes, then discover gradient descent and backpropagation.

Show a child a few dogs and cats and soon they can tell them apart, with no rule given. Networks learn the same way: from examples. We will follow the smallest learner there is, one neuron with one number.

What is a neuron?

It takes numbers in, multiplies each by its own number (a weight), and adds the results up. That is all.

Anatomy of a neuronTop: three inputs, each multiplied by its own weight, are added together by a neuron to give one output. Bottom: the simplest neuron, with one input multiplied by one weight to give one output.a neuron in general
x1x_1
x2x_2
x3x_3
×w1\times w_1
×w2\times w_2
×w3\times w_3
∑\sum
out
w1x1+w2x2+w3x3w_1 x_1 + w_2 x_2 + w_3 x_3
the neuron in this article
xx
×w\times w
wxw x
out
What to notice: a neuron is a small calculation. Each input is multiplied by its own weight, the results are added up, and the sum comes out. The weights are the only things that can be learned. The neuron in this article is the smallest case: one input, one weight. Hover or tap any part for an explanation.

The weights are the only part that changes. A network is many neurons wired together, and learning means finding good weights.

Learn by example

To teach a neuron, you need examples: inputs, each with the answer you want.

An archaeologist opens a tomb and finds a papyrus: a list of objects and their lengths, in royal cubits. We use metres, so how long was a cubit in those?

So the archaeologist measures the same objects with a tape measure. Now there are examples: each object has a length in cubits (from the papyrus) and in metres (from the tape).

Papyrus: cubits (input)Tape measure: metres (the answer we want)
21.07
52.58
10.55
105.25
31.55
73.66
42.13

Metres are roughly half the cubits, but not exactly: scribes’ rods varied, and tape measures slip. No rule fits every row, so we want the best fit.

Let’s train a neuron

Let’s train a neuron to learn the conversion for us. It has one input and one weight ww:

A neuron that converts cubits to metres, before trainingA length in cubits goes in, is multiplied by the weight w, which starts at 1, and the neuron says a length in metres.cubits2×1 × 2metres2.00w = 1.00
Our neuron, before training: cubits in, multiply by the weight w, metres out. We start with the guess w = 1. Hover any part for an explanation.

The weight is what the neuron learns. The right ww is the conversion: multiply cubits by it and you get metres. We do not know it yet, so the neuron starts with a guess: a cubit is a metre, w=1w = 1.

How wrong is it?

For the 2-cubit object it says 2.002.00 m. The tape measure says 1.071.07 m. It is off by 0.930.93.

We square the error, so that too high and too low cannot cancel out. That is the loss: 0.932≈0.860.93^2 \approx 0.86. Now the goal is clear: make the loss small.

Which way should we nudge it?

We want the loss to get lower, so we need to know which way to push ww: up or down?

The answer is the gradient ∂L∂w\dfrac{\partial L}{\partial w}: how much the loss changes for a small nudge of ww. To find it, we look at the two small steps of the forward pass, one at a time:

  • Neuron: m=w×xm = w \times x, so ∂m∂w=x=2\dfrac{\partial m}{\partial w} = x = 2.
  • Loss: L=(m−t)2L = (m - t)^2, so ∂L∂m=2(m−t)=1.86\dfrac{\partial L}{\partial m} = 2(m - t) = 1.86.

The chain rule joins them by multiplying:

∂L∂w=∂L∂m×∂m∂w=1.86×2=3.72\frac{\partial L}{\partial w} = \frac{\partial L}{\partial m} \times \frac{\partial m}{\partial w} = 1.86 \times 2 = 3.72
One neuron, forward and backwardForward: the input x equal to 2 and the weight w equal to 1 are multiplied to give m equal to 2.00. The loss compares m with the target t equal to 1.07 and gives 0.86. Backward: the slope of the loss with respect to m is 1.86, and multiplying by the slope of m with respect to w, which is 2, gives 3.72 at the weight.
x=2x = 2
w=1w = 1
×\times
m=w⋅xm = w \cdot x
∂m∂w=x\dfrac{\partial m}{\partial w} = x
m=2.00m = 2.00
t=1.07t = 1.07
loss
L=0.86L = 0.86
L=(m−t)2L = (m - t)^2
∂L∂m=2(m−t)\dfrac{\partial L}{\partial m} = 2(m - t)
∂L∂m=1.86\dfrac{\partial L}{\partial m} = 1.86
×∂m∂w=2\times \dfrac{\partial m}{\partial w} = 2
∂L∂w=3.72\dfrac{\partial L}{\partial w} = 3.72
From equation to derivative
StepForward equationLocal derivativeValue here
The neuronm=w×xm = w \times x∂m∂w=x\dfrac{\partial m}{\partial w} = x22
The lossL=(m−t)2L = (m - t)^2∂L∂m=2(m−t)\dfrac{\partial L}{\partial m} = 2(m - t)2×0.93=1.862 \times 0.93 = 1.86
Chain rule∂L∂w=∂L∂m×∂m∂w\dfrac{\partial L}{\partial w} = \dfrac{\partial L}{\partial m} \times \dfrac{\partial m}{\partial w}1.86×2=3.721.86 \times 2 = \mathbf{3.72}
What to notice: the forward pass (blue) is two small equations, and the orange equation under each one is its local derivative. Walking back along the orange path, the chain rule multiplies those local derivatives together, and what arrives at w is the gradient we wanted. Squares are data, circles are things that compute. Hover or tap any part for an explanation.

Positive means: raising ww makes things worse, so lower it. (Sending the error backwards like this is called backpropagation.)

Learning by small steps

Roll the ball downhill:

w←w−η ∂L∂ww \leftarrow w - \eta \, \frac{\partial L}{\partial w}

η\eta is the learning rate, the step size. Press Next step and follow one object: forward through the neuron, the loss, then the derivative travelling back. The weight is nudged, and the next object goes through.

Watch a neuron train, one example at a time

Interactive
A neuron training on one row of dataThe input x and weight w flow into a multiplying neuron to give an answer m. The loss compares m with the target t. The derivative of the loss then flows backwards along the orange path to the weight, and the weight is nudged.2
xx
w =1.000
×\times
mm
1.07
tt
loss
LL
∂L∂m\dfrac{\partial L}{\partial m}
∂L∂w\dfrac{\partial L}{\partial w}
×x\times x

Show the calculation, line by lineHide the calculation
Cubits xMetres tw after

Try it yourself: hover over the weight on the connection in the diagram (or tap it) for a slider, and pick a different starting guess. Look at the tabs above the chart, and raise the learning rate to see steps that are too big.

The weight settles near 0.520.52. The loss never reaches zero, because the measurements disagree with each other.

Putting it to work

Nobody told it the conversion. Now the archaeologist can read the rest of the papyrus, which gives the Great Pyramid as 280 cubits tall: 280×0.52≈147280 \times 0.52 \approx 147 m.

Where this comes from

Least squares: Legendre and Gauss, around 1800. Gradient descent: Cauchy, 1847. Backpropagation: Rumelhart, Hinton and Williams, 1986. A neuron with learned weights: Frank Rosenblatt’s perceptron, 1958.

Next: one neuron can only draw a straight line, and only through zero. More inputs, more neurons.

← Browse all subjectsBack to the beginning ↑