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.
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
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 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”.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Pre-training vs fine-tuningTrain once on a mountain of raw text, then briefly on a small labelled set for one job. Beginner
- The attention mechanismA word scores every other word, turns the scores into shares, and blends what they offer. Intermediate
- Retrieval-augmented generation (RAG)Find the relevant passages first, then let the model answer from them. Intermediate
Spotted a mistake on this page?