Chapter 2 of 13 · 24 min

Learn by Building

Karpathy’s curriculum: rebuild the thing until the abstraction becomes obvious.

Goal of this chapterAfter this chapter you will understand Karpathy’s build-from-scratch learning style, from micrograd to makemore to GPT-2 reproduction, and why teaching and implementation are central to his pedagogy.
Watch this chapterAndrej Karpathy's key moments on learn by building, back to back
6 moments · 4 min
1 / 6
“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-11 · 0:44

The lesson

Karpathy’s way of learning starts by rebuilding the system yourself at the smallest scale where the core idea is still visible. In micrograd, that means reducing deep learning to an autograd engine and seeing neural networks as mathematical expressions, so backpropagation stops feeling magical and becomes a set of local derivative rules composed through a graph123. The point is not to stay at toy scale forever; it is to make the mechanism legible before you rely on large libraries.

He applies the same pattern to language modeling. In makemore, he begins with a simple next-character model so you can understand training examples, probabilities, sampling, and optimization with the same machinery even as the forward pass becomes more sophisticated45. That progression also reveals why simple count tables fail as context grows, and why learned embeddings matter: they let the model share structure across similar contexts instead of memorizing every combination separately67.

From there, Karpathy scales the learner’s mental model without changing the basic workflow: tokenize inputs, compute logits, optimize loss, sample outputs. When he explains GPT from scratch, he still starts from simple tokenization and tiny datasets, then adds the Transformer as the component doing the main representational work8. In the GPT architecture, autoregressive generation depends on the triangular mask so the model predicts the next token without seeing the future, which makes the abstract idea of language modeling concrete at the implementation level910.

An important part of this curriculum is that supposedly peripheral pieces are also worth rebuilding. Karpathy treats tokenization as central because many failures blamed on the model actually originate there, so understanding how text becomes tokens is part of understanding the model itself111213. His build-from-scratch style therefore includes the messy edges, not just the elegant core, because real understanding has to survive contact with the full stack14.

Finally, rebuilding is not only for personal understanding; it is also how Karpathy teaches. Reproducing GPT-2 from the original weights and then reimplementing the architecture forces you to inspect what the model actually contains and how the parts fit together, from embeddings to attention to MLPs1516. That kind of reconstruction turns a famous system into a sequence of understandable choices, and it trains the habit of learning by implementation rather than by description alone1718.

5 principles from Andrej Karpathy

1

Start with the smallest faithful implementation

Reduce the system until the essential mechanism is easy to inspect. If you can build the core yourself, abstractions in larger frameworks become easier to trust and debug.

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

Keep the training loop fixed while the model grows

Move from a simple model to a richer one without changing the overall learning recipe: create examples, compute logits, measure loss, update parameters, sample outputs. This helps you see what changed in the model versus what stayed invariant in the learning process.

“None of this will fundamentally change. The only thing that will change is the way we do the forward pass where we take in some previous characters and calculate the logits for the next character in a sequence. That will become more complex, and, uh, but we'll use the same machinery to optimize it. And, um, it's not obvious how we would have extended this bigram approach into the case where there are many more characters at the input because eventually these tables would get way too large because there's way too many combinations of what previous characters, uh, could be.”
Podcast/video interview, 2020-11answer page →
3

Use embeddings to escape combinatorial explosion

When context tables become too large, stop memorizing separate cases and learn distributed representations instead. Embeddings let nearby contexts share statistical strength and support generalization.

“…you can transfer knowledge through that embedding, and you can generalize in that way.”
Building makemore Part 2: MLP, online lecture, 2020
4

Treat tokenization as part of the model

Do not treat preprocessing as a boring detail. If tokenization shapes what the model can easily represent, then understanding model behavior requires understanding token boundaries and encoding choices too.

“A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization…”
Talk / tutorial presentation, 2019
5

Learn advanced systems by reproducing them

Take a known model and rebuild it component by component until its behavior is no longer mysterious. Reproduction turns architecture diagrams into operational knowledge.

“…we're going to be reproducing the 124 million parameter model…”
OpenAI / online talk, 2022-11

Moments to watch

In his own words

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

