Generative AI
Top-k Selection
O(n log n) time to sort n candidates (or O(n) with a selection algorithm) · O(k) space for the shortlist.
The idea, in plain English
Picture a leaderboard for 'what word comes next'. Instead of only ever crowning a single number-one word, or weighing every possible word in the language, you just keep the top handful of contenders, say the top 3, and ignore the long tail of unlikely options. This is top-k selection: a filtering step language models use before choosing the next word. You keep only the k highest-scoring candidates, then decide among just those.
How it works
- 1Score every candidate, meaning every possible next word, with a number. A higher number means the model thinks that word is more likely to come next.
- 2Sort the candidates by score, highest first.
- 3Keep only the top k candidates and throw away the rest, no matter how good or bad they were.
- 4Turn the kept candidates' scores back into probabilities, for example with softmax (see Softmax & Temperature), using only this smaller shortlist. Then pick from among them.
When you'd use it
Use this to control text generation so a model cannot wander into extremely unlikely, nonsensical words. It is a common safety net used alongside temperature, often set to something like k=40 or k=50 in real systems.
Common beginner mistakes
- Don't set k too small, like k=1. That is the same as always picking the single highest-scoring word every time, which can make text repetitive and robotic.
- Don't set k too large, close to the whole vocabulary. Top-k then stops doing anything useful, since you are barely filtering out any of the unlikely candidates.
- Don't forget to re-score, or renormalize, the probabilities after cutting candidates out. The leftover scores no longer add up to 100% unless you recompute them over just the shortlist.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
All candidates and scores: cat=5 dog=4 fish=3 bird=2 ant=1 eel=0
Keeping top 3 by score (top-k filtering): cat dog fish
Renormalized probabilities among the top 3:
cat: 0.67
dog: 0.24
fish: 0.09
Selected token (highest probability): catNot sure this is the right topic? See the learning paths → or where this leads →