How temperature shapes what a language model says
A language model does not pick words, it assigns probabilities. Temperature decides how boldly it samples from them. Drag the slider and see.
In short
A language model never "decides" on a word. For every position it produces a score (a logit) for each possible next token, turns the scores into probabilities, and then samples one. The temperature setting reshapes those probabilities before sampling: low values make the model nearly deterministic, high values make it adventurous and sometimes incoherent.
How it works
- The model reads the text so far and outputs one logit per token in its vocabulary.
- The logits are divided by the temperature
T. - A softmax converts them into probabilities that sum to 1.
- One token is drawn at random according to those probabilities.
With T → 0 the highest-scoring token always wins (greedy decoding). With T = 1 you sample from the model's raw distribution. With T > 1 the distribution flattens and unlikely tokens get a real chance.
Under the hood
The formula is p_i = exp(z_i / T) / Σ_j exp(z_j / T). Dividing by a small T stretches the gaps between logits, so the top token dominates; a large T shrinks the gaps. In practice temperature is often combined with top-k or top-p (nucleus) sampling, which cut off the long tail of very unlikely tokens before sampling — a fix for the degenerate text that pure high-temperature sampling can produce.
Try it yourself
The demo below uses seven made-up logits for the prompt "The capital of France is". Move the slider and press Draw 50 samples. Then try the same idea in a few lines of Python:
import numpy as np
def sample(logits, temperature=1.0):
z = np.array(logits) / temperature
p = np.exp(z - z.max())
p /= p.sum()
return np.random.choice(len(p), p=p)
print([sample([6.2, 3.4, 3.1, 2.2], 0.3) for _ in range(10)]) # mostly 0
print([sample([6.2, 3.4, 3.1, 2.2], 2.0) for _ in range(10)]) # much more varied
Terminal
Play with it
Sources
What should happen next with this?
One click per article, no account needed. “Go deeper” feeds the research queue.