Example 1
Arguments: [
[
0,
0.6931471805599453,
1.0986122886681098
],
1,
2
]
Expected result: [
0,
0.4,
0.6000000000000001
]Retain IDs 2 and 1 with unnormalized weights 3 and 2, yielding [0,0.4,0.6].
Decoding and sampling · Medium
Problem 24 of 50 · Python and NumPy
Return a vocabulary-wide probability vector after temperature scaling and top-k filtering. Retain exactly k tokens ranked by decreasing original logit, breaking ties by smaller token ID. Excluded tokens receive zero probability.
For temperature > 0, apply stable softmax to retained logits divided by temperature. At temperature = 0 return a one-hot vector at the smallest-ID maximum, independent of k.
def top_k_distribution(logits, temperature, k):Arguments: [
[
0,
0.6931471805599453,
1.0986122886681098
],
1,
2
]
Expected result: [
0,
0.4,
0.6000000000000001
]Retain IDs 2 and 1 with unnormalized weights 3 and 2, yielding [0,0.4,0.6].
Arguments: [
[
2,
2,
1
],
0,
3
]
Expected result: [
1,
0,
0
]Temperature zero selects greedy ID 0 because it is the smaller of the tied maxima.