What does an LLM actually predict, and what does temperature change?
Question
A support chatbot has written "Your order has been" and must choose the next token. The model gives four tokens these raw scores, called logits:
| token | logit |
|---|---|
| shipped | 2.0 |
| late | 1.0 |
| lost | 0.0 |
| cancelled | -1.0 |
Turn the logits into probabilities. Then do it again at temperature 0.5 and at temperature 2. What does the model actually output at each step? What do greedy decoding and sampling each pick here?
Show hints (3)
- "SCORES TO PROBABILITIES"
Use softmax: raise e to the power of each logit, then divide each result by their sum. The results add up to 1.
- "WHERE TEMPERATURE GOES"
Temperature divides every logit before softmax. Dividing by 0.5 doubles the gaps between logits. Dividing by 2 halves them.
- "TWO WAYS TO PICK"
Greedy decoding takes the most likely token every time. Sampling draws a token at random, with the probabilities as weights.
Show checked explanation
At each step an LLM outputs a probability for every token in its vocabulary. Temperature divides the logits before softmax: below 1 it sharpens the distribution, above 1 it flattens it, and the order never changes. Greedy decoding always picks "shipped". Sampling usually picks it, but sometimes picks another token.
import math import random tokens = ["shipped", "late", "lost", "cancelled"] logits = [2.0, 1.0, 0.0, -1.0] # the model's raw scores def softmax(scores, temperature=1.0): scaled = [s / temperature for s in scores] exps = [math.exp(s - max(scaled)) for s in scaled] # no overflow return [e / sum(exps) for e in exps] for t in (0.5, 1.0, 2.0): probs = softmax(logits, t) print(f"T={t}:", " ".join(f"{w} {p:.3f}" for w, p in zip(tokens, probs))) print("greedy:", tokens[logits.index(max(logits))]) rng = random.Random(8) print("sampled:", " ".join(rng.choices(tokens, softmax(logits), k=8)))
T=0.5: shipped 0.865 late 0.117 lost 0.016 cancelled 0.002 T=1.0: shipped 0.644 late 0.237 lost 0.087 cancelled 0.032 T=2.0: shipped 0.455 late 0.276 lost 0.167 cancelled 0.102 greedy: shipped sampled: shipped lost shipped late shipped shipped cancelled shipped
Code run on CPython 3.12 against the example above. Checked by an editor other than the author.
Walkthrough
Softmax turns logits into probabilities. At temperature 1, "shipped" gets e² / (e² + e¹ + e⁰ + e⁻¹) ≈ 0.644.
| temperature | shipped | late | lost | cancelled |
|---|---|---|---|---|
| 0.5 | 0.865 | 0.117 | 0.016 | 0.002 |
| 1 | 0.644 | 0.237 | 0.087 | 0.032 |
| 2 | 0.455 | 0.276 | 0.167 | 0.102 |
At temperature 0.5 the logits become 4, 2, 0 and -2. The gaps double, so the top token takes more of the probability. At temperature 2 the gaps halve, and the distribution flattens. The order never changes: "shipped" stays first at every temperature above 0. As the temperature goes towards 0, its probability goes towards 1. That limit is greedy decoding.
Greedy decoding always picks "shipped", so the same prompt gives the same text. Sampling draws from the distribution. With the seed in the code, 5 of 8 draws are "shipped", and "late", "lost" and "cancelled" appear once each. That is why a chatbot can answer the same question in different words.
Writing a reply repeats this step. The model adds the chosen token to its input and predicts the next one. This is called autoregressive generation. A token is often a whole common word, but rare words are split into several pieces.
A higher temperature gives more varied text, and more chances to pick a poor token.
Common mistakes
- Saying the model outputs the next word. It outputs probabilities for every token in its vocabulary. A separate decoding step picks one.
- Thinking a higher temperature makes the model know more or reason better. It only reshapes the same distribution. The weights and the order of tokens stay the same.
- Putting temperature 0 into the formula. That divides by zero. Treat 0 as greedy decoding: as the temperature falls towards 0, all the probability moves to the highest logit (split evenly if several tie).
- Expecting greedy decoding to find the most likely whole reply. It takes the best token at each step, but a weaker first token can lead to a likelier sentence. Beam search keeps several options.
Follow-up questions
- "What are top-k and top-p sampling?" Both cut off the unlikely tail before sampling. Top-k keeps the k most likely tokens. Top-p keeps the fewest top tokens whose probabilities add up to at least p. Then you rescale those to sum to 1 and sample. At temperature 1, p = 0.8 keeps "shipped" and "late": 0.644 + 0.237 = 0.881.
- "Where do fine-tuning and RLHF fit in?" Pretraining teaches the model to predict the next token on a huge amount of text. Fine-tuning continues training on examples of the behaviour you want, such as good support replies. RLHF then trains it on human judgements of which reply is better. All of them change the weights, and so the logits. Decoding works the same way afterwards.
What a strong answer covers
Says the model outputs a next-token distribution and writes text one token at a time. Computes softmax with temperature on small logits, explains that temperature changes the shape but not the order, and contrasts greedy decoding with sampling, top-k and top-p.
Open in interactive practice deck →