Top-P Sampling
What is Top-P Sampling?
In one sentence: Top-P sampling means the model picks from the smallest group of likely tokens whose chances add up to P, such as 90%.
Instead of a fixed number like Top-K, Top-P adapts. When the model is sure, the group might be just one or two tokens. When it is unsure, the group gets bigger. It is also called nucleus sampling.
Σ Quick math / rule of thumb: top_p = 0.9 keeps only the smallest set of top tokens whose probabilities sum to 90%, dynamically trimming the long tail of unlikely words.
What does Top-P Sampling look like?
With top-p 0.9, if "Paris" alone has a 95% chance after "The capital of France is," the model only considers "Paris."
Why does Top-P Sampling matter?
Top-P is a common, flexible way to balance creativity and good sense.
Top-P Sampling vs. Temperature
Temperature rescales how flat or sharp all token probabilities are, while Top-P (nucleus sampling) cuts off the unlikely tail once top choices add up to probability P.
| Dimension | Top-P Sampling | Temperature |
|---|---|---|
| Mechanism | Keeps smallest top set summing to P (e.g., 0.9) | Sharpens (<1) or flattens (>1) logits |
| Effect on long tail | Hard-truncates low-probability tail tokens | Lowers tail odds but keeps rare words possible |
| Practical advice | Keep near 0.9–0.95 as a safety net | Tune one primarily (e.g., T=0.2 for code, 0.8 for writing) |
Last updated · Fact-checked by The AI Math
Linking to this page from a lesson or article? <a href="https://getaimath.com/dictionary/top-p-sampling/">Top-P Sampling, explained simply</a> (The AI Math Dictionary)