DL Notes
Complete guide to backpropagation algorithm in neural networks - the chain rule, gradient computation, weight updates, and implementation from scratch with PyTorch.
Backpropagation (backward propagation of errors) is the algorithm that makes training deep neural networks possible. It efficiently computes gradients of the loss function with respect to every weight in the network using the chain rule of calculus.
The Core Idea
After forward propagation produces a prediction, we compare it with the true label using a loss function. Backpropagation then flows the error backward through the network, computing how much each weight contributed to the error.
The Chain Rule
The mathematical foundation of backpropagation is the chain rule:
Step-by-Step Derivation
Consider a 2-layer network with sigmoid activation and MSE loss:
| Layer 1 | z1 = W1·x + b1, a1 = σ(z1) |
| Layer 2 | z2 = W2·a1 + b2, ŷ = σ(z2) |
| Loss | L = (1/2)(y - ŷ)² |
Output Layer Gradients:
∂L/∂ŷ = -(y - ŷ)
∂ŷ/∂z2 = σ(z2)·(1 - σ(z2)) = ŷ·(1 - ŷ)
δ2 = ∂L/∂z2 = -(y - ŷ) · ŷ · (1 - ŷ)
∂L/∂W2 = δ2 · a1^T
∂L/∂b2 = δ2
Hidden Layer Gradients:
Complete Implementation from Scratch
Backpropagation in PyTorch
PyTorch handles backpropagation automatically through its autograd system:
Gradient Flow Visualization
∂L/∂W3 ∂L/∂W2 ∂L/∂W1
┌─────────────┬─────────────┬─────────────┐
│ │ │ │
↓ ↓ ↓ ↓
x ──→ [W1,ReLU] ──→ [W2,ReLU] ──→ [W3,σ] ──→ ŷ ──→ L
a1 a2 a3
δ1 ←────── δ2 ←────── δ3 ←────── ∂L/∂ŷ
Computational Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Forward Pass | O(n²) per layer | O(n) activations cached |
| Backward Pass | O(n²) per layer | O(n²) for gradients |
| Weight Update | O(n²) total | O(n²) for weight storage |
| Total per iteration | O(L·n²) | O(L·n²) |
Common Issues and Solutions
| Problem | Cause | Solution |
|---|---|---|
| Vanishing gradients | Sigmoid/tanh in deep nets | Use ReLU, ResNets |
| Exploding gradients | Large weight init | Gradient clipping, proper init |
| Slow convergence | Small learning rate | Adaptive optimizers (Adam) |
| Dead neurons | ReLU with negative inputs | Leaky ReLU, proper init |
Interview Questions
- Why is caching activations necessary during forward propagation?
Backpropagation needs the intermediate activations to compute gradients. Without caching, we would need to recompute forward passes for each layer.
- What is the vanishing gradient problem?
When using sigmoid/tanh in deep networks, gradients multiply through many layers. Since sigmoid derivative maxes at 0.25, gradients shrink exponentially, making early layers untrainable.
- How does gradient clipping prevent exploding gradients?
It scales down the gradient vector when its norm exceeds a threshold, preventing large weight updates while preserving gradient direction.
- What is the difference between backpropagation and gradient descent?
Backpropagation computes gradients using the chain rule. Gradient descent uses those gradients to update weights. They are complementary but distinct concepts.
- Can backpropagation get stuck in local minima?
In theory yes, but high-dimensional loss surfaces mostly have saddle points rather than true local minima. SGD with momentum helps escape these.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Backpropagation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Deep Learning topic.
Search Terms
deep-learning, deep learning, deep, learning, neural, network, fundamentals, backpropagation
Related Deep Learning Topics