Temperature Sampling
Implement temperature-scaled sampling from logits.
Temperature controls the randomness of predictions:
- Divide logits by temperature T
- Apply softmax:
Higher T -> more uniform (random), lower T -> more peaked (deterministic).
Since we need deterministic output for testing, instead of sampling, return the probability distribution after temperature scaling.
Input format:
- Line 1: Temperature T (float)
- Line 2: Space-separated logits (floats)
Output: The probability distribution after temperature scaling, as a list of floats rounded to 4 decimal places.
Example
1.0 2.0 1.0 0.1
[0.6590, 0.2424, 0.0986]
Tis a positive temperature andlogitsare raw scores , one per token. The output is one probability per token, in the same order, summing to 1.- With the scaled scores are unchanged: . Shifting every score down by the largest, , gives . The ratios survive because the common factor cancels between numerator and denominator.
- Exponentiating: , , , with total .
- Dividing each by the total: , , , which gives
[0.6590, 0.2424, 0.0986]rounded to 4 decimal places. - Notice how reshapes this. At the gaps double to and the top token rises to ; at the gaps halve and it falls to . The ranking never changes, only how sharp it is.
Constraints
- T > 0
- Use softmax with numerical stability (subtract max)
- Round each probability to 4 decimal places