Chapter 3 of 13 · 18 min

Backprop From Scratch

The chain rule becomes a machine for learning.

Goal of this chapterAfter this chapter you will understand computational graphs, gradients, backpropagation, autograd, and why neural networks can be trained by nudging parameters.
Watch this chapterAndrej Karpathy's key moments on backprop from scratch, back to back
5 moments · 4 min
1 / 5
“Even neural networks are just a mathematical expression.”
Educational talk, 2021-11 · 4:23

The lesson

Backpropagation starts from a simple idea: treat a model as a computational graph, where each node is a small mathematical operation and the whole network is just one larger expression. Karpathy emphasizes that this is not specific to neural networks; a neural net is still just a mathematical expression, so the same differentiation machinery applies to it as to any other composed function12. This framing matters because it turns training from mysterious "AI" behavior into ordinary calculus on a graph of additions, multiplications, and nonlinearities.

The key mechanism is the chain rule applied locally. Each operation knows how its output changes with respect to its inputs, and during the backward pass those local derivatives are combined with the gradient coming from above; when gradients flow through a path, the correct operation is multiplication3. That is why backprop can scale: you do not symbolically differentiate the entire expression at once, you let each small gate contribute its piece, such as tanh using its own local backward rule4.

Autograd is the system that automates this bookkeeping. Karpathy describes micrograd as a tiny autograd engine that implements backpropagation, storing enough information during the forward pass so the backward pass can traverse the graph and accumulate gradients efficiently52. Once those gradients tell you how each parameter affects the loss, learning becomes a repeated loop of running the model, propagating gradients backward, and nudging parameters to reduce the loss62.

This also explains why neural networks can fail to learn well if the signals inside the graph are unhealthy. If the model starts with outputs that are far too extreme, it can be confidently wrong at initialization instead of behaving like a near-uniform predictor, which makes optimization harder78. And because nonlinearities shape gradient flow, saturated activations can shrink useful learning signals, so inspecting activations and gradients is part of understanding whether backprop is giving the network a chance to improve74.

Once you see training as gradients flowing through a graph, the parameter update step becomes intuitive. A gradient tells you which direction increases the loss, so you move parameters in the opposite direction, repeating this many times; learning rate choices then control how large those nudges are, and can be reduced over time for finer adjustments69. Karpathy's larger point is that modern deep learning works because we can efficiently compute exact gradients through large expressions and use them to iteratively improve parameters2.

5 principles from Andrej Karpathy

1

See a neural network as a computation graph

Treat every model as a composition of small operations instead of a monolithic black box. This makes differentiation and debugging concrete, because each piece has a clear forward computation and a clear local effect on the output.

“Even neural networks are just a mathematical expression.”
Educational talk, 2021-11
2

Apply the chain rule locally

Backprop works by letting each operation contribute its local derivative and combining it with the incoming gradient. In practice, this means gradients are propagated step by step through the graph rather than by deriving one huge formula.

“…the correct thing to do is to multiply them…”
CS231n Winter 2016 Lecture 4
3

Use autograd to automate gradient bookkeeping

An autograd engine records enough structure during the forward pass to run a backward pass later. This lets you focus on defining the computation while the system computes parameter gradients needed for learning.

“…micrograd is basically an autograd engine.”
Educational talk, 2021-11
4

Diagnose learning by inspecting outputs and gradients

If a network starts off overly certain, it may be confidently wrong and harder to optimize. Check whether initial predictions look reasonable and whether nonlinearities are allowing gradients to pass instead of crushing them.

“…the network is very confidently wrong…”
Building makemore Part 3 lecture, 2021
5

Train by repeated small parameter nudges

The training loop is a rhythm: compute predictions, backpropagate gradients, then update parameters. Learning rate controls the size of each step, and shrinking it over time can help with fine-tuning later in training.

“…forward, backward, update; forward, backward, update.”
CS231n Winter 2016 Lecture 4

In his own words

The quotes behind the lesson, word for word from the transcripts. Numbers match the lesson above.

1
“Even neural networks are just a mathematical expression.”
Educational talk, 2021-11
2
“Micrograd is basically an autograd engine. Autograd is short for automatic gradient, and really what it does is it implements backpropagation. Now, backpropagation is this algorithm that allows you to efficiently evaluate the gradient of some kind of a loss function with respect to the weights of a neural network, and what that allows us to do then is we can iteratively tune the weights of that neural network to minimize the loss function and therefore improve the accuracy of the network. So backpropagation would be at the mathematical core of any modern deep neural network library, like say PyTorch or JAX. So the functionality of micrograd is, I think, best illustrated by an example, so…”
Educational talk, 2021-11answer page →
3
“…the correct thing to do is to multiply them…”
CS231n Winter 2016 Lecture 4
4
“…the backward pass for tanh is one minus t squared…”
Educational talk, 2021-11
5
“…micrograd is basically an autograd engine.”
Educational talk, 2021-11
6
“…forward, backward, update; forward, backward, update.”
CS231n Winter 2016 Lecture 4
7
“…the network is very confidently wrong…”
Building makemore Part 3 lecture, 2021
8
“At initialization, we have no reason to believe any characters to be much more likely than others, and so we'd expect that the probability distribution that comes out initially is a uniform distribution assigning about equal probability to all the 27 characters. So basically, what we'd like is the probability for any character would be roughly 1 over 27. That is the probability we should record, and then the loss is the negative log probability. So let's wrap this in a tensor and then we can take the log of it, and then the negative log probability is the loss we would expect, which is 3.29, much, much lower than 27.”
Building makemore Part 3 lecture, 2021answer page →
9
“You'll notice that in the update here, the learning rate is scaled as a function of number of iterations, and it shrinks. This is something called learning rate decay. So, in the beginning, you have a high learning rate, and as the network sort of stabilizes near the end, you bring down the learning rate to get some of the fine details in the end. And in the end, we see the decision surface of the neural net, and we see that it learns to separate out the red and the blue area based on the data points.”
Educational talk, 2021-11answer page →

Reflect

  1. Pick a simple function with a few operations, like a weighted sum followed by tanh. Can you describe the forward pass and then explain, node by node, how the gradient would move backward?
  2. Think of a model or system you have worked with that felt like a black box. How would representing it as a computation graph change the way you debug or improve it?
  3. When you initialize a model or start a project, what would count as a healthy baseline behavior before optimization begins?

Check your understanding

1. Why is backpropagation efficient for training neural networks?
2. What is the role of an autograd engine like micrograd?
3. A network begins training with extremely large, overconfident outputs and a very high loss. What is the main issue Karpathy highlights?

Go deeper

Search every transcript
Open the full search page →