Guide

Training a neural network means adjusting every weight so a single number, the loss, gets smaller. To do that, you need the slope of the loss with respect to every weight. The efficient method is reverse-mode automatic differentiation. In neural networks it is called backpropagation. Frameworks hide it behind loss.backward(), but if you cannot picture what that call does, you will struggle to debug training.

The problem is scale. A model can have millions of weights. Estimating each slope by nudging one weight and re-running the forward pass would take one pass per weight. Backprop gets all of them from one forward pass and one backward pass, because it works backward from the single scalar loss.

Concepts in this topic

Read in this order. Each page uses the one before it.

StepConceptWhat it answers
1Computational graphs and reverse modeHow does a forward pass record what to differentiate?
2A tiny autograd classHow does the backward walk work in code?
3Gradients for linear layers and softmaxWhat are the matrix forms for real layers?
4A worked training stepWhat does one full forward and backward step look like with numbers?

The chain rule behind all of this is on the calculus page.

Common bugs

Most training failures from backprop fall into a few patterns.

BugSymptomFix
Forgot to zero gradientsUpdates look like the sum of many past batchesZero the gradients before each backward pass
In-place op on a tensor needed for backwardRuntime error, or silently wrong gradientsUse out-of-place ops on tracked tensors
detach() or rebuilding a tensor from a Python floatGradients stop flowing upstreamDetach only for metrics, not for the loss
sum where you meant mean on the lossEffective learning rate changes with batch sizePick one reduction and stay consistent, usually mean
Train and eval modes mixed upValidation looks strangeCall model.train() and model.eval() correctly

Important

Gradients accumulate by design. Every call to backward() adds to the existing .grad. If you forget to reset them, you train on the sum of every past batch.

Memory: why activations dominate

Backprop needs the forward values, called activations, for the whole batch. For a deep network with long inputs, storing them costs more memory than the weights.

Adam keeps two moment buffers per weight, so its optimizer state is about twice the size of the weights. Activations often outgrow both as batch size and sequence length rise.

Trade-off

Store or recompute. Storing every activation makes the backward pass fast but uses a lot of memory. Gradient checkpointing keeps only some activations and recomputes the rest during the backward pass. The source's rough estimate is O(√L) memory for L layers, at about 33% extra compute. Reach for it when you run out of memory before you run out of compute.

Common mistakes

  • Reusing a value without adding its gradients. When a node feeds two paths, both paths contribute to its gradient. Use +=, not =.
  • Ignoring shapes. The gradient of a tensor always has the same shape as the tensor. If a shape is wrong, the bug is usually in a broadcast or a transpose.
  • Stopping the graph by accident. Calling .item() or converting to NumPy severs the connection. Your loss should stay connected.

Key takeaways

  • Backprop is reverse-mode autodiff on a scalar loss: one forward pass and one backward pass give every gradient.
  • The forward pass stores the graph and the intermediate values. The backward pass multiplies local derivatives by the gradient flowing in.
  • Always zero gradients before the next backward pass, and avoid in-place ops on tensors that backward needs.
  • Activation memory, not weight memory, often limits training. Checkpointing trades compute for memory.

Further learning

Karpathy builds micrograd live, which is the best companion to this topic. Pause and type along.

The spelled-out intro to neural networks and backpropagation: building microgradAndrej Karpathy

3Blue1Brown's intuition pass before the calculus:

Backpropagation, intuitively | Deep Learning Chapter 33Blue1Brown
Backpropagation calculus | Deep Learning Chapter 43Blue1Brown
CS231n: Backpropagation, IntuitionsCourse materials and notes for Stanford class CS231n: Deep Learning for Computer Vision.cs231n.github.io karpathy/microgradA tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API - karpathy/microgradGitHub