Backpropagation

GATE Relevance: Very High

Prerequisites

  • Multilayer Perceptron — Forward propagation, weight matrices
  • Calculus: chain rule, partial derivatives

🎯 Learning Objectives

  • Review the chain rule for computing derivatives of compositions
  • Understand computational graphs and how gradients flow backward
  • Derive the backpropagation algorithm for a 2-layer network
  • Identify the vanishing and exploding gradient problems
  • Work through a complete numerical example computing all partial derivatives

1. Chain Rule Review

Backpropagation is fundamentally an application of the chain rule of calculus.

Chain Rule (Single Variable)

If y = f(g(x)), then:

dy/dx = (dy/dg) · (dg/dx) = f′(g(x)) · g′(x)

Chain Rule (Multivariate)

If L depends on z, and z depends on w through multiple paths, then:

∂L/∂w = ∑i (∂L/∂zi) · (∂zi/∂w)

2. Computational Graphs

A neural network can be represented as a directed acyclic graph (DAG) where:

  • Each node represents an operation (multiplication, addition, activation).
  • Edges represent the flow of values (forward) or gradients (backward).

Simple Computational Graph

For a single neuron computing y = σ(wx + b) with loss L = (y − t)2:

x, w → [multiply] → m = wx
m, b → [add] → z = m + b
z    → [sigmoid] → y = σ(z)
y, t → [loss]    → L = (y − t)²

Forward pass: compute left to right. Backward pass: compute gradients right to left.

3. Backpropagation Algorithm

Consider a network with L layers. The loss function is J. We need ∂J/∂W[l] and ∂J/∂b[l] for each layer l to perform gradient descent.

General Backpropagation Algorithm

1. FORWARD PASS: Compute z[l] and a[l] for l = 1, …, L
2. Compute loss: J = Loss(a[L], y_true)

3. BACKWARD PASS:
   Compute output error: δ[L] = ∂J/∂z[L]
   For l = L, L−1, …, 1:
       ∂J/∂W[l] = δ[l] · (a[l−1])T
       ∂J/∂b[l] = δ[l]
       δ[l−1] = (W[l])T · δ[l] ⊙ f′(z[l−1])

4. UPDATE: W[l] ← W[l] − η · ∂J/∂W[l]
           b[l] ← b[l] − η · ∂J/∂b[l]

where ⊙ denotes element-wise multiplication (Hadamard product).

Key Insight: δ[l]

The quantity δ[l] = ∂J/∂z[l] is called the error signal (or local gradient) at layer l. It represents how much the loss changes with respect to the pre-activation at that layer. The beauty of backpropagation is that δ[l−1] can be computed from δ[l] using just one matrix multiplication and an element-wise product.

4. Derivation for a 2-Layer Network

Consider network [n0, n1, n2] with sigmoid activation and MSE loss.

Forward Pass

z[1] = W[1]x + b[1],   a[1] = σ(z[1])
z[2] = W[2]a[1] + b[2],   ŷ = a[2] = σ(z[2])
J = ½(y − ŷ)2

Backward Pass

Output layer gradients:

∂J/∂ŷ = −(y − ŷ) = (ŷ − y)

δ[2] = ∂J/∂z[2] = ∂J/∂ŷ · σ′(z[2])
       = (ŷ − y) · ŷ(1 − ŷ)

∂J/∂W[2] = δ[2] · (a[1])T
∂J/∂b[2] = δ[2]

Hidden layer gradients:

δ[1] = (W[2])T · δ[2] ⊙ σ′(z[1])
       = (W[2])T · δ[2] ⊙ a[1] ⊙ (1 − a[1])

∂J/∂W[1] = δ[1] · xT
∂J/∂b[1] = δ[1]

Sigmoid Derivative Shortcut

σ′(z) = σ(z)(1 − σ(z)) = a(1 − a). This means you can compute the derivative from the activation value without recomputing the exponential.

5. Complete Numerical Worked Example

Setup

Network [2, 2, 1], sigmoid activation, MSE loss, learning rate η = 0.5.

W[1] = [[0.15, 0.20],   b[1] = [[0.35],
          [0.25, 0.30]]              [0.35]]

W[2] = [[0.40, 0.45]]   b[2] = [[0.60]]

Input: x = [[0.05], [0.10]]   Target: y = 0.01

