📘
Temperature Sampler
Given a list of logits (raw scores) for vocabulary words and a temperature value, apply temperature scaling and compute the resulting probability distribution using softmax.
Temperature scaling divides each logit by the temperature before applying softmax: pi=∑jezj/Tezi/T
Input format:
- Line 1: Comma-separated word:logit pairs (e.g., "cat:2.0,dog:1.0,fish:0.5")
- Line 2: Temperature T (float)
Output: Each word and its probability (rounded to 4 decimal places), sorted by probability descending, one per line as "word probability".
Example:
Input:
cat:2.0,dog:1.0,fish:0.5 1.0
Output:
cat 0.6285 dog 0.2312 fish 0.1402
Reasoning:
Step 1: Divide logits by temperature (T=1.0) cat: 2.0/1.0 = 2.0, dog: 1.0/1.0 = 1.0, fish: 0.5/1.0 = 0.5
Step 2: Apply softmax e^2.0 ≈ 7.389, e^1.0 ≈ 2.718, e^0.5 ≈ 1.649 Sum ≈ 11.756
Step 3: Normalize cat: 7.389/11.756 ≈ 0.6285 dog: 2.718/11.756 ≈ 0.2312 fish: 1.649/11.756 ≈ 0.1402
Constraints:
- Temperature T > 0
- Apply softmax after dividing logits by T
- Round probabilities to 4 decimal places
- Output sorted by probability descending (ties: alphabetical)
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.