Lesson 14: Neural Networks: How They Work
A neural network is the most flexible model in this course, and also the one most often described in mystical terms. Strip away the mystique and it is something you already understand: a model made of many small units, each doing a simple calculation, connected in layers. The name comes from a loose inspiration — the brain's neurons — but a neural network is not a brain. It is a mathematical function built from simple pieces, and its power comes from combining a great many of those pieces.
Each unit, or neuron, holds a number called its activation. A neuron adds up the activations of the neurons feeding into it, each multiplied by a weight — a number that says how strongly that connection matters — then adds a bias, and passes the result through a simple function that decides how strongly it fires. The network is arranged in layers: an input layer that holds the features, one or more hidden layers that transform them, and an output layer that gives the prediction. A network built to recognise handwritten digits might take a 28 by 28 grid of pixels as its input — 784 numbers — pass them through hidden layers, and finish with ten outputs, one per digit.
Learning by adjusting the weights
Training a neural network means adjusting its weights and biases so that its predictions get better, and it uses the same idea you met in Lesson 3: a loss function, and gradient descent to reduce it. The difference is scale. A small digit-recognition network has around thirteen thousand weights and biases to adjust, and a large one has millions or billions. The algorithm that works out how each weight should change is called backpropagation: it measures how much each weight contributed to the final error and nudges it accordingly, starting from the output layer and working backwards. That is why it is called back-propagation — the error signal propagates backwards through the layers.
Hidden layers are where the magic, such as it is, happens. The first hidden layer might learn to detect edges in an image; the next combines edges into shapes; the next combines shapes into parts of a digit. Nobody programs those detectors in: they emerge from training, because detecting edges turns out to be useful for reducing the loss. This is also why neural networks can do things straight lines cannot: by stacking layers, they build up complex patterns from simple ones, step by step.
Why networks need bends: activation functions
There is a detail in the neuron's calculation that deserves attention: after adding up its inputs, each neuron passes the result through an activation function, and that function must be non-linear — it must bend. If every neuron only added and multiplied, then a whole network, however many layers it had, would collapse into a single straight-line calculation, and it could never learn anything a straight line could not. The bend is what lets layers build on each other: the first layer's bends feed the second layer's bends, and complex patterns emerge from the combination. The most common activation today is called ReLU, which simply outputs zero for negative inputs and the input itself for positive ones — a humble bend, but enough. Older networks used the sigmoid curve you met in Lesson 7, which squeezes values between zero and one. The choice matters for training speed and stability, but the principle is the same: without a bend at each neuron, depth buys nothing at all.
Neural networks are powerful, but they are hungry and opaque. They need far more data than a decision tree or a linear model, they take longer to train, and when they make a mistake it is hard to say why — you cannot read a million weights the way you can read a decision tree. That is why they are not the answer to every problem: for neat tabular data, a random forest often wins. But for images, sound and language, where the patterns are layered and complex, neural networks are the best tool we have — and the next lesson shows how far that reaches into everyday life.
A small network: inputs, hidden layers, outputs
Information flows from left to right. Every connection carries a weight, and training adjusts all of them at once.
Key Terms
- Neural network
- A model made of layers of small units, each computing a weighted sum of its inputs, used for complex patterns in images, sound and language.
- Neuron
- One unit of a network, holding a number called its activation, computed from the neurons feeding into it.
- Activation
- The number held by a neuron, passed on to the next layer.
- Weight
- A number that says how strongly one neuron's output affects the next neuron; training adjusts the weights.
- Hidden layer
- A layer between the input and output layers, where the network builds up intermediate patterns such as edges or shapes.
- Backpropagation
- The training algorithm for neural networks: it works out how much each weight contributed to the error and nudges it, starting from the output layer and working backwards.
- Deep learning
- Machine learning with neural networks that have many hidden layers, able to build complex patterns from simple ones.
Discussion Questions
- A network's first hidden layer learns to detect edges, though nobody programmed it to. How does training produce that without anyone telling it to?
- Why would a random forest be a better choice than a neural network for a small spreadsheet of house prices?
- If a neural network makes a wrong decision about a person, why is it hard to explain what went wrong?
Practice Questions
- What is a neuron in a neural network?
- What is a weight, and what happens to it during training?
- What are hidden layers for?
- What is backpropagation?
- Name two disadvantages of neural networks compared with a decision tree.
Show answers
- One unit that holds a number, its activation, computed from the neurons feeding into it. It adds up its inputs, each multiplied by a weight, adds a bias, and passes the result through a simple function.
- A weight says how strongly one neuron affects the next; training adjusts it to reduce the loss. All the weights together are what the network learns.
- They build up intermediate patterns, such as edges, then shapes, then parts of a digit. Stacking layers lets the network build complex patterns from simple ones, step by step.
- The algorithm that works out how much each weight contributed to the error and nudges it, working backwards from the output. It is how gradient descent is applied to a network with many layers.
- They need much more data, and they are hard to explain. A tree can be read by a person; a network's millions of weights cannot, and training takes longer.