DL0082 LLM Temperature

What is LLM temperature?

Answer

Temperature is a single positive scalar that divides the model’s logits before the softmax at every decoding step, so it reshapes the next-token distribution without touching the model weights. At T = 1 you sample from exactly the distribution the model was trained to produce; values below 1 sharpen that distribution toward the highest-scoring token, and values above 1 flatten it toward uniform over the vocabulary. The transformation is monotone in the logits, so temperature never changes which token is ranked first, only how much probability the gaps between logits are worth. The practical effect is a variance knob: low temperature gives repetitive but stable text, high temperature gives diverse text with a much fatter tail of implausible tokens. Because it is applied independently at every step of an autoregressive loop, a small per-token change in tail mass compounds across a long generation.

(1) Logit Rescaling, Not Reranking: dividing all logits by the same T preserves their order, so the argmax token is invariant and only the sampling probabilities move.
(2) The Two Limits: as T approaches 0 sampling collapses to greedy decoding, and as T grows the distribution approaches uniform; frameworks special-case T = 0 as argmax because the division itself is undefined.
(3) Tails Gain Disproportionately: temperature is a power transform of the base distribution, so unlikely tokens change by the largest relative factor; with logits (4, 3, 2, 1) the top token falls from 0.644 to 0.455 at T = 2 while the weakest rises from 0.032 to 0.102.
(4) Compounding Across Steps: the model conditions on its own samples, so one low-quality token drawn from the inflated tail can steer every subsequent token, which is why high temperature degrades long generations faster than short ones.
(5) Not A Truthfulness Knob: lowering T reduces variance, not error; if the mode is wrong, greedy decoding returns the wrong answer with total confidence.

Grouped bar chart of next-token probabilities for four tokens with logits 4.0, 3.0, 2.0 and 1.0 at temperatures 0.5, 1.0 and 2.0, showing the top token falling from 0.865 to 0.455 and the weakest token rising from 0.002 to 0.102

Figure 1: The same four logits under three temperatures. Entropy grows from 0.66 bits at T = 0.5 to 1.80 bits at T = 2, and the weakest token gains roughly 47x probability across that range while the top token loses less than half.

Temperature never acts alone in a real decoder. The usual pipeline is logits → temperature → top-k or top-p truncation → renormalize → sample, and the ordering matters: because temperature runs first, raising it inflates the tail that nucleus sampling then has to cut, so the same p = 0.95 threshold admits a larger candidate set than it did at T = 1. That is why “high temperature plus top-p” is not a safety net, and why many production defaults pair a moderate temperature with a fixed truncation instead of pushing either one hard. A separate but easily confused use of the same formula is temperature scaling for calibration, where a single T is fit on held-out data to correct an overconfident classifier; there the goal is matching predicted confidence to observed accuracy, not generating diverse text, and the fitted value is a property of the model rather than a user-facing creativity dial.

Mathematical Formulation:
p_i(T) = \frac{\exp(z_i/T)}{\sum_{j=1}^{V} \exp(z_j/T)}
p_i(T) \propto p_i(1)^{1/T}
i^{*} = \arg\max_j z_j
\lim_{T \to 0^{+}} p_{i^{*}}(T) = 1
H(T) = -\sum_{i=1}^{V} p_i(T) \log_2 p_i(T)

Where:

  • p_i(T) is the sampling probability of vocabulary token i at temperature T, and z_i is that token’s raw logit from the final linear layer.
  • V is the vocabulary size and j indexes every candidate in the softmax denominator, so all probabilities are renormalized after rescaling.
  • T > 0 is the temperature: values below 1 sharpen the distribution, values above 1 flatten it, and T = 1 is an identity operation.
  • The second line shows temperature is a power transform with exponent 1/T applied to the base distribution, which is the compact reason tail tokens move by the largest relative factor.
  • i^{*} is the argmax token, and the fourth line is the greedy limit; implementations therefore branch to argmax at T = 0 instead of evaluating the ratio.
  • H(T) is the entropy of the next-token distribution in bits; it increases monotonically with T and is bounded above by \log_2 V.
Two panels versus temperature from 0.05 to 5: left panel shows entropy in bits rising monotonically from near 0 toward the 2-bit uniform limit and passing 1.37 bits at T equals 1; right panel shows top-token probability falling from 1 to about 0.33 while the mass of the two weakest tokens rises from near 0 to about 0.40

Figure 2: Sweeping T over the same four logits. Entropy is monotonically increasing and saturates at \log_2 4 = 2 bits, while the tail mass of the two weakest tokens more than doubles between T = 1 and T = 2.

KnobTemperatureTop-kTop-p (Nucleus)
What it changesRescales all logits; every token keeps nonzero probabilityKeeps the k highest-scoring tokens, zeroes the restKeeps the smallest set whose mass reaches p, zeroes the rest
Adapts to context?No, the same scalar at every step regardless of confidenceNo, a fixed candidate countYes, the set shrinks when the model is confident
Typical failure modeToo low: loops and boilerplate. Too high: off-topic or invented tokensFixed k is too tight on flat distributions and too loose on peaked onesA high temperature inflates the tail, so the same p admits far more junk
When to reach for itGlobal variance control, and generating diverse samples for self-consistency votingCheap hard cap on the candidate set, useful as a safety floorDefault truncation for open-ended text, paired with a modest temperature

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *