Sub topic 5
How sampling and temperature shape variability
This chapter assumes you know that a model predicts the next token (covered earlier), that you have used a chat assistant, and that you can read short Python. It explains why asking the same question twice can give different answers, and what that means for spotting made-up content.
From scores to a choice
At each step the model produces a raw score, called a logit, for every token in its vocabulary. A softmax function turns those scores into probabilities that add up to 1. The system then picks one token from that distribution and repeats the process for the next token.
Picking is the step that introduces variability. If the system always took the highest-probability token, the output would be the same every time. This is called greedy decoding. Most chat products instead sample, which means they draw a token at random, weighted by the probabilities.
What temperature does
Temperature is a number that rescales the logits before softmax. Each logit is divided by the temperature.
- A low temperature (below 1) makes the gaps between scores larger. The top candidate gets most of the probability.
- A temperature of 1 leaves the model's own distribution unchanged.
- A high temperature (above 1) shrinks the gaps. Probability spreads toward less likely tokens.
This code shows the effect on three candidate tokens.
import math
def softmax(logits, temperature=1.0):
scaled = [x / temperature for x in logits]
m = max(scaled)
exps = [math.exp(x - m) for x in scaled]
total = sum(exps)
return [e / total for e in exps]
logits = [2.0, 1.0, 0.5]
print(softmax(logits, temperature=1.0))
print(softmax(logits, temperature=0.5))At temperature 1.0 the top token gets about 0.63 of the probability. At 0.5 it gets about 0.84. The ordering never changes. Temperature only changes how strongly the model favors its first choice.
Many systems also offer top-p or top-k settings. These cut off the unlikely tail of the distribution before sampling. They limit how far a high temperature can wander, but they work on the same principle.
Variability is not the cause of hallucination
It is easy to conclude that lowering temperature to 0 removes made-up content. It does not. Temperature controls how the model chooses among its own predictions. It does not add knowledge or check facts.
- If the model's most likely continuation is a wrong date, greedy decoding gives that wrong date every time. The answer is consistent and still false.
- Higher temperature increases the chance of picking a less likely token. That can produce odd or invented details, especially in long outputs where one unusual choice shapes everything after it.
- Low temperature reduces random drift, but it can hide uncertainty. You see one confident answer instead of the spread of possibilities.
Using variability as a signal
The spread of outputs can still be useful. When the model knows a fact well, the right answer holds most of the probability, and repeated samples agree. When it is guessing, probability is split across several plausible answers, and samples differ. The next chapters on self-consistency build on this idea. A small preview:
from collections import Counter
def agreement(answers):
normalized = [a.strip().lower().rstrip(".") for a in answers]
value, count = Counter(normalized).most_common(1)[0]
return value, count / len(normalized)
samples = ["1889", "1889.", "1887", "1889", "1901"]
print(agreement(samples))This prints ('1889', 0.6). Three of five samples agree, so the answer is plausible but not solid.
Common errors
- Treating temperature 0 as a "truth mode".
- Judging reliability from one sample. A single response shows only one draw from the distribution.
- Assuming high temperature is only for creative work and harmless elsewhere. For factual tasks it raises the chance of invented details.
- Reading agreement as proof. Samples can agree because they share the same training gap.
Exercise
A model at temperature 0 gives the same wrong birth year in ten separate runs. A colleague says the repetition shows the year is correct. What is wrong with that reasoning, and what check would you do instead?
Show answer
At temperature 0 the model picks its highest-probability token each time, so repetition is expected whether the answer is right or wrong. Agreement here only shows the model's top prediction is stable. To check the year, compare it with a primary source such as a biography or official record. If you want to test uncertainty, sample at a moderate temperature (for example 0.7 to 1.0) several times and see whether the answers differ.
Exercise
Using the softmax function above with logits [2.0, 1.0, 0.5], would temperature 2.0 give the top token more or less probability than temperature 1.0? Why?
Show answer
Less. Dividing by 2.0 shrinks the gaps between the scores, so the distribution flattens and the top token's share falls below about 0.63.