1
“…micrograd is basically an autograd engine.”
Educational talk, 2021-11
2
“Even neural networks are just a mathematical expression.”
Educational talk, 2021-11
3
“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 →
4
“None of this will fundamentally change. The only thing that will change is the way we do the forward pass where we take in some previous characters and calculate the logits for the next character in a sequence. That will become more complex, and, uh, but we'll use the same machinery to optimize it. And, um, it's not obvious how we would have extended this bigram approach into the case where there are many more characters at the input because eventually these tables would get way too large because there's way too many combinations of what previous characters, uh, could be.”
Podcast/video interview, 2020-11answer page →
5
“How you would sample from the model. So, we're going to generate 20 samples. At first, we begin with all dots, so that's the context. And then until we generate the zeroth character again, we're going to embed the current context using the embedding table C. Now, usually, uh, here, the first dimension was the size of the training set, but here we're only working with a single example that we're generating, so this is just, um, dimension one, just for simplicity. Um, and so this embedding then gets projected into the hidden state. You get the logits. Now, we calculate the probabilities. For that, you can use f.softmax.”
Building makemore Part 2: MLP, online lecture, 2020answer page →
6
“…you can transfer knowledge through that embedding, and you can generalize in that way.”
Building makemore Part 2: MLP, online lecture, 2020
7
“That the predictions from this model are not very good because you only take one character of context. So the model didn't produce very name-like sounding things. Now, the problem with this approach, though, is that if we are to take more context into account when predicting the next character in the sequence, things quickly blow up, and this table—the size of this table—grows, and in fact it grows exponentially with the length of the context. Because if we only take a single character at a time, that's 27 possibilities of context. But if we take two characters in the past and try to predict the third one, suddenly the number of rows in this matrix, you can look at it that way, is 27 * 27. So there's 729 possibilities for what could have come in the context.”
Building makemore Part 2: MLP, online lecture, 2020answer page →
8
“Transformer is the neural net that actually does all the heavy lifting under the hood…”
Conference keynote, 2022
9
“The future cannot communicate with the past…”
Conference keynote, 2022
10
“What we implemented here is a decoder-only Transformer. So there's no component here — this part is called the encoder — and there's no cross-attention block here. Our block only has a self-attention and the feed-forward, so it is missing this third in-between piece here. This piece does cross-attention, so we don't have it, and we don't have the encoder; we just have the decoder. And the reason we have a decoder-only, uh, is because we are just uh generating text and it's unconditioned on anything. We're just—we're just blabbering on according to a given data set. What makes it a decoder is that we are using the triangular mask in our uh Transformer, so it has this autoregressive property where we can just uh go and sample from it.”
Conference keynote, 2022answer page →
11
“…tokenization is my least favorite part of working with large language models…”
Talk / tutorial presentation, 2019
12
“A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization…”
Talk / tutorial presentation, 2019
13
“So tokenization is at the heart of a lot of weirdness in large language models, and I would advise that you do not brush it off. A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization and fundamentally trace back to it. So if you've noticed any issues with large language models — can't, you know, not able to do spelling tasks very easily — that's usually due to tokenization. Simple string processing can be difficult for the large language model to perform natively. Non-English languages can…”
Talk / tutorial presentation, 2019answer page →
14
“Concludes my fairly long video on tokenization. I know it's a try, I know it's annoying, I know it's irritating. I personally really dislike this stage. What I do have to say at this point is don't brush it off. There's a lot of foot guns, sharp edges here, security issues, AI safety issues. As we saw, plugging in unallocated memory into language models. So, it's worth understanding this stage. That said, I will say that eternal glory goes to anyone who can get rid of it. I showed you one possible paper that tried to do that, and I think, I hope a lot more can follow over time.”
Talk / tutorial presentation, 2019answer page →
15
“…we're going to be reproducing the 124 million parameter model…”
OpenAI / online talk, 2022-11
16
“…everything here is just the other weights and biases and everything else of this Transformer…”
OpenAI / online talk, 2022-11
17
“…we can actually match the 124 million parameter checkpoints of GPT-2…”
OpenAI / online talk, 2022-11
18
“So the attention is the reduce, and the MLP is the map. And what you end up with is that the Transformer just ends up just being a repeated application of map produce if you want to think about it that way. So this is where they communicate, and this is where they think individually about the information that they've gathered. And every one of these blocks iteratively refines the representation at the residual stream.”
OpenAI / online talk, 2022-11answer page →

Reflect

  1. What topic are you currently learning that you understand only through APIs or diagrams? What would a smallest faithful reimplementation look like?
  2. Where in your own work do you dismiss a 'preprocessing' or 'plumbing' step that may actually determine downstream behavior?
  3. Think of a system you use that seems too large to understand. How could you create a staged curriculum for yourself, from toy version to partial reproduction?

Check your understanding

1. Why does Karpathy begin with toy implementations like micrograd or simple character models?
2. What problem do embeddings solve in the makemore progression?
3. How does tokenization fit into Karpathy’s build-from-scratch learning style?

Go deeper

Search every transcript
Open the full search page →