We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Decoding Strategies step 4 of 8
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.
Loading visualization…
Nothing accepted yet. When a submission passes, the code that passed shows up
here, one entry per mode.
Stuck?
PyTorch reference solution
Sign in to attempt this problem and reveal the reference solution.