Week 12 · Neural networks
Read the words of the video
- Imagine a huge room of tiny voters. Each voter checks a few things, decides their opinion, then passes it to the next row of voters. This continues until the last row makes a final decision, like "This photo is a cat!" This is like a neural network.
- Each voter is a neuron, and each row is a layer. Stacking layers helps the network build complex ideas from simple ones, like finding edges then shapes. Training means nudging their 'trust levels' by sending blame backward if the answer is wrong. This is how it learns.
- Let's see training in action! Imagine a tiny chain of two voters. An input goes forward to get an answer. If it's wrong, we send the 'blame' backward through the chain. Each voter adjusts its trust level just a little, learning from the mistake to improve next time.
- For your exams, remember to count all the 'trust levels' including biases. Also, non-linear 'squish' functions are key for powerful networks, and randomly starting weights is important! Keep practicing; you're building amazing machine-learning superpowers, one neuron at a time!
The one-minute story
Picture a huge room of tiny voters. Each voter looks at a few numbers, decides how much to trust each one, adds up their opinion, and then gives a "how excited am I?" answer between low and high. Their answers are passed to a second row of voters, who weigh those answers in the same way, and so on, until a final row announces "this photo is a cat (92%)".
That room is a neural network. Each voter is a neuron; each row is a layer. A single neuron is just logistic regression from last week. Stacking layers lets the network build complicated features out of simple ones: edges, then shapes, then faces.
Training means starting with random trust levels (weights) and nudging them: show an example, see how wrong the answer was, and send the blame backwards through the network (backpropagation) to see how each weight should change. Repeat many times: that is gradient descent.
By the end of this week you can:
- describe a neuron, activation functions, and a layer;
- run a forward pass and count parameters;
- use softmax and cross-entropy for multiclass classification;
- explain backpropagation as the chain rule and do a small example;
- list practical issues: overfitting, vanishing gradients, learning rate.
Words you need
- Neuron (unit) - computes .
- Activation function - the non-linear "squish".
- Layer - a group of neurons that all see the same inputs. Input, hidden and output layers.
- Weights / biases - the learnable numbers.
- Forward pass - computing the output from the input.
- Backpropagation - computing every weight's gradient by running the chain rule backwards.
- Epoch - one full pass over the training data.
Idea 1: One neuron, and why we need activations
A neuron takes inputs , forms and outputs .
| Activation | Formula | Range | Notes |
|---|---|---|---|
| Sigmoid | probability-like; saturates (gradient ) for large | ||
| tanh | centred at 0; also saturates | ||
| ReLU | cheap; no saturation for ; today's default | ||
| Softmax (output layer) | probabilities that sum to 1 | for multiclass | |
| Step | if else | the perceptron's activation |
Why non-linear? If were just the identity, then two layers compute , which is one linear layer. Stacking would add nothing. Non-linear activations are what let depth create richer and richer functions.
Special cases: a neuron with a step activation is the perceptron (week 9); with a sigmoid it is logistic regression. A neural network is a stack of logistic-regression-like units whose outputs feed the next row.
Idea 2: Layers and the forward pass
For a layer with weight matrix (one row per neuron), bias vector and activation applied entry by entry:
and the next layer takes as its input.
Question. A network has 2 inputs, one hidden layer of 2 ReLU neurons with (biases ), and one linear output neuron with weights (bias 0). Compute the output for . Then count the parameters of a network.
Solution.
- Hidden pre-activations: .
- ReLU: .
- Output: .
Parameter count for : layer 1 has weights biases ; layer 2 has . Total . In general a layer from to units has parameters.
Idea 3: Multiclass output with softmax
For classes, the output layer has neurons producing scores (logits) . Softmax turns them into probabilities:
(All positive, adding to 1; the biggest score gets the biggest share.) The loss is the cross-entropy: where is the true class (with one-hot labels, ). With this is the logistic loss of week 9.
Question. Scores . Find the softmax probabilities and the cross-entropy loss if the true class is the first.
Solution. , , ; sum . Probabilities: . Loss . If the true class were the third, the loss would be : a big penalty for a confident mistake.
Idea 4: Training and backpropagation
Define a loss (squared error for regression, cross-entropy for classification), then run gradient descent on all weights:
The gradients come from the chain rule (MLF week 2). Backpropagation is simply an efficient bookkeeping of it: compute everything forward and remember the intermediate values, then walk backwards from the loss, multiplying local derivatives and reusing results, so one backward pass yields all the gradients.
Question. A network is , then , with loss . Take , , , and learning rate . Compute the gradients and do one update.
Solution.
Forward: , , .
Backward (chain rule, from the loss to the weights):
- .
- .
- .
- .
Update: , .
Check: the new prediction is , much closer to ; the loss fell from to about . ✓ Notice was computed once and reused: that reuse is the whole trick of backprop.
Idea 5: Practical matters
- Non-convex: the loss surface has many local minima and saddles, so results depend on the random start (weights are initialised randomly, not to zero: if all weights start equal, all neurons in a layer stay identical).
- Mini-batch SGD with a sensible learning rate (too big diverges, too small crawls).
- Vanishing gradients: with sigmoid/tanh in deep nets, gradients multiply many numbers below 1 and shrink to nothing; ReLU helps.
- Overfitting: networks have huge numbers of parameters and can memorise the training data. Remedies: more data, weight decay (an penalty, as in ridge), early stopping (stop when validation error rises), dropout.
- Universal approximation: even one hidden layer with enough neurons can approximate any reasonable function; depth makes it far more efficient.
- Scale your inputs before training.
Your turn
A single neuron has , and a sigmoid activation. Compute its output for .
Need a hint?
.
Show the full solution
. .
A network has inputs of size 10, hidden layers of size 8 and 6, and 3 outputs. How many weights and biases?
Need a hint?
Layer by layer: .
Show the full solution
: . : . : . Total .
Scores . What are the probabilities? Scores ?
Need a hint?
Equal scores give equal shares.
Show the full solution
. For : and .
A network has two layers with identity activations: . Show it is no more powerful than one layer.
Need a hint?
Matrix multiplication is associative.
Show the full solution
with . It is a single linear layer. Non-linear activations are what make depth useful.
For the chain , , with , , , , give and .
Need a hint?
Forward first: , .
Show the full solution
. . . .
Exam radar
In the MLT End Term: Activation Functions in 19 of 49 papers, Neural Networks in 16, Architecture in 18, ReLU in 11, Sigmoid in 10, Perceptron in 13.
Typical: forward pass through a tiny network; parameter counting; softmax values; "which activation is needed to make the network non-linear?"; derivative of sigmoid (); backprop gradient for a small chain; "what happens with identity activations?".
- Count the biases too: per layer.
- Without non-linear activations, depth adds nothing.
- Softmax outputs sum to 1; sigmoid outputs do not need to.
- Do not initialise all weights to the same value.
- Sigmoid derivative is , which is why deep sigmoid nets suffer vanishing gradients.
Quick quiz
How many parameters (weights and biases) are in a 4 to 5 to 3 network?
- 35
- 43
- 38
- 60
Why do neural networks need non-linear activation functions?
- Otherwise any stack of layers collapses to one linear map.
- To make the numbers smaller.
- To remove the bias.
- To speed up the forward pass.
The softmax of the scores (1, 1) is...
- (1, 1)
- (0, 1)
- (0.5, 0.5)
- (0.25, 0.75)
Select all TRUE statements. (select all that apply)
- Backpropagation applies the chain rule to compute gradients.
- A sigmoid neuron is a logistic regression unit.
- Setting all initial weights to the same constant is good practice.
- The loss surface of a neural network is convex.
Recap
- Neuron ; layer . Activations: sigmoid, tanh, ReLU, softmax. Non-linearity is essential.
- Parameters of a layer : .
- Softmax + cross-entropy for multiclass.
- Backpropagation = chain rule with reuse; update . Non-convex; random initialisation; watch overfitting and vanishing gradients.
- ; ReLU ; sigmoid with derivative
- Softmax ; loss
- Parameters per layer:
- Chain rule:
- MLF week 7 - Finding the lowest point: gradient descent: Backpropagation is gradient descent plus the chain rule.
- MLP week 11 - Neural networks in sklearn: Neural networks: the theory, then the code.
- MLP week 7 - Multiclass classifiers: Multiclass classification.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 18 real questions on this week All End Term papers