AI & machine learning / Temperature and sampling

Temperature and sampling

How a language model picks its next word: a weighted draw from a list of probabilities, a temperature that sharpens or flattens the list, and two cut-offs that trim its tail.

Beginner7 steps~6 min
Candidate — a possible next word and its odds
Temperature — sharpens or flattens the odds
Draw — the random pick of one word
Cut-off — top-k or top-p trims the tail
// THE SHORT VERSION

In 30 seconds

A language model does not choose one next word. It gives every possible word a probability, and the word you see is drawn at random according to those odds.

Temperature reshapes the odds first. A low value makes the likeliest word dominate, so answers repeat; a high value evens the odds, so answers vary and stray more.

Top-k and top-p remove the unlikely tail before the draw: top-k keeps a fixed number of words, top-p keeps as many as it takes to cover a set share of the probability.

Temperature and sampling, step by step

STEPS01 / 07

Five candidate words stand in for a vocabulary of tens of thousands of tokens. T = 0 itself would divide by zero, so software treats it as “always take the top token”; even then tiny numerical ties can vary.

STEP 01 / 07 A score for every next word
THE MATH
p = e^score ÷ total scores: 3, 2, 1.5, 0.5, 0 p: 58% 21% 13% 5% 3%
  1. Step 1: A score for every next word. A language model does not output a word. For the text so far it outputs a score for every word it knows, and softmax turns those scores into probabilities that add up to 1. After “The sky is”, our toy model gives “blue” 58% and still leaves 3% for “falling”.
  2. Step 2: Sampling: a weighted draw. The next word is drawn at random, in proportion to the probabilities: lay them end to end from 0 to 1, pick a random number, and see whose segment it lands in. Over 100 draws about 58 give “blue” and 3 give “falling”. Always taking the top word instead is called greedy decoding.
  3. Step 3: Low temperature sharpens. Temperature T divides every score before softmax. With T = 0.5 the scores double, so the gaps between them double too, and the exponent turns bigger gaps into a far bigger lead: “blue” rises from 58% to 84%. As T approaches 0 the top word takes everything, which is greedy decoding.
  4. Step 4: High temperature flattens. With T = 2 every score is halved, the gaps shrink and the probabilities move towards each other: “blue” falls to 39% and “falling” triples to 9%. The order of the words never changes, only how much the likely ones dominate. A very high T approaches a uniform draw over all words.
  5. Step 5: Top-k: keep the k best. Very unlikely words are often poor choices, so the tail is usually cut before the draw. Top-k keeps only the k most likely words and rescales them to add up to 1. With k = 2, “blue” and “clear” share everything, 73% to 27%. The cut is the same size whether the model is sure or unsure.
  6. Step 6: Top-p: keep a share. Top-p, or nucleus sampling, keeps the smallest set of top words whose probabilities add up to at least p. With p = 0.9 that takes three words here. The set adapts: on the sharp T = 0.5 distribution two words already reach 95%, so only two would be kept. When the model is unsure, more candidates stay in.
  7. Step 7: Predictable or varied. These settings trade repeatability for variety. Near T = 0 the same prompt gives the same safe answer, which suits facts and code. Higher settings give different, less expected text each run, which suits brainstorming, but the odds of an odd word such as “dark” or “falling” climb from 8% at T = 1 to 20% at T = 2.
VALUES SO FAR
scores3 2 1.5 0.5 0model
T = 158 21 13 5 3 %odds
r = 0.71····draw
T = 0.5····sharp
T = 2····flat
top-k 2····cut
top-p .9····cut
rare····T 1 → 2

Spotted a mistake on this page?