Step 1: Forward Pass

  • Hidden neuron h1: z1 = 0.15(0.05) + 0.20(0.10) + 0.35 = 0.0075 + 0.02 + 0.35 = 0.3775
    a1 = σ(0.3775) = 1/(1+e−0.3775) ≈ 0.5933
  • Hidden neuron h2: z2 = 0.25(0.05) + 0.30(0.10) + 0.35 = 0.0125 + 0.03 + 0.35 = 0.3925
    a2 = σ(0.3925) ≈ 0.5969
  • Output neuron: zo = 0.40(0.5933) + 0.45(0.5969) + 0.60 = 0.2373 + 0.2686 + 0.60 = 1.1059
    ŷ = σ(1.1059) ≈ 0.7514

Step 2: Compute Loss

J = ½(0.01 − 0.7514)2 = ½(0.7414)2 ≈ 0.2748

Step 3: Backward Pass — Output Layer

  • δ[2] = (ŷ − y) · ŷ(1 − ŷ) = (0.7514 − 0.01) · 0.7514 · 0.2486 ≈ 0.7414 × 0.1868 ≈ 0.1385
  • ∂J/∂wo1 = δ[2] · a1 = 0.1385 × 0.5933 ≈ 0.0821
  • ∂J/∂wo2 = δ[2] · a2 = 0.1385 × 0.5969 ≈ 0.0827
  • ∂J/∂bo = δ[2] = 0.1385

Step 4: Backward Pass — Hidden Layer

  • δh1 = (wo1 · δ[2]) · a1(1−a1) = (0.40 × 0.1385) × 0.5933 × 0.4067 ≈ 0.0554 × 0.2413 ≈ 0.01337
  • δh2 = (wo2 · δ[2]) · a2(1−a2) = (0.45 × 0.1385) × 0.5969 × 0.4031 ≈ 0.06233 × 0.2405 ≈ 0.01499
  • ∂J/∂w11 = δh1 · x1 = 0.01337 × 0.05 ≈ 0.000669
  • ∂J/∂w12 = δh1 · x2 = 0.01337 × 0.10 ≈ 0.001337
  • ∂J/∂w21 = δh2 · x1 = 0.01499 × 0.05 ≈ 0.000750
  • ∂J/∂w22 = δh2 · x2 = 0.01499 × 0.10 ≈ 0.001499

Step 5: Weight Updates (η = 0.5)

wo1 ← 0.40 − 0.5(0.0821) = 0.40 − 0.0411 = 0.3589
wo2 ← 0.45 − 0.5(0.0827) = 0.45 − 0.0414 = 0.4086
w11 ← 0.15 − 0.5(0.000669) ≈ 0.14967
w12 ← 0.20 − 0.5(0.001337) ≈ 0.19933
w21 ← 0.25 − 0.5(0.000750) ≈ 0.24963
w22 ← 0.30 − 0.5(0.001499) ≈ 0.29925

6. Vanishing & Exploding Gradients

Vanishing Gradient Problem

During backpropagation, gradients are multiplied by activation derivatives at each layer. For sigmoid, σ′(z) ≤ 0.25. After L layers, the gradient at layer 1 contains a factor of roughly 0.25L, which shrinks exponentially. This means early layers learn extremely slowly.

Exploding Gradient Problem

If weights are initialised too large, the product of weight matrices across many layers can grow exponentially, causing enormous gradient values. This leads to unstable training with wildly oscillating loss.

Solutions

ProblemSolutions
Vanishing gradientsReLU activation, residual connections (skip connections), LSTM/GRU, proper initialisation (He, Xavier)
Exploding gradientsGradient clipping, weight regularisation, proper initialisation, batch normalisation

7. Gradient Checking

To verify backpropagation implementation, compare analytical gradients with numerical gradients:

∂J/∂w ≈ [J(w + ε) − J(w − ε)] / (2ε)

For ε ≈ 10−7, the relative difference should be < 10−5.

GATE Tip

Gradient checking is used for debugging only, not during training (it’s too slow — requires 2 forward passes per parameter). Turn it off for actual training.

Summary

  • Backpropagation applies the chain rule to compute gradients layer-by-layer from output to input.
  • Key formula: δ[l] = (W[l+1])Tδ[l+1] ⊙ f′(z[l])
  • Gradient for weights: ∂J/∂W[l] = δ[l](a[l−1])T
  • Vanishing gradients (sigmoid) & exploding gradients (large weights) are key challenges.
  • Solutions: ReLU, skip connections, gradient clipping, proper initialisation.
  • For GATE: be able to compute one step of backpropagation numerically.