AI Dictionary of Terms

Top-p Sampling (Nucleus Sampling)

A decoding strategy for language models that dynamically filters the vocabulary to the smallest set of tokens whose cumulative probability exceeds a threshold $p$, balancing output creativity and coherence.

The Simple Version

A smart way for an AI to choose its next word. Instead of always picking the most likely word, or picking randomly from the whole dictionary, Top-p looks at the top contenders until their combined probability hits a certain percentage (like 90%), and then randomly picks one from just that small, high-quality group.

Detailed Explanation

In autoregressive generation, pure greedy search leads to repetitive text, while pure random sampling produces gibberish. Top-p sampling solves this by sorting the probability distribution of the next token in descending order. It then accumulates the probabilities until the sum reaches $p$ (e.g., 0.9). The model then samples exclusively from this “nucleus” of likely tokens. Unlike Top-k sampling (which always picks the top $k$ tokens regardless of their actual probabilities), Top-p dynamically adjusts the size of the candidate pool based on the model’s confidence.

Key Characteristics

Business Context

Real-World Analogy

Ordering food at a restaurant. Greedy search is always ordering your absolute favorite dish. Top-p sampling is looking at the menu, picking the top 3 dishes you’d actually be happy eating, and then letting your mood decide which one to order today.

Code Example

# Conceptual: Top-p Sampling logic
import numpy as np

def top_p_sampling(probabilities, p=0.9):
    # Sort probabilities in descending order
    sorted_indices = np.argsort(probabilities)[::-1]
    sorted_probs = probabilities[sorted_indices]
    
    # Calculate cumulative sum
    cumulative_probs = np.cumsum(sorted_probs)
    
    # Find the cutoff index where cumulative prob exceeds p
    cutoff_index = np.searchsorted(cumulative_probs, p, side='right')
    
    # Mask out probabilities outside the nucleus
    nucleus_probs = sorted_probs[:cutoff_index + 1]
    nucleus_probs /= nucleus_probs.sum() # Renormalize
    
    # Sample from the nucleus
    chosen_index = np.random.choice(len(nucleus_probs), p=nucleus_probs)
    return sorted_indices[chosen_index]

Common Misconceptions

Sources & Further Reading