Neural Networks Explained: Neurons, Weights and Layers
Neural networks power many of the image, speech and text tools you hear about. The name sounds like a brain in a box, but the real thing is much simpler: lots of small calculations, each multiplying, adding and passing on a number. This guide explains those pieces, how they learn, and how to count exactly how many numbers a network has to learn.
One artificial neuron
An artificial neuron is a tiny calculator. It does three things:
- Multiply each input by a weight. A weight is a number that says how much that input matters. A big positive weight means "this input pushes the answer up", a negative weight means "this input pushes it down", and a weight near zero means "ignore this".
- Add everything up, plus a bias. The bias is one extra number that shifts the total up or down, like a starting score.
- Pass the total through an activation function. This is a simple rule that shapes the output. A very common one, called ReLU, just says: if the total is negative, output 0; otherwise output the total.
Here is one neuron in Python. Examples use Python 3.10 or later and only the standard library. The inputs and weights are made up.
inputs = [1.0, 0.0] # made-up: is it furry? (1 = yes), does it bark? (0 = no)
weights = [0.8, -0.5]
bias = -0.3
total = bias
for x, w in zip(inputs, weights):
total += x * w
output = max(0.0, total) # ReLU activation: negative totals become 0
print(f"weighted sum = {total:.2f}, output = {output:.2f}")
Running it with Python 3.12 printed:
weighted sum = 0.50, output = 0.50
Check it by hand: 1.0 x 0.8 + 0.0 x (minus 0.5) + (minus 0.3) = 0.5. The total is positive, so ReLU lets it through unchanged.
The idea was borrowed loosely from brain cells, which receive signals and "fire" when the signals are strong enough. But an artificial neuron is only a formula. It does not think, feel or understand anything, so the brain comparison only goes so far.
Layers of neurons
One neuron can only draw a very simple boundary. The power comes from connecting many of them in layers:
- The input layer holds the data: for example 4 measurements of a flower, or 784 pixel values of a small picture. It does no calculating; it just passes the numbers on.
- One or more hidden layers in the middle. Each neuron in a hidden layer takes every output of the layer before it, applies its own weights and bias, and passes its answer on.
- The output layer gives the result: often one neuron per possible answer, with the biggest output treated as the network's guess.
When every neuron in one layer connects to every neuron in the next, the layers are called dense or fully connected. A network with many hidden layers is called a deep network, which is where the name deep learning comes from.
How a network learns
A new network starts with random weights, so its first guesses are nonsense. Training fixes that, one small step at a time:
- Give the network a batch of training examples, each with its correct answer (its label).
- Let it make its guesses, and measure how wrong they are with an error score.
- Work out, for every weight and bias, which direction would make the error a little smaller. The method for doing this efficiently through all the layers is called backpropagation, because it works backward from the output toward the input.
- Change every weight and bias a little in that direction. The size of the change is set by the learning rate.
- Repeat with the next batch, and keep going for many passes over the data (epochs).
That is the same "small steps downhill" idea shown for a straight line in linear regression and gradient descent, just with many more numbers to adjust at once. Nothing in the network decides to learn: the arithmetic of the update rule does all the work.
Counting parameters
The parameters are all the numbers training can change: the weights and the biases. Counting them tells you how big a network is. Between two dense layers of sizes a and b there are:
- a x b weights (one for each connection), and
- b biases (one for each neuron in the second layer).
The input layer has no biases, because it does no calculating. Here are three examples, the same ones the Neural Network Calculator gives:
| Layer sizes | Weights | Biases | Total parameters |
|---|---|---|---|
| 2, 3, 1 | 2x3 + 3x1 = 9 | 3 + 1 = 4 | 13 |
| 4, 8, 3 (4 flower measurements, 3 kinds of flower) | 32 + 24 = 56 | 8 + 3 = 11 | 67 |
| 784, 128, 64, 10 (28 x 28 pixel digit pictures, 10 digits) | 100,352 + 8,192 + 640 = 109,184 | 128 + 64 + 10 = 202 | 109,386 |
The last network looks small on paper (four layers), yet it has more than a hundred thousand numbers to learn, almost all of them in the first set of connections: 784 x 128 = 100,352. Wide layers next to each other multiply up quickly.
This function does the same counting:
def count_parameters(layers):
weights = 0
biases = 0
for a, b in zip(layers, layers[1:]):
weights += a * b # every neuron in one layer connects to every neuron in the next
biases += b # one bias for each neuron after the input layer
return weights, biases, weights + biases
for layers in ([2, 3, 1], [4, 8, 3], [784, 128, 64, 10]):
w, b, total = count_parameters(layers)
print(f"{layers}: {w:,} weights + {b:,} biases = {total:,} parameters")
Output:
[2, 3, 1]: 9 weights + 4 biases = 13 parameters
[4, 8, 3]: 56 weights + 11 biases = 67 parameters
[784, 128, 64, 10]: 109,184 weights + 202 biases = 109,386 parameters
The numbers match the calculator. Both count dense layers only. Networks for images and text often use other kinds of layers (convolutional layers for pictures, attention layers for text) that share weights in different ways, so they need different formulas.
Why the count matters
- Data. More parameters can learn more complicated patterns, but they also make it easier to memorize the training set instead of learning the pattern (overfitting). Small datasets usually suit small networks.
- Memory. Every parameter takes memory during training, and so do its gradient and the optimizer's extra numbers. For the 109,386-parameter network, the GPU Memory Calculator gives a rough planning estimate of 3.15 MB in total with the Adam optimizer, 32-bit numbers and batches of 32: tiny, so it fits on any ordinary computer. The guide to what computer you need for machine learning explains that estimate.
A short history
The idea is older than most people expect. In 1958 the psychologist Frank Rosenblatt described the perceptron, a single artificial neuron that could learn its weights from examples, in the journal Psychological Review. Training networks with hidden layers became practical after David Rumelhart, Geoffrey Hinton and Ronald Williams popularized backpropagation in a 1986 paper in Nature, "Learning representations by back-propagating errors". In 2024 John Hopfield and Geoffrey Hinton were awarded the Nobel Prize in Physics "for foundational discoveries and inventions that enable machine learning with artificial neural networks".
For parents and teachers
You can act out a tiny network with no screens at all: pupils stand in layers, each holds a card with a weight, and passes on a weighted vote. The unplugged activities guide on this blog sets out a "human neural network" step by step.