We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
medium
research
Nucleus (Top-P) Sampling
Implement the nucleus filtering step of Top-P sampling from “The Curious Case of Neural Text Degeneration” (Holtzman et al., 2020).
Nucleus sampling restricts the vocabulary to the smallest set of tokens whose cumulative probability exceeds a threshold p. This produces more natural text than top-k or pure sampling.
Given:
-
probs: shape(vocab_size,)— probability distribution over vocabulary -
p: float — cumulative probability threshold
Steps:
- Sort probabilities in descending order
- Compute cumulative sum
- Find the smallest set where cumsum >= p
- Zero out all probabilities outside this nucleus
- Renormalize
Output: Tensor of shape (vocab_size,) — filtered and renormalized distribution.
Hints
nucleus-sampling
top-p
holtzman-2020
text-generation
decoding
Sign in to attempt this problem and view the solution.