Neural networks
Neural networks power the fancy stuff: image recognition, chatbots, the offside cameras at this World Cup. They sound intimidating, but the building block is something you already understand from Lesson 2. Here we take them apart and let you fire one yourself.
A neuron is a familiar move
A single neuron does exactly two things: it takes its inputs, multiplies each by a weight and adds them up, then squashes the result with a function like the sigmoid. Look familiar?
That is logistic regression from Lesson 2, almost letter for letter. One neuron is basically a logistic regression. The power of a neural network is not one neuron, it is stacking hundreds of them into layers.
A neuron weighs its inputs, adds them, and squashes the total into a signal it passes on. Wire many neurons together in layers and the network can learn patterns far more twisty than a single straight formula ever could.
Layers: where the magic scales
Neurons are organised into layers. The input layer is your features. The output layer is the answer (win or not). In between sit one or more hidden layers, and this is where the real work happens.
Each hidden neuron learns to spot one useful combination, maybe "strong attack AND playing at home," or "tired legs AND a long trip." Later layers combine those combinations into even richer ones. A single formula cannot bend that way; a stack of neurons can. That is why neural networks handle messy things like images and language.
Fire a network yourself
Below is a small network that predicts a win. Tick which features it is allowed to use, slide their values, then fire a signal and watch it travel through the layers to a decision. Turn a feature off and the network has to decide with less to go on, exactly the trade-off a real builder wrestles with.
How to use this lab
- Use the checkboxes to select which features the network may use.
- Slide each selected feature to set today's value (0 = low, 100 = high).
- Press Fire the signal and watch gold pulses carry the data left to right.
- Read the lit-up decision and the win score. Turn features on and off and fire again to see how much each one matters.
The catch
All that power has a price. Neural networks need lots of data and lots of computing to train, and they are hard to explain: you cannot always point to one clean rule for why they decided something. For a quick, understandable football call, logistic regression or a decision tree is often the smarter pick. For reading a photo of the pitch, a neural network wins every time. Matching the model to the job is the real skill.
It is tempting to reach for a giant neural network for everything. But with only a few hundred past matches, a simple model usually beats a huge one, which just memorises. Neural networks shine when you have mountains of data (millions of images, billions of words), not a small table of games.
A whole layer in one line
Writing out $w_1 x_1 + w_2 x_2 + \dots$ for every single neuron gets old fast. So we stack the inputs into a vector and every weight into a matrix, and let the maths run a full layer at once:
Here $\mathbf{x}$ is the column of inputs, $W$ is a grid holding every weight (one row per neuron), $\mathbf{b}$ is a small nudge for each neuron (its bias), and $\sigma$ is applied to each result. One tidy line does the work of the whole layer. It is also why graphics cards, built for exactly this kind of matrix multiply, train networks so fast.
Sigmoid or ReLU?
The squash back in Lesson 2 was the sigmoid. It is perfect for a final answer between 0 and 1, but buried inside deep networks it makes learning crawl. So hidden layers today almost always use ReLU, which could not be simpler:
Keep the number if it is positive, otherwise send zero. That one little bend is enough to let a stack of neurons curve around complicated data, it is dirt cheap to compute, and it dodges the "signal fades to nothing" problem that deep sigmoid stacks suffer from.
How a network actually learns
A fresh network starts with random weights, so its first guesses are useless. Training fixes that with a loop you already half-know from Lesson 1:
- Predict: push one training match through the layers and see what comes out.
- Measure: compare the guess to the real result with a loss, a single number for how wrong it was.
- Nudge: shift every weight a hair in the direction that would have made the loss smaller.
- Repeat across the whole dataset, thousands of times, until the loss stops falling.
Each nudge follows one rule, applied to every weight, millions of times over:
In plain words: move each weight a little in the direction that lowers the loss $L$, and the step size $\eta$ is the learning rate. Working out that direction layer by layer, backwards from the answer, is backpropagation. Taking the step again and again is gradient descent. That is the engine under every neural network, from this tiny one to the giants behind chatbots.
The same network in real code
You will almost never wire this by hand. Here is our win-predictor as scikit-learn would build it: one hidden layer of four neurons, ReLU inside, trained in a single call. Press Run.
from sklearn.neural_network import MLPClassifier
import numpy as np
# each row: [ranking gap, recent form, playing at home?] scaled 0-1
X = np.array([[0.9, 0.7, 1], [0.8, 0.6, 0], [0.2, 0.3, 1],
[0.1, 0.2, 0], [0.6, 0.8, 1], [0.3, 0.4, 0]])
y = np.array([1, 1, 0, 0, 1, 0]) # 1 = won, 0 = did not
# one hidden layer of 4 neurons, ReLU inside
net = MLPClassifier(hidden_layer_sizes=(4,), activation="relu",
max_iter=2000, random_state=1)
net.fit(X, y) # gradient descent runs here
new = [[0.85, 0.75, 1]] # strong side, in form, at home
print("Win probability:", net.predict_proba(new)[0][1].round(3))
print("Call:", "WIN" if net.predict(new)[0] == 1 else "NOT A WIN")
Win probability: 0.938 Call: WIN
Same shape as the lab above: features in, one hidden layer, a probability out. The library hides the matrix maths and the gradient descent, but it is doing exactly what we just walked through.
- A neuron weighs its inputs, adds them, and squashes the total, just like logistic regression.
- Neurons stack into layers: input, hidden, output.
- Hidden layers learn useful combinations of features, which lets networks handle very complex patterns.
- A full layer is one line of matrix maths; deep layers use ReLU, and the final answer uses the sigmoid.
- Networks learn by gradient descent and backpropagation: predict, measure the loss, nudge every weight, repeat.
- They need lots of data and compute, and are harder to explain than a tree or a line.
Next: put a model to work predicting where a penalty will go.