Neural Networks and Backpropagation
A neural network is a stack of simple units that together can learn very complex functions. It is the foundation of image recognition, speech and language models.
A neuron
A neuron computes a weighted sum of its inputs plus a bias, then applies an activation:
With a sigmoid activation, a single neuron is exactly logistic regression.
Layers
Neurons are organised in layers. A layer with weight matrix (shape out × in) transforms its input vector:
A multi-layer perceptron (MLP) chains layers:
flowchart LR
subgraph Input
x1((x1)) & x2((x2)) & x3((x3))
end
subgraph Hidden["Hidden layer (ReLU)"]
h1((h1)) & h2((h2)) & h3((h3)) & h4((h4))
end
subgraph Output["Output (softmax)"]
o1((ŷ1)) & o2((ŷ2))
end
x1 & x2 & x3 --> h1 & h2 & h3 & h4
h1 & h2 & h3 & h4 --> o1 & o2Why non-linearity
Without activations, two layers collapse into one: , still linear. Non-linear activations let each layer bend the space, and stacking them builds complicated decision boundaries.
| Activation | Formula | Notes |
|---|---|---|
| Sigmoid | Output in (0,1); saturates, so gradients vanish; used for binary outputs | |
| Tanh | Zero-centred, but still saturates | |
| ReLU | Default for hidden layers: cheap, no saturation for ; units can “die” at 0 | |
| Leaky ReLU | Avoids dead units | |
| GELU / SiLU | smooth ReLU-like | Used in transformers |
| Softmax | Output layer for multi-class probabilities |
Universal approximation: a network with one hidden layer and enough units can approximate any continuous function on a bounded region. In practice, depth is far more efficient than width: deep networks reuse features hierarchically (edges → textures → parts → objects).
Training: forward pass, loss, backward pass
- Forward: compute activations layer by layer and store them.
- Loss: e.g. cross-entropy between and the true label.
- Backward (backpropagation): use the chain rule to get for every layer, from the output back to the input.
- Update with SGD or Adam.
Backprop for a two-layer network
Forward: , , , , .
Backward (the softmax + cross-entropy gradient is famously clean):
The pattern repeats for any depth: error signal × input to the layer gives the weight gradient, and Wᵀ × error, gated by the activation’s derivative passes the error back one layer. The cost of the backward pass is about twice the forward pass.
Parameters vs hyperparameters
- Parameters: weights and biases, learned by gradient descent.
- Hyperparameters: chosen by you, such as the number of layers and units, activation, learning rate, batch size, epochs, regularisation and optimiser. Tune them on validation data (grid, random or Bayesian search).
Check yourself
Why do neural networks need non-linear activation functions?
Why is ReLU usually preferred over sigmoid in hidden layers?
In backpropagation, the gradient for a layer's weights is the upstream gradient multiplied by…
Item 14 of 24 · updated 2026-10-12