Why Look Inside
Neural networks power image recognition, speech recognition and language models. They can seem mysterious, but each part is simple arithmetic. This lesson follows that arithmetic by hand for one small unit, then shows how many such units, and many small corrections, add up to a model that learns. The next lesson trains a small network on real images.
One Neuron
A network is built from neurons, also called units. A neuron does two things:
- It calculates a weighted sum: each input is multiplied by its own weight, the products are added, and a bias is added at the end. Weights say how much each input matters, and in which direction; the bias shifts the result.
- It passes that sum through an activation function. A common one, called ReLU, keeps positive values unchanged and turns negative values into 0.
Worked example: three inputs 2, 1 and 3; weights 0.5, −1 and 0.25; bias 0.5.
| Step | Calculation | Result |
|---|---|---|
| Weighted sum | 0.5 × 2 + (−1) × 1 + 0.25 × 3 + 0.5 | 1.25 |
| ReLU | 1.25 is positive, so it is kept | 1.25 |
If the weighted sum had been −0.7, ReLU would output 0. That on-off behaviour is what lets networks build up patterns that a single weighted sum could not represent.
Layers
Neurons are arranged in layers. The inputs, such as 64 pixel values of an image, feed every neuron in the first hidden layer; each of those neurons has its own weights and bias. Their outputs feed the next layer, and so on, to the output layer, which produces the prediction: for digit images, one output per digit, 0 to 9. A network with one hidden layer of 32 neurons reading 64 pixels already has 64 × 32 weights between those two layers, plus 32 × 10 weights to the output layer.
"Deep" learning simply means many hidden layers. Nobody chooses the weights by hand: there are far too many, and nobody knows the right values. They are learned.

Read the diagram from left to right, then follow the return arrow. Inputs flow forward through the layers to an output. The output is compared with the right answer to measure the error, called the loss. The return arrow is training: the loss is used to adjust every weight a little.
Loss: Measuring the Error
To learn, the network needs a number that says how wrong it is: the loss. For a number target, one common loss is the squared error: the difference between the right answer and the output, squared. Suppose the right answer for our neuron's input was 2:
loss = (2 − 1.25)² = 0.75² = 0.5625
For classification, networks use a loss based on how much probability they gave to the right class. Whatever the form, lower loss means better predictions on the training data.
Training: Many Small Corrections
Training repeats one idea: change each weight slightly in the direction that lowers the loss.
For each weight, calculus gives the gradient: how much the loss would change if that weight changed slightly, and in which direction. For the third weight in the example, the gradient is −4.5: increasing that weight would lower the loss. Training takes a small step against the gradient, scaled by the learning rate. With a learning rate of 0.02:
| Step | Calculation | Result |
|---|---|---|
| New third weight | 0.25 − 0.02 × (−4.5) | 0.34 |
| New weighted sum | 0.5 × 2 + (−1) × 1 + 0.34 × 3 + 0.5 | 1.52 |
| New loss | (2 − 1.52)² | 0.2304 |
The loss fell from 0.5625 to 0.2304 after one small step. A real network updates every weight at once, using the gradient for each, and repeats over many examples and many passes through the data. Finding all the gradients efficiently, layer by layer from the output backwards, is called backpropagation. This repeated stepping downhill on the loss is gradient descent; scikit-learn's networks use refined versions of it, by default one called Adam.
Two consequences matter in practice:
- Training is a search that can stop before it has finished. If it hits its step limit first, scikit-learn warns that the optimisation has not converged, as with logistic regression.
- Networks are sensitive to the scale of their inputs; features on very different scales make the steps poorly sized. Scaling the inputs first, as in the leakage lesson, is standard practice.
What a Trained Network Is
After training, a network is a large set of numbers: weights and biases. Making a prediction is the forward calculation from this lesson, repeated for every neuron. The network does not store its training examples, and it does not reason about them; it has numbers that happen to produce good outputs for inputs like the ones it was trained on. Its answers on very different inputs can be confidently wrong, which matters again for language models.
Check Your Understanding
- A neuron has inputs 1 and 4, weights 2 and −1, and bias 1. What does it output with ReLU?
- Why does a network's answer for the same input change after training, although its code does not?
- A loss curve keeps falling but training stops with a convergence warning. What happened, and what are two possible responses?
Answer and Reasoning
- The weighted sum is 2 × 1 + (−1) × 4 + 1 = −1. ReLU turns negative values into 0, so the output is 0.
- Training changed the weights and biases, the numbers the code calculates with. The calculation is the same; the numbers in it are different.
- Training reached its maximum number of iterations while the loss was still improving. Allow more iterations, or make each step more effective, for example by scaling the inputs. Then check the test score, since a lower training loss does not by itself mean better predictions on new data.