Machine Learning / AI
June 2026 · 17 min read · Deep Learning · Gradient Descent · Optimisation · Last updated: 22 June 2026

Neural Networks & Backpropagation — The Maths Behind AI

Written by MySimulator Team · Reviewed by MySimulator Editorial Review

Every image recognised by your phone, every sentence completed by a language model, every recommendation served by a streaming platform — all emerge from the same algorithm: a network of weighted sums shaped by backpropagation. The core idea is disarmingly simple: make a prediction, measure the error, and nudge every weight in the direction that would have reduced that error. Repeat millions of times. What follows is the precise mathematics that transforms this intuition into the engine of the AI revolution.

1. From Perceptron to MLP

The story begins in 1958 with Frank Rosenblatt's perceptron — a model of a single biological neuron. It takes n input signals x₁, x₂, ..., xₙ, multiplies each by a learned weight, sums them with a bias, and fires (outputs 1) if the result exceeds a threshold, otherwise stays silent (outputs 0):

output = 1 if (w₁x₁ + w₂x₂ + ... + wₙxₙ + b > 0) else 0

The perceptron learning rule — increase weights for correctly classified inputs, decrease for misclassified — was shown to converge for any linearly separable problem. The 1969 book Perceptrons by Minsky and Papert famously proved that a single-layer perceptron cannot compute XOR, effectively freezing AI research for over a decade.

The solution, already known theoretically, was to stack perceptrons into layers. A multilayer perceptron (MLP) — also called a feedforward neural network or deep network — has:

With at least one hidden layer and a non-linear activation function, an MLP can approximate any continuous function on a compact domain — a fact formalised by the Universal Approximation Theorem (Section 9). The practical revolution came in the 1980s when Rumelhart, Hinton, and Williams showed how to efficiently train MLPs using backpropagation.

2. The Forward Pass: z = Wx + b

Consider a network with L layers. Let layer l have nₗ neurons. The computation at each layer proceeds in two steps:

Step 1 — Linear transformation: Form the pre-activation vector z by multiplying the weight matrix W^(l) (shape nₗ × nₗ₋₁) by the activations from the previous layer a^(l-1) and adding the bias vector b^(l):

z^(l) = W^(l) · a^(l-1) + b^(l)

Step 2 — Non-linear activation: Apply a scalar activation function σ element-wise to produce the activations for the next layer:

a^(l) = σ(z^(l))

The input to the network sets a^(0) = x (the raw features). The output of the last layer a^(L) = ŷ is the network's prediction. For classification tasks the softmax function is typically applied to the final layer to convert raw scores (logits) into a probability distribution over classes:

softmax(z)_k = e^(z_k) / Σ_j e^(z_j)

Each output node ŷ_k gives the network's estimated probability that input x belongs to class k. All outputs are positive and sum to 1, satisfying the axioms of a probability distribution.

Why is the linear step z = Wx + b not enough on its own? Stacking linear transformations produces another linear transformation: W₂(W₁x + b₁) + b₂ = (W₂W₁)x + (W₂b₁ + b₂). Without non-linear activations, a 100-layer network is equivalent to a single linear layer and cannot represent curved decision boundaries.

3. Activation Functions

The activation function σ is the source of all non-linearity — and therefore all expressive power — in a neural network. Three families dominate modern practice:

Sigmoid

σ(z) = 1 / (1 + e^(−z)) range: (0, 1)

Historically dominant. Maps any real input to (0,1), making it natural for binary classification output layers. Its gradient σ'(z) = σ(z)(1 − σ(z)) peaks at 0.25 and collapses toward zero for |z| > 3 — the root cause of the vanishing gradient problem (Section 7).

Hyperbolic Tangent (tanh)

tanh(z) = (e^z − e^(−z)) / (e^z + e^(−z)) range: (−1, 1)

Zero-centred variant of sigmoid, often preferred in hidden layers. Same vanishing gradient problem.

Rectified Linear Unit (ReLU)

ReLU(z) = max(0, z) range: [0, ∞)

Introduced as an activation for deep networks by Nair and Hinton (2010). Computationally trivial, gradient is 1 for z > 0 and 0 for z < 0 — dramatically alleviating vanishing gradients. Powers virtually all modern deep networks. Variants include Leaky ReLU (small negative slope for z < 0), ELU, and GELU (used in Transformers).

🧠 Neural Network Simulator Build layers, choose activations, and watch training converge in real time

4. Loss Function: Cross-Entropy

Training requires a scalar measure of how wrong the network's predictions are. For multi-class classification with C classes, the standard choice is the cross-entropy loss:

L = −Σ_k y_k · log(ŷ_k)

where y_k ∈ {0,1} is the one-hot true label (1 for the correct class, 0 for all others) and ŷ_k is the network's predicted probability for class k. Because exactly one y_k = 1, this reduces to:

L = −log(ŷ_correct)

The loss is zero when ŷ_correct = 1 (perfect confidence) and approaches infinity as ŷ_correct → 0. This is exactly what we want: heavily penalise confident wrong predictions.

Cross-entropy arises naturally from maximum likelihood estimation. If we model the output as a categorical distribution, maximising the log-likelihood of the training labels is equivalent to minimising the cross-entropy loss. This gives the loss a solid probabilistic foundation beyond mere convenience.

For binary classification (C = 2) this simplifies to binary cross-entropy:

L = −[y · log(ŷ) + (1−y) · log(1−ŷ)]

For regression tasks the common choice is mean squared error (MSE):

L = (1/n) · Σ_i (y_i − ŷ_i)²

5. Backpropagation and the Chain Rule

We need to compute ∂L/∂w for every weight w in the network so we can update weights to reduce the loss. A network with millions of parameters makes this seem intractable, but backpropagation solves it in a single backward pass using the chain rule of calculus.

