Chapter 4 of 13 · 18 min

Tiny Language Models

Names, characters, probabilities, and the first step toward GPT.

Goal of this chapterAfter this chapter you will understand language modeling as next-token prediction, how to evaluate models with likelihood, and why embeddings beat raw lookup tables.
Watch this chapterAndrej Karpathy's key moments on tiny language models, back to back
4 moments · 3 min
1 / 4
“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, 2020 · 1:13:26

The lesson

A tiny language model can be understood as a next-token predictor. In this chapter’s setup, the tokens are characters in names, and the model learns to predict the next character from the previous ones. Karpathy starts with a simple bigram view, where one character of context is enough to define a probability distribution over the next character, then treats generation as repeatedly sampling from that distribution until an end token appears1.

To judge whether such a model is good, you do not rely on whether a few sampled names look nice. You evaluate it by asking how much probability it assigns to the real data, using likelihood, log likelihood, and especially negative log likelihood as the training and comparison signal. This also explains why smoothing matters in count-based models: if some character transition was never seen, assigning it zero probability can break evaluation and make the model too brittle.

The first limit of raw count tables is context size. With only one previous character, predictions are weak, but as soon as you try to condition on more characters, the number of possible contexts grows explosively, so a literal lookup table becomes impractical21. That is the key reason to move from memorizing counts to learning a function that maps context to next-token probabilities.

An embedding table is the first useful step in that direction. Instead of treating each character identity as an isolated row in a huge table, you map each discrete token to a learned vector, then feed those vectors into an MLP to predict the next character. The benefit is not just compression: similar contexts can share statistical structure, so the model can reuse what it learns across examples rather than starting from scratch for every exact character combination3.

This is also the bridge toward larger sequence models. The MLP still uses a fixed amount of context, but it already replaces explicit counting with learned representations and a forward pass that outputs logits over the next token14. Later sequence models such as RNNs extend the same next-token idea to variable-length histories by reusing the same computation across time, which is what makes sequence modeling more general56.

5 principles from Andrej Karpathy

1

Model language as next-token prediction

Start with the task definition: given previous tokens, predict a probability distribution over the next one. Once you frame the problem this way, both training and generation become clear: train on real sequences, then sample repeatedly from the predicted distribution to produce new sequences.

“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 →
2

Evaluate with negative log likelihood, not vibes

A model should be judged by how much probability it gives to real data, not by a few hand-picked samples. Negative log likelihood gives a consistent way to compare models and exposes issues like zero-probability transitions in unsmoothed count tables.

3

Expect raw context tables to blow up

A count table works for tiny context, but it scales badly because every extra position multiplies the number of possible contexts. If you find yourself enumerating all exact token combinations, that is a sign you need a learned model instead.

“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 →
4

Use embeddings to share statistical strength

Embeddings turn discrete symbols into learned vectors, so the model can generalize across similar inputs. In practice, this means learning from one context can help with another, instead of storing totally separate statistics for every exact case.

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

See the MLP as a step toward sequence models

A fixed-window MLP already teaches the core machinery: represent tokens, compute logits, optimize the prediction objective, and sample from the resulting probabilities. More advanced sequence models keep the same goal while extending how context is represented.

“We can actually operate over sequences: sequences at the input/output or both at the same time.”
CS231n Winter 2016 Lecture 10, 2017

In his own words

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

1
“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 →
2
“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 →
3
“…you can transfer knowledge through that embedding, and you can generalize in that way.”
Building makemore Part 2: MLP, online lecture, 2020
4
“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 →
5
“We can actually operate over sequences: sequences at the input/output or both at the same time.”
CS231n Winter 2016 Lecture 10, 2017
6
“This is what allows us to process, you know, variably sized inputs.”
CS231n Winter 2016 Lecture 10, 2017

Reflect

  1. Think of a prediction problem in your own work. What are the 'tokens,' what is the context, and what would next-token prediction look like there?
  2. Where have you used a lookup-table style solution that works for small cases but fails to scale as combinations grow? What would an embedding-like representation buy you?
  3. If you judged your current model or system by likelihood on real data rather than by a few examples that look good, would your conclusion change?

Check your understanding

1. Why does a simple count table become a poor approach as you increase context length in character prediction?
2. What is the main advantage of embeddings over a raw lookup table of exact contexts?
3. What is the best way to evaluate a tiny language model according to this chapter?

Go deeper

Search every transcript
Open the full search page →