Skip to content
MAMasterAI Now

大语言模型阅读约 4 分钟

What Is Top-p Sampling?

这篇答案尚未翻译成中文,因此你正在阅读英文原文。

简要回答

Top-p sampling, also called nucleus sampling, limits generation to the smallest group of tokens whose probabilities add up to at least p. That group is the “nucleus”. The probabilities inside it are rescaled to sum to one, and the next token is sampled from that shortened list. Because the size of the nucleus changes with how confident the model is, top-p adapts better than a fixed top-k cutoff.

How nucleus sampling works

A language model gives every token a probability. Most of that mass usually sits on a handful of sensible continuations, while thousands of tokens share a long, thin tail. Sampling from the full distribution occasionally picks a tail token that derails the sentence; always picking the top token produces repetitive text.

Top-p takes a middle path. Sort the tokens by probability, add them up from the top until the running total reaches p, and keep exactly that set. Discard the rest, renormalise the kept probabilities so they sum to one, and sample.

nucleus = smallest set S with Σᵢ∈S pᵢ ≥ p, then p′ᵢ = pᵢ / Σⱼ∈S pⱼ
The nucleus can be one token or hundreds, depending on how peaked the model’s distribution is at that step.
Notep = 1 keeps everything

With p = 1 the nucleus is the entire vocabulary, so top-p has no effect and you are sampling from the raw distribution (after temperature). Lower values cut more of the tail.

A worked example

Take five candidate tokens with probabilities [0.50, 0.25, 0.15, 0.06, 0.04]. They already sum to one. With p = 0.8, the cumulative totals are 0.50, 0.75, 0.90 — so the nucleus is the first three tokens, and the last two are removed entirely.

TokenOriginal pCumulativeKept at p = 0.8Renormalised
A0.500.50Yes0.556
B0.250.75Yes0.278
C0.150.90Yes0.167
D0.060.96No—
E0.041.00No—

Raising p to 0.95 keeps four tokens, because the cumulative total only reaches 0.95 after D. Renormalising by 0.96 gives 0.521, 0.260, 0.156 and 0.063. A single parameter has changed both the candidate set and the relative odds inside it.

Top-p versus top-k and temperature

Top-k always keeps the same number of tokens. When the model is certain, that wastes probability on nonsense; when it is uncertain, k may be too small to allow a valid alternative. Top-p adapts: a peaked distribution yields a tiny nucleus, a flat one yields a larger nucleus.

ControlWhat it limitsBehaviour
TemperatureHow sharp the distribution isReshapes all probabilities
Top-kNumber of candidatesFixed count regardless of confidence
Top-pCumulative probability massAdaptive candidate count

Because temperature is usually applied first, it changes the ordering of cumulative mass and therefore which tokens fall inside the nucleus. Tuning both at once makes it hard to attribute a change in output quality to one setting; fix one and vary the other when you evaluate.

Practical trade-offs and limitations

Low p (for example 0.1–0.5) is useful for factual extraction and code, where an unlikely token is usually a bug. High p (0.9–1.0) preserves diversity for creative writing and for generating multiple candidate answers. Many providers expose a default near 1.0, which effectively disables truncation unless you change it.

Top-p is not a factuality control. It removes improbable tokens, not incorrect claims: the most probable sentence can still be false, and a nucleus can contain only wrong options. It also cannot recover a token that should have been likely but was assigned low probability by the model — truncation only ever removes candidates.

If you need reproducible outputs for testing, top-p and temperature are the wrong levers; use a seed where the API offers one and pin the model version. The answer on why ChatGPT gives different answers explains the remaining sources of variation.

Choose a nucleus and sample from it

A fixed next-token distribution. Move the p slider to include or exclude tokens, watch the nucleus renormalise, and sample a token from what remains.

Nucleus at p = 0.80 — kept mass 0.91
A
73.1%kept
B
26.9%kept
C
0.0%cut
In nucleus
2 / 3
Mass kept
0.91
Last draw
—

Lower p to 0.5 and token C is cut entirely — its probability is reassigned to A and B. Raise p to 1 and nothing is cut, so top-p stops constraining the output at all.

Runs entirely in your browser — nothing you change here is sent anywhere.

在课程中深入学习

这些阶段用完整的讲解和实时模拟讲解同一个概念。

自己动手试试

  • LLM API Cost Calculator →

    Turn a token count and a traffic guess into a real monthly bill. Set the prompt size, the answer size and how many requests you expect, and every priced model is compared side by side.