A model gives next-token probabilities (0.50, 0.20, 0.15, 0.10, 0.05) for tokens A–E. What is the sampling distribution under top-k with k=2, and under nucleus (top-p) sampling with p=0.8? Why is nucleus sampling often preferred to a fixed k?

Sequence models and transformers, Decoding
Level L2 Elementary. Length Medium. Nature Th+Mat Both.

What's being asked

Whether you can apply truncation and renormalisation and explain the adaptive size of the nucleus.

Key idea

Top-k keeps a fixed number of tokens. Top-p keeps the smallest set whose cumulative probability is at least p. Both then renormalise the kept probabilities.

Steps

  1. Top-k, k=2: keep A and B, with total mass 0.70. Renormalise: (0.50,0.20)/0.70=(0.714, 0.286). C, D and E get probability 0.
  2. Top-p, p=0.8: cumulative sums are 0.50, 0.70, 0.85. The smallest set reaching 0.8 is {A,B,C}.
  3. Renormalise by 0.85: (0.50,0.20,0.15)/0.85=(0.588, 0.235, 0.176).
  4. When the model is confident (for example P(top)=0.95), the nucleus may contain one token, so output is nearly deterministic. When the model is unsure and probabilities are flat, the nucleus holds many tokens.
  5. A fixed k cannot adapt: it may include many unlikely tokens in a confident step, or cut off reasonable tokens in an uncertain step.
  6. Both methods cut off the unreliable low-probability tail, which reduces incoherent text compared with pure sampling.

Common mistakes

How long your answer should be

Worked solution; about 3 minutes.

Read this

Previous in Sequence models and transformers: An encoder gives hidden states h1=(1,0), h2=(0,1) and h3=(1,1). The decoder state is s=(2,1). Using Luong dot-product attention, compute the attention weights and the context vector. How does Bahdanau (additive) attention differ?
Next in Sequence models and transformers: In BERT's masked language modelling, 15% of tokens are chosen for prediction. Of those, 80% become [MASK], 10% become a random token and 10% stay unchanged. What fraction of all tokens are in each case? Why not simply use [MASK] every time?