Temperature, top-k and top-p: the decision that changes the text more than switching models
The model returns a distribution. Turning that into words is your choice.
Landed here from LinkedIn? This is chapter 07 of the course GPT-1 and GPT-2, taken apart. It reads on its own; if you want the beginning, it is chapter 00.
The model does not choose the next word. You do. There is a widespread misunderstanding. The model returns a score for each of the 50,257 tokens in the vocabulary and stops there. Choosing is the job of a separate algorithm that runs afterwards and that you control. Temperature, top-k, top-p: three knobs that change the result more than switching models. And measured on GPT-2 small, 'The capital of France is' has 1,503 tokens in its nucleus of doubt, with the favourite worth only 8.5%.
The three points
- Greedy always takes the most likely and falls into repetition. Pure sampling takes anything and falls into nonsense. Everything else is the middle ground.
- Temperature is the boldness knob: it flattens or sharpens the distribution before sampling. Top-k cuts at a fixed count; top-p cuts where the probability mass says.
- Beam search exists and is almost never used in open generation. The post explains why.
What the chapter covers · 9 sections · 9 min
- What the model really returns
- Greedy: always the most likely
- Pure sampling, and why it fails too
- Temperature: the boldness knob
- Top-k: fixed cutoff
- Top-p (nucleus sampling): adaptive cutoff
- Repetition penalty, and the ready-made recipes
- Beam search — and why it is not used here
- In the lab
All chapters of the course · GPT-1 and GPT-2, taken apart
- 00 · The seven fossils: why GPT-1, GPT-2 and RoBERTa are a historical cross-section, not a list
- Glossary · Course glossary: what token, layer and model mean in AI, and in the rest of IT
- 01 · Why GPT-2 sees neither letters nor words, and what that costs in Portuguese
- 02 · Embeddings: from a meaningless integer to a vector that knows king and queen are related
- 03 · Attention: the single operation that replaced twenty years of recurrent networks
- 04 · Twelve heads instead of one, and the single line of code that separates GPT from RoBERTa
- 05 · Why GPT-2 needs 48 floors to answer, and what happens inside each one
- 06 · What scale really buys: from 124 million to 1.5 billion, measured
- 07 · Temperature, top-k and top-p: the decision that changes the text more than switching models
- 08 · Perplexity: the one number that says what the model learned, without anecdotes
- 09 · GPT-1 to GPT-2: same size, half the perplexity. Four decisions explain it
- 10 · RoBERTa: when the advance is training better what already existed
- 11 · Encoder vs decoder: one line of code and every consequence that descends from it
- 12 · The detector: how a language model becomes a classifier, and why this one should decide nothing
- 13 · Reading GPT-2's original code: 174 lines against everything you learned
- 14 · Serving models: from function call to HTTP server, and what production does on top
- 15 · The lab guide: every command, every script, and everything that was measured
- 16 · The ceiling of this path: LeCun's argument, read from inside GPT-2

Writes the GPT-2 course and runs the Antivision experiment, measuring every run before publishing the number. The LinkedIn posts start here and end in the chapters.
Follow on LinkedIn →