MLCrack

    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

    1. Forward pass: compute output layer by layer, caching intermediate activations
    2. Backward pass: compute gradients from output back to input using cached values
    3. Update: adjust parameters using computed gradients

    Why It's Efficient

    Let CC be one function evaluation cost and NN the number of parameters; assume one scalar loss.

    MethodWork for the full gradient
    Numerical differentiationO(NC)O(NC) โ€” perturb each parameter
    Forward-mode autodiffO(NC)O(NC) with N seeded directional passes
    Reverse-mode (backprop)O(C)O(C) 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 once

    Backprop costs only ~2-3ร— one forward pass to compute ALL gradients โ€” this efficiency is what made training deep networks tractable.