Python / Python Deep Learning and Neural Networks Interview Questions
Explain backpropagation mathematically. How does the chain rule enable computing gradients through many layers?
Backpropagation is the algorithm for computing the gradient of a scalar loss L with respect to every parameter in the network. It exploits the chain rule of calculus: if the loss depends on parameter W through intermediate quantities h₁, h₂, ..., hₙ, then ∂L/∂W = (∂L/∂hₙ)(∂hₙ/∂hₙ₋₁)···(∂h₁/∂W). Backprop applies the chain rule systematically starting from the loss and working backwards through each layer, accumulating local gradients.
At each layer, two quantities are needed: the local gradient (how does the layer's output change with its input/weights?) and the upstream gradient (how does the loss change with this layer's output?). Multiplying them gives the gradient flowing to the layer's parameters and to its input, which becomes the upstream gradient for the preceding layer.
import torch import torch.nn as nn model = nn.Sequential( nn.Linear(10, 64), nn.ReLU(), nn.Linear(64, 1) ) loss_fn = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.01) x = torch.randn(32, 10) y = torch.randn(32, 1) # --- Standard training step --- optimizer.zero_grad() # 1. Clear old gradients (they accumulate!) y_hat = model(x) # 2. Forward pass â build computation graph loss = loss_fn(y_hat, y)# 3. Compute scalar loss loss.backward() # 4. Backprop â traverse graph in reverse # populates .grad for every parameter optimizer.step() # 5. Update parameters: W -= lr * W.grad # Inspect gradients of first layer print(model[0].weight.grad.shape) # torch.Size([64, 10]) # Manual chain rule for a single neuron: # loss = (y_hat - y)^2, y_hat = w*x + b # dL/dw = 2*(y_hat - y) * x <- upstream * local w = torch.tensor([2.0], requires_grad=True) x_s = torch.tensor([3.0]) y_s = torch.tensor([1.0]) loss_s = (w * x_s - y_s) ** 2 loss_s.backward() print(w.grad) # tensor([40.]) == 2*(2*3-1)*3
More Related questions...