AI & machine learning / Neural networks

Neural networks

What a neural network actually computes: weighted sums, a bend, and layers. One tiny network with nine numbers is worked through by hand, then we see why the bend matters and how the numbers are learned.

Beginner7 steps~7 min
Input — a number fed into the network
Hidden neuron — weighted sum, bias, then ReLU
Output neuron — gives the network’s answer
Weight — how much one connection counts
// THE SHORT VERSION

In 30 seconds

A neural network is built from neurons. Each one multiplies its inputs by weights, adds a bias, and passes the total through a simple bend called an activation function.

Neurons are arranged in layers. Feeding numbers in at one end and computing layer by layer to the other is the forward pass.

The bend is essential: without it, any stack of layers is no more powerful than a single straight-line formula. The weights are not written by people. Training nudges them, over and over, to make the outputs less wrong.

Neural networks, step by step

STEPS01 / 07

The weights here were chosen by hand so the sums stay easy. ReLU is one of several activation functions, and real networks have thousands to billions of weights.

STEP 01 / 07 One neuron: a weighted sum
THE MATH
z = w1·x1 + w2·x2 + b = 2×1 + 2×1 + (−2) = 2
  1. Step 1: One neuron: a weighted sum. A neuron is a small calculation. It multiplies each input by a weight, which says how much that input counts, adds the results, and adds one more number of its own, the bias, which shifts the total up or down. With inputs 1 and 1, weights 2 and 2 and bias −2, the sum z is 2.
  2. Step 2: Then an activation. Before passing its sum on, the neuron puts it through an activation function. A common one is ReLU: negative sums become 0, positive sums pass through unchanged. With inputs 0 and 0 the same neuron gets z = −2 and outputs 0. That kink at zero is what makes the function non-linear.
  3. Step 3: Neurons in layers. A network is neurons wired in layers. The inputs feed two hidden neurons, h1 and h2, and their outputs feed one output neuron, y. Every arrow carries a weight and every neuron has a bias: nine numbers in all. Those nine numbers are the whole network; change them and it computes something else.
  4. Step 4: Forward pass: hidden layer. Running the network is called a forward pass: feed in the inputs and compute layer by layer. With x1 = 1 and x2 = 1, hidden neuron h1 sums 1 + 1 + 0 = 2 and h2 sums 2 + 2 − 2 = 2. Both sums are positive, so ReLU passes them on unchanged.
  5. Step 5: Forward pass: output. The output neuron treats the hidden values as its inputs: 1×2 + (−1)×2 + 0 = 0. Repeat the pass for the other inputs and a pattern appears: the output is 1 when exactly one input is 1, and 0 otherwise. This pattern is called XOR, a famous test case: no single neuron can produce it.
  6. Step 6: Why the bend matters. Remove ReLU and the layers collapse: a weighted sum of weighted sums is just one weighted sum, here y = 2 − x1 − x2. Plotted against x1 + x2 that is a straight line, and no straight line goes up and then down. ReLU lets h2 switch on part-way, which bends the output. More neurons and layers stack more bends into richer shapes.
  7. Step 7: Training, in one line. Nobody picks the weights by hand. They start random. Training runs a forward pass on an example, measures how wrong the output is (the loss), works out for every weight which direction would shrink the loss, and nudges each weight a little that way. Repeated over many examples, the loss falls and the weights settle.
VALUES SO FAR
inputs1, 1input
z2sum
numbers····learned
h1····hidden
h2····hidden
y····output
all four····XOR
no ReLU····wrong
loss····error

Spotted a mistake on this page?