Decoding Temperature › Worked Example: Step-by-Step Temperature Calculation
Language AI HandbookAll chapters Decoding Temperature › Worked Example: Step-by-Step Temperature CalculationOne section · Sections in this chapter4 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Before implementing temperature in a full generation loop, let's walk through a complete numerical example from raw logits to sampled token. This trace is the kind of calculation that happens millions of times during a single generation call: once per token position, across every possible token in the vocabulary.
Suppose our model has just processed the prompt "The weather in Paris is" and the final linear layer has output the following logits for five candidate tokens:
- "beautiful": 3.5
- "rainy": 2.0
- "cold": 1.8
- "unusual": 0.9
- "enormous": -0.5
We will apply temperature and trace every step.
Step 1: Divide all logits by the temperature.
Notice that the logit differences have all been amplified by a factor of . The gap between "beautiful" and "rainy" was in the original logits, and now it is in the scaled logits.
Step 2: Exponentiate the scaled logits.
Step 3: Compute the normalization constant (denominator).
Step 4: Divide each exponentiated value by to get probabilities.
Step 5: Verify that probabilities sum to 1.
The small rounding error is expected; exact computation would give exactly 1.0.
Step 6: Sample from the distribution.
We now draw one token index according to these probabilities. With , "beautiful" has an 81.1% chance of being selected. If we had used instead, the probability of "beautiful" would be lower, and "rainy" and "cold" would have had more of a fighting chance. The temperature has sharpened the distribution, amplifying the model's confidence in "beautiful" beyond what the raw logits at would have implied. Notice that "enormous" has only a 0.3% chance: semantically, "The weather in Paris is enormous" is nonsensical, and the model's logits correctly reflect this, assigning a strongly negative logit. Even at , the probability of "enormous" would only rise to about 2%: a sensible result.
Let's confirm these calculations with code:
# Numerical trace of temperature scaling
example_tokens_trace = ["beautiful", "rainy", "cold", "unusual", "enormous"]
example_logits_trace = torch.tensor([3.5, 2.0, 1.8, 0.9, -0.5])
T_trace = 0.7Step 1: Scaled logits (z_i / T): beautiful : 3.50 / 0.7 = 5.000 rainy : 2.00 / 0.7 = 2.857 cold : 1.80 / 0.7 = 2.571 unusual : 0.90 / 0.7 = 1.286 enormous : -0.50 / 0.7 = -0.714 Step 2: Exponentiated (exp(z_i / T)): beautiful : 148.413 rainy : 17.412 cold : 13.085 unusual : 3.617 enormous : 0.490 Step 3: Normalization constant Z = 183.016 Step 4: Final probabilities: beautiful : 0.8109 (81.1%) rainy : 0.0951 (9.5%) cold : 0.0715 (7.1%) unusual : 0.0198 (2.0%) enormous : 0.0027 (0.3%) Step 5: Sum check = 1.000000
The computed values confirm our hand calculation. Notice how dramatically the temperature has concentrated probability on "beautiful": it captures over 81% of the probability mass despite its logit being only 1.5 units above the second-highest token. The exponential nature of softmax magnifies even moderate logit differences into large probability gaps.