The chain rule states that if L depends on z which depends on w, then:

∂L/∂w = (∂L/∂z) · (∂z/∂w)

In a network, the loss L depends on the output ŷ, which depends on activations, which depend on pre-activations z, which depend on weights W. Expanding the chain:

∂L/∂W^(l) = ∂L/∂ŷ · ∂ŷ/∂z^(L) · ∂z^(L)/∂a^(L-1) · ... · ∂z^(l)/∂W^(l)

The key insight of backpropagation is that these partial derivatives can be computed efficiently by passing a gradient signal (called delta, δ) backwards through the network, reusing computations at each layer. For layer l, define:

δ^(l) = ∂L/∂z^(l) (gradient with respect to pre-activations)

Starting from the output layer (using, e.g., softmax + cross-entropy, where the gradient is simply ŷ − y), propagate backwards:

δ^(l) = (W^(l+1))ᵀ · δ^(l+1) ⊙ σ'(z^(l))

where ⊙ denotes element-wise multiplication and σ'(z^(l)) is the derivative of the activation function evaluated at the pre-activations of layer l. The weight gradient is then simply:

∂L/∂W^(l) = δ^(l) · (a^(l-1))ᵀ ∂L/∂b^(l) = δ^(l)

Total computation: one forward pass + one backward pass = 2× a forward pass. Regardless of network depth, all gradients are computed in constant multiples of a single forward pass — this is what makes training deep networks tractable.

6. Stochastic Gradient Descent

Once gradients are computed, weights are updated to descend the loss landscape. Gradient descent takes a step in the negative gradient direction:

w := w − η · ∂L/∂w

where η (eta) is the learning rate — the step size. Too large and updates overshoot minima; too small and training is impractically slow.

In practice, computing the exact gradient over the entire training set (batch gradient descent) is computationally prohibitive for large datasets. Instead, stochastic gradient descent (SGD) estimates the gradient from a randomly selected mini-batch of B examples (typically B = 32–512):

∂L/∂w ≈ (1/B) · Σ_{i∈batch} ∂L_i/∂w

This noisy gradient estimate is surprisingly effective: the noise acts as implicit regularisation, helping escape shallow local minima, and mini-batch parallelism makes GPU acceleration straightforward.

Momentum and Adaptive Methods

Vanilla SGD converges slowly in ravines — regions where the curvature is much higher in one direction than another. Modern optimisers address this:

7. The Vanishing Gradient Problem

Backpropagation multiplies gradients layer by layer using σ'(z). For sigmoid, the maximum value of σ'(z) is 0.25. In a network with L = 20 layers, the gradient of the loss with respect to the first layer's weights passes through 20 multiplication by values typically much less than 1:

∂L/∂W^(1) ∝ σ'(z^(1)) · σ'(z^(2)) · ... · σ'(z^(L))

With sigmoid, typical products like 0.2^20 ≈ 10⁻¹⁴ make the gradient essentially zero. Early layers receive almost no training signal — their weights barely move while later layers overfit. This was the dominant obstacle to deep networks throughout the 1990s.

Several solutions emerged:

The exploding gradient problem (product > 1 at each step) is addressed by gradient clipping: if ||∇L|| > threshold, rescale ∇L → threshold · ∇L / ||∇L||.

📉 Backpropagation Visualiser See gradient flow layer by layer and observe vanishing gradients live

8. Batch Normalisation

Proposed by Ioffe and Szegedy in 2015, batch normalisation (BN) normalises the pre-activations within each mini-batch to have zero mean and unit variance, then applies learnable scale γ and shift β parameters:

μ_B = (1/B) · Σ z_i (mini-batch mean)
σ²_B = (1/B) · Σ (z_i − μ_B)² (mini-batch variance)
z_hat_i = (z_i − μ_B) / √(σ²_B + ε)
BN(z_i) = γ · z_hat_i + β

where ε ≈ 10⁻⁵ is a small constant for numerical stability. The learnable parameters γ and β allow the network to undo the normalisation if needed, preserving representational capacity.

Batch normalisation has several beneficial effects:

At inference time, the mini-batch statistics are replaced by running averages computed during training. For sequence models and small-batch settings, Layer Normalisation (normalising across features rather than the batch dimension) is preferred — it powers every modern Transformer.

Modern large language models like GPT-4 use Pre-LayerNorm (applying LayerNorm before the attention and feed-forward sublayers) rather than Post-LayerNorm. This choice improves training stability for very deep networks (100+ layers) and high learning rates.

9. Universal Approximation Theorem

Why do neural networks work at all? The theoretical grounding is the Universal Approximation Theorem, proved in various forms by Cybenko (1989), Hornik (1991), and Barron (1993).

The classical version states:

Let σ be any non-constant, bounded, continuous activation function (e.g. sigmoid). Then for any continuous function f: [0,1]ⁿ → ℝ and any ε > 0, there exists a single hidden-layer neural network F with finitely many neurons such that |F(x) − f(x)| < ε for all x in [0,1]ⁿ.

In plain language: a single hidden layer with enough neurons can approximate any continuous function arbitrarily well. This establishes that MLPs are not limited to simple hypotheses — they are, in principle, as expressive as any model.

However, the theorem comes with important caveats:

More modern results (Barron 1993; Eldan and Shamir 2016) quantify the depth-width tradeoff precisely. For functions with bounded Fourier energy, a two-layer network needs O(1/ε²) neurons, while deeper networks can achieve the same approximation quality with exponentially fewer neurons. Depth is not just convenient — it is computationally fundamental.

Together, the Universal Approximation Theorem and the efficiency of backpropagation explain why neural networks became the dominant paradigm in machine learning: they are both expressive enough to model the complexity of the real world and trainable enough that gradient descent actually finds good solutions.

Sources