Ultralytics YOLO27:
Back to Ultralytics Glossary

Nucleus Sampling

Learn how nucleus sampling (top-p) selects tokens for AI text generation, and compare it with top-k, greedy decoding, and temperature to tune output diversity.

Nucleus sampling, also called top-p sampling, is a way to choose the next token during AI text generation. Instead of always picking the most likely token, the model builds a shortlist whose probabilities add up to at least a chosen threshold, then randomly samples from that shortlist. The shortlist changes with each prediction: it can be small when one continuation is obvious and larger when several continuations are plausible.

How Nucleus Sampling Works#

A language model assigns a probability to every possible next token. These probabilities are commonly calculated from the model’s output scores using softmax, which makes the values add up to one. Nucleus sampling sorts the tokens from most to least likely, includes them until their cumulative probability reaches the threshold p, excludes the rest, and samples from the included tokens. Here, p stands for probability mass, not the probability of one individual token. PyTorch’s softmax documentation explains the probability conversion, while IBM’s decoding guide describes the cumulative cutoff. (docs.pytorch.org)

Suppose four possible next tokens have probabilities of 0.50, 0.25, 0.15, and 0.10. With top_p=0.80, the first two total only 0.75, so the third is included too, bringing the shortlist to 0.90. The fourth is excluded. The model then samples among the first three in proportion to their probabilities after normalization; it does not give each an equal chance. The cutoff may exceed p because the token that crosses it remains in the shortlist. Google Cloud’s explanation of top-p describes the same token-selection rule. (cloud.google.com)

This selection happens again after every generated token. As the sentence develops, the model computes a new distribution and therefore a new nucleus.

Top-p vs. Top-k, Greedy Decoding, and Temperature#

These settings affect different parts of generation:

  • Top-k sampling keeps a fixed number of candidates, such as the five most likely tokens. Top-p keeps enough candidates to reach a probability threshold, so its shortlist has no fixed size.
  • Greedy decoding always selects the single most likely token. It is predictable but offers none of the variation that sampling allows.
  • Temperature changes how strongly the model favors high-probability tokens before selection. Lower temperatures concentrate probability on leading choices; higher temperatures spread it more widely. Top-p instead sets a cumulative cutoff on the resulting distribution.

Implementations can combine these controls, but their interaction makes results harder to interpret. When experimenting, change one setting at a time. The OpenAI Chat Completions parameter reference describes top_p and recommends adjusting either it or temperature rather than both at once. (platform.openai.com)

Where It Matters in Practice#

In a customer-support chatbot, nucleus sampling can allow several natural phrasings of a routine answer without drawing from every unlikely continuation. For example, an assistant explaining a return policy might vary its opening sentence while preserving the policy details supplied in its prompt. Sampling does not verify those details: a fluent answer can still be wrong, so factual checks and appropriate grounding remain necessary.

In image captioning, a vision-language model may have several reasonable ways to describe the same street scene. Top-p can vary the wording of captions while filtering out low-probability token choices. A visual pipeline might first use Ultralytics YOLO26 for object detection, then provide detected objects as context to a caption generator. YOLO’s detection step does not use nucleus sampling to choose its object labels; the setting applies to the downstream language-generation step. The TensorFlow image-captioning tutorial illustrates how visual input and a text decoder fit together. (ibm.com)

Trying Top-p in a Documented Workflow#

The Ultralytics LLM interface accepts request arguments for a compatible language-model endpoint. After installing ultralytics[llm] and setting OPENAI_API_KEY, this example requests a short response with top_p=0.9:

from ultralytics import LLM

# Use an endpoint that supports the top_p request argument.
llm = LLM("gpt-4.1-mini", api="chat.completions")

prompt = "Describe a city bus in one friendly sentence."
response = llm(prompt, top_p=0.9)

message = response.choices[0].message
print(message.content)

The code leaves token selection to the language-model provider. Running it again may produce different wording, but top_p=0.9 is not a promise that every response will differ. Check the selected model’s supported parameters before applying the same setting elsewhere.

Choosing a Useful Setting#

Start with the model’s default, then compare outputs on representative prompts. Lower top_p if responses wander into unlikely wording; consider a higher value if they are needlessly repetitive. Judge the result by the task—not by variety alone. For customer answers or captions, review factual accuracy and whether important details are present. A narrower shortlist can reduce some unusual continuations, but it cannot make unsupported statements true. IBM’s guidance on generating accurate output likewise treats decoding choices as only one part of a grounded generation workflow. (ibm.com)

Explore solutions

Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning