LLM Basics
Lesson 14 of 20Training and Sampling

Sampling Strategies: Temperature, Top-k & Top-p

Once trained, generating text means repeatedly asking "given everything so far, what's the distribution over the next word?" and picking one. How you pick matters a lot:

  • Temperature rescales the logits before softmax. Low temperature sharpens the distribution toward the top choice (more predictable, more repetitive); high temperature flattens it (more diverse, more prone to nonsense).
  • Top-k simply deletes every candidate outside the k most likely, then renormalizes, a safety rail that stops the model from ever picking something wildly unlikely, no matter how flat the distribution gets.
  • Top-p (nucleus sampling) does something subtly different: instead of a fixed count of candidates, it keeps the smallest set of top candidates whose probabilities add up to at least p, then renormalizes just those.

"The cat sat on the ___", adjust temperature and sampling cutoff, then sample

mat
0.65
floor
0.14
couch
0.09
chair
0.03
table
0.02
roof
0.00
bed
0.05
rug
0.01

8 of 8 candidates survive this cutoff.

Push temperature toward 0 and hit Sample repeatedly, you'll get "mat" almost every time, since the distribution collapses onto the single highest-scoring word. Push it up toward 2 with a high top-k and you'll start seeing genuinely surprising picks. This exact tradeoff, coherent-but-boring versus diverse-but-risky, is why every text-generation setting exposes knobs like these.

Switch the widget above to top-p and watch the "candidates survive" count as you adjust temperature. That count is the real difference from top-k: with top-k fixed at, say, 4, the model always considers exactly 4 words, whether it's extremely confident or genuinely torn between many options. Top-p adapts, when one word dominates the distribution, a single candidate alone can cross a high p threshold; when the distribution is flatter, top-p automatically opens up to more candidates to reach the same cumulative probability. That's why top-p and top-k are usually described as solving the same problem (cut off the unlikely tail) with different definitions of "cut off."

Both the training loop and the sampling function, in plain Python, using the exact same numbers as the widgets above:

Python

Your turn

Implement top_p_filter(probs, p): keep the smallest set of highest-probability candidates whose cumulative probability reaches at least p, zero out the rest, then renormalize so the kept probabilities sum to 1.

Python

Your turn

Implement greedy_decode(logits): return the index of the highest-scoring logit, exactly what temperature_sample converges to as temperature approaches 0.

Python

As temperature approaches 0, sampling becomes equivalent to…

Every sample above costs the same to compute no matter how long the conversation gets so far, but real generation has a performance wrinkle worth understanding before this arc wraps up: KV caching, next.