TorchedUp
ProblemsPremium
TorchedUp
Top-p (Nucleus) SamplingEasy
ProblemsPremium

Top-p (Nucleus) Sampling

Top-p (nucleus) sampling selects the smallest set of tokens whose cumulative probability exceeds threshold p, then samples from them. Unlike top-k (fixed count), top-p adapts: it picks fewer tokens when the model is confident and more when uncertain.

Steps:

  1. Apply softmax to logits
  2. Sort probabilities descending
  3. Compute cumulative sum
  4. Keep tokens up to and including the one that crosses threshold p
  5. Renormalize and return in original token order

Signature: def top_p_sampling(logits, p)

  • logits: (vocab_size,) — unnormalized scores
  • p: float — cumulative probability threshold (0 < p ≤ 1)
  • Returns: (vocab_size,) — renormalized probability distribution

Math

Asked at

Python (numpy)0/3 runs today

Test Results

○very peaked distribution keeps only 1 token
○flat distribution keeps all tokens to reach p=0.9
○p=1.0 equals plain softmax🔒 Premium
○output is a probability distribution: sums to 1
○output is non-negative
○preserves argmax (nucleus always contains the max-logit token)
Advertisement