principles.fyi · the brain · concept
top-k sampling
Keep only the few most likely next words, then randomly pick one — but favoring the likelier ones.
keep top k tokens by probability, renormalize so they sum to 1, then sample
The model first gives every possible next word a raw score (a "logit"). A softmax turns those scores into probabilities that add up to 1. Top-k sampling then keeps only the k highest-scoring words (say k=50), throws away the rest, and rescales the survivors' probabilities so they again add up to 1. It then picks one at random — but it's a *weighted* draw, not a coin flip: a word at 60% is chosen far more often than one at 2%, so the model still usually says something sensible. It works because the long discarded tail is mostly nonsense, so cutting it keeps the output coherent while still leaving room for variety. This is the final step that turns the predicted scores into one actual chosen word: bigger k lets in more long-shot words (more surprising), smaller k stays near the top few (safer and more predictable).
Appears in
- From scores to a word — and the loop Transformers, ELI5 · pt 7