Backpropagation and Autograd
How gradients flow backward through a computation graph, and how automatic differentiation libraries compute them for you.
Intermediate · about 8 hours · difficulty 4/5
Learn first: Perceptrons and Multilayer Networks
Backpropagation computes the gradient of the loss with respect to every weight by applying the chain rule from the output back to the input, reusing intermediate results so the cost is similar to one forward pass.
Understand it by building it
- Represent the computation as a graph of simple operations (add, multiply, tanh).
- Each node knows its local derivative; multiply local derivatives backward along the graph.
- Gradients from multiple paths into a node add up.
- Implementing a tiny scalar autograd engine (in the style of micrograd) makes this click permanently.
Autograd in frameworks
PyTorch, TensorFlow and JAX record operations and compute gradients automatically: loss.backward() in PyTorch, GradientTape in TensorFlow, jax.grad in JAX. You still need the mental model to debug.
Problems it explains
- Vanishing gradients: repeated multiplication by small derivatives (sigmoid saturation, deep stacks) shrinks gradients toward zero.
- Exploding gradients: the opposite, common in RNNs; fixed with gradient clipping.
- Why initialization, normalization and residual connections matter.
Good enough to move on when you can build a small autograd engine, train an MLP with it, and verify its gradients against PyTorch.
Key concepts
- Computation graphs
- Chain rule backward pass
- Gradient accumulation across paths
- Automatic differentiation
- Vanishing and exploding gradients
- Gradient clipping
Mini project
Implement a scalar autograd engine with add, multiply, tanh and backward(), train a small MLP on make_moons with it, and compare gradients to PyTorch on the same inputs.
Check yourself
Why do gradients from two different paths into the same node get summed rather than averaged or overwritten?
Free resources
- Neural Networks: Zero to Hero (micrograd lecture) (Andrej Karpathy)
- CS231n: Backpropagation, intuitions (Stanford)
- Neural Networks series (backpropagation chapters) (3Blue1Brown)