How does Andrej Karpathy sample from a trained language model?

Andrej Karpathy said you sample by starting with an initial context, embedding it, projecting it through the network, and turning the logits into probabilities with softmax. Then you draw the next index with multinomial sampling, shift the context window forward, and repeat until the sequence is complete. That is how the model generates new strings one character at a time.

“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.”
— Andrej Karpathy, Building makemore Part 2: MLP, online lecture, 2020 · at 1:13:26
Andrej Karpathy: Andrej Karpathy on MLPs for Language Modeling: makemore

Key takeaways

  • Use softmax to convert logits into a probability distribution.
  • Sample the next token probabilistically, not by always taking the max.
  • Update the context window after each generated token.

Context

At the end of the 2020 makemore lecture, Karpathy demonstrates how to generate names from the trained model. The quote above is taken word for word from the recording's transcript.

In the transcript

…uh, pretty good chunks of it. And this paper also has quite a few ideas for improvements that you can play with. So, all of those are knobs available to you, and you should be able to beat this number. I'm leaving that as an exercise to the reader, and, uh, that's it for now, and I'll see you next time. Before we wrap up, I also wanted to show

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.

um, of logits, and that just basically exponentiates the logits and makes them sum to one. And similar to cross-entropy, it is careful that there's no overflows. Once we have the probabilities, we sample from them using torch.multinomial to get our next index, and then we shift the context window to append the index and record it. And then we can just, um, decode all the integers to strings and…

Read the full transcript of Building makemore Part 2: MLP, online lecture

Topics

More questions about Andrej Karpathy

All Andrej Karpathy answers · All Andrej Karpathy recordings

How this answer was made: the quote is checked word for word against the transcript of the recording and confirmed as the speaker's own words; the summary above it was drafted with AI from that transcript and quality-checked. Methodology and corrections.

Search every transcript
Open the full search page →