Keep the top k
Cut off the long tail of unlikely words before picking the next one.
Top-k sampling keeps only the k most likely next words and throws the rest away, so a model never picks something wild. The survivors are then rescaled so they add up to 1 again.
Write topK(probs, k): probs is an array of probabilities and k is a whole number from 1 up. Return a new array the same length where the k largest stay (divided by their total) and every other entry is 0. On a tie, the earlier index wins a place. If k is larger than the array, keep everything.
Call show(kept) with your answer and the stage draws it as bars.
Example: topK([0.1, 0.6, 0.3], 2) returns about [0, 0.667, 0.333].
Tests you can see
topK([0.1,0.6,0.3], 2)[0,0.6666666666666667,0.33333333333333337]topK([0.2,0.5,0.3], 1)[0,1,0]topK([0.25,0.75], 5)[0.25,0.75]
More tests are hidden. The server runs them all before a time counts.
The idea behind this, in a two-minute live pageCall show() while your code works and the run plays back here.
Tab indents · Cmd/Ctrl+Enter runs