TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

33. Top-p (Nucleus) Sampling

Easy

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

pi​=∑j​exp(zj​)exp(zi​)​Np​=min{S⊆V:i∈S∑​pi​≥p}p^​i​={pi​/∑j∈Np​​pj​0​i∈Np​otherwise​

Asked at

NumPy

import numpy as np

 

def top_p_sampling(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?