← All labs

Temperature and sampling

What does temperature change, and what does it not?

Back to journey scene 7
The model produces scores (logits). Softmax turns them into shares of the total, and a sampling rule picks one token. Temperature divides the scores before softmax. At temperature 0 the rule is greedy: always the top token (no division by zero is performed).

Toy: "Tomorrow the weather will be …"

Toy calculation· 6 made-up scores
Scores (logits): fixed by the model
  1. sunny3.1
  2. cloudy2.4
  3. rainy1.9
  4. windy1.2
  5. snowy0.3
  6. purple-1.5
Shares at T = 1.00, and draws
  1. sunny49.5%29/50
  2. cloudy24.6%11/50
  3. rainy14.9%6/50
  4. windy7.4%0/50
  5. snowy3.0%4/50
  6. purple0.5%0/50

Entropy 1.302 nats. Shares sum to 100.0%. Same seed and settings give the same draws.

Moderate temperature: several tokens are drawn. The scores themselves never changed.

Predict first

Does increasing the temperature guarantee a better answer?

Predict first

Does changing the temperature change the model's weights?

The formula and the greedy limit
p_i = exp(z_i / T) / Σ_j exp(z_j / T). As T → 0 the largest score takes all the probability, so T = 0 is implemented directly as “choose the argmax”, never by dividing by zero. Top-k and top-p further restrict which tokens can be drawn.