The artificial neuron
A neuron multiplies each input by a weight, adds the results, and can then transform the sum. You can follow the entire calculation with two numbers.
Open this chapter with experiments and audio
What does a single neuron do?
Take a rectangular room. Enter its length and width as two numbers. A neuron with two inputs contains two weights, one for each input, and usually a bias. Each weight determines the change in the weighted sum when its input increases by one unit. The bias gives the value of the sum when both inputs are zero.
z = w₁ × length + w₂ × width + b
With weights 2 and 3 and bias 0, a 5 by 5 room gives 10 + 15 = 25. That agrees with the area. Before changing any weights, make a prediction for a room measuring 10 by 10. The neuron gives 20 + 30 = 50. Its actual area is 100 m².
Change one value and predict the effect
Change a single input while keeping the other fixed. For this neuron, making the room one metre longer always changes its weighted sum by the same amount. For an actual rectangle, the extra square metres depend on the width. At width 3 the added strip covers 3 m². At width 8 it covers 8 m². This difference explains exactly why one linear neuron cannot learn the area rule for arbitrary rooms.
What changes after the sum?
An activation function transforms the sum. ReLU keeps positive values and replaces negative values with zero. A ReLU neuron can respond over part of its input range and remain silent elsewhere. Combining several such neurons produces a piecewise linear function with bends. Those bends make it possible to approximate curved relationships over a chosen range.
Keep the weight at 2 in the graph below. First move the input, then hold it still and move the bias. Locate the input at which the output first rises above zero.
When the bias is −2, the line crosses the threshold at x = 1. A positive bias moves the threshold to the left. Above it, every extra input unit adds two output units.
weights = [2, 3]
bias = -12
for room in [(1, 1), (5, 5), (10, 10)]:
z = sum(w * x for w, x in zip(weights, room)) + bias
print(room, z, max(0, z))
Ordinary Python 3 is sufficient. The first weighted sum is −7, so its activation is zero. For the 5 by 5 room the sum is 13 and passes through. Try changing only the bias. The threshold moves even though the weights stay fixed.
Who chooses the three numbers?
The layer definition determines that three parameter slots exist. The initialization rule gives each slot a starting value. During training the loss measures how well the model performed on examples. A gradient tells the optimizer how a small change to each slot would change this loss locally. The optimizer uses those gradients to update the stored numbers.
If the same trained neuron sees a new room, which numbers change?
The inputs and computed activations change. Its two weights and bias keep their saved values during ordinary inference.
How can two simple units measure a distance?
Suppose the desired output is the distance of a sensor reading x from zero. A reading of −3 is three units away, just like +3. A single straight line has one slope everywhere. Distance decreases with x to the left of zero and increases with x to the right. A change of slope is needed at zero.
A rectified linear unit, ReLU, outputs the larger of its input and zero. Send x to one unit and −x to another, then add the two outputs. For x = −3 they are 0 and 3. For x = +2 they are 2 and 0. For x = 0 they both give 0. Their sum is |x| for every real x. One branch supplies the increasing half of the function and the other supplies the decreasing half. These weights were chosen by hand so the mechanism is visible.
Can you use this distance to compare the two on/off sensors in chapter 11?
Give the first unit left − right and the second right − left. Their sum is |left − right|. Equal inputs give 0. Unequal binary inputs differ by 1 and give 1. For continuous inputs it measures the size of their disagreement. If you remove both ReLU operations, the two differences always cancel and the output is zero.
Test what happens when you move or remove a nonlinearity →
Further reading · PyTorch · ReLU