DEEP LEARNING ยท INTERVIEW QUESTION
Explain backpropagation. Why is it efficient?
๐ข beginner ยท Neural Network Fundamentals
Hint: Chain rule applied recursively. Think about computational graph and reverse-mode autodiff.
Answer
Backpropagation computes gradients of the loss with respect to all parameters using the chain rule applied in reverse through the computational graph.
How It Works
- Forward pass: compute output layer by layer, caching intermediate activations
- Backward pass: compute gradients from output back to input using cached values
- Update: adjust parameters using computed gradients
Why It's Efficient
Let be one function evaluation cost and the number of parameters; assume one scalar loss.
| Method | Work for the full gradient |
|---|---|
| Numerical differentiation | โ perturb each parameter |
| Forward-mode autodiff | with N seeded directional passes |
| Reverse-mode (backprop) | work, with saved/recomputed intermediates |
python
import torch
# PyTorch handles backprop automatically
x = torch.randn(10, requires_grad=True)
y = (x ** 2).sum() # forward pass
y.backward() # backward pass (backprop)
print(x.grad) # dy/dx = 2x for all params at onceBackprop costs only ~2-3ร one forward pass to compute ALL gradients โ this efficiency is what made training deep networks tractable.
Related questions
- What are activation functions? Compare ReLU, GELU, and Swish.
- Compare Adam, SGD with momentum, and AdaGrad. When would you use each?
- Explain batch normalization. Why does it help training?
- What is dropout and how does it prevent overfitting?
- Explain the key innovations in ResNet. Why do residual connections work?