Top-p
Top-p, also called nucleus sampling, is a text-generation strategy where, instead of fixing the number of candidate tokens, you fix the cumulative probability mass you're willing to sample from. Tokens are sorted by probability from highest to lowest, and you keep adding them to the "nucleus" until their cumulative probability reaches the threshold p. Only tokens inside that nucleus are eligible to be sampled next. A top-p of 0.9 means you're drawing from the smallest set of tokens that together account for 90% of the model's confidence.
Think of it like planning a weekend trip: rather than always choosing from a fixed list of five destinations, you pick whichever set of options collectively covers 90% of your enthusiasm—some weeks that's two places, other weeks it's six, depending on how spread out your interest is.
from transformers import pipeline
generator = pipeline("text-generation", model="gpt2")
output = generator(
"Researchers discovered that",
do_sample=True,
top_p=0.92,
max_new_tokens=80
)
print(output[0]["generated_text"])
When the model is very confident, the nucleus shrinks automatically; when probability is spread across many tokens, the nucleus widens—adapting to context in a way that a fixed top-k cannot.
Top-p sampling complements Temperature, which scales the distribution before nucleus selection, and is often compared with Top-k Sampling, which uses a fixed candidate count. Both strategies affect the final output of Softmax probability calculations.
You'll encounter top-p whenever you call a generative API and want consistent quality across both predictable factual completions and open-ended creative writing, often setting it alongside Temperature for fine-grained control.