Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-291

Top-k Sampling Threshold

Problem

Return the top-k sampling threshold for a probability distribution: the k-th largest probability in probs.

Tokens with probability below this threshold are excluded by top-k sampling. Return 0.0 when k is not positive, k exceeds the number of probabilities, or probs is empty.

Hint

Sort descending and read the probability at position k - 1.

Test cases

case 1
in: [[0.5,0.3,0.2],2]
expected: 0.3
case 2
in: [[0.1,0.4,0.4,0.1],1]
expected: 0.4
case 3
in: [[0.25,0.25,0.25,0.25],3]
expected: 0.25
case 4
in: [[0.5,0.5],0]
expected: 0
case 5
in: [[0.5,0.5],5]
expected: 0
Starter codePython
def top_k_sampling_threshold(probs, k):
    # Your code here
    pass

5 test cases · run your code against them in the editor