What is Top-k or top-p (nucleus) sampling?

Imagine you're picking your favorite candies from a giant bowl, but instead of taking all of them, you only choose the top 5 that look the best to you right now. That’s like Top-k sampling: it picks the k most likely next words to continue a sentence.

How does it work?

When something like a chatbot is writing, it doesn’t just pick one word, it thinks about many possible ones. With Top-k sampling, it narrows down its choices to only the top k most probable words, and then randomly picks one from that group. It's like choosing between your 5 favorite candies instead of all 100 in the bowl.

What is Top-p (nucleus) sampling?

Now imagine you’re not just picking the top 5 candies, you're picking all the candies that together make up 80% of the sweetness in the bowl. That’s like Top-p sampling, also called nucleus sampling: it includes all words whose total probability adds up to a certain percentage (p), and then randomly picks one from that group.

This way, it keeps things interesting, not too strict, not too wild, just right! Imagine you're picking your favorite candies from a giant bowl, but instead of taking all of them, you only choose the top 5 that look the best to you right now. That’s like Top-k sampling: it picks the k most likely next words to continue a sentence.

How does it work?

When something like a chatbot is writing, it doesn’t just pick one word, it thinks about many possible ones. With Top-k sampling, it narrows down its choices to only the top k most probable words, and then randomly picks one from that group. It's like choosing between your 5 favorite candies instead of all 100 in the bowl.

What is Top-p (nucleus) sampling?

Now imagine you’re not just picking the top 5 candies, you're picking all the candies that together make up 80% of the sweetness in the bowl. That’s like Top-p sampling, also called nucleus sampling: it includes all words whose total probability adds up to a certain percentage (p), and then randomly picks one from that group.

This way, it keeps things interesting, not too strict, not too wild, just right!

Take the quiz →

Examples

  1. A child picks the top 3 most likely words to continue a sentence.
  2. Choosing the best 2 out of 10 possible answers for a math problem.
  3. Selecting the top 5 fruits from a basket with the most ripeness.

Ask a question

See also

Discussion

Recent activity