temperature sampling

When a language model finishes a forward pass it does not hand you a word. It hands you a probability distribution over its entire vocabulary, and *decoding* is the policy you use to turn that distribution into the next token. The model is the same every time; the sampler is the dial you actually control at inference. Two people running the identical model can get a crisp deterministic answer or a wild creative riff purely by choosing different decoding settings.\n\n**Greedy decoding takes the single most likely token at every step.** It is fast, reproducible, and locally optimal, but it is also myopic: always grabbing the top token can walk the model into bland, repetitive, or degenerate loops because the globally best sentence sometimes starts with a locally second-best word.\n\n**Beam search widens the search by keeping the *k* most probable partial sequences alive at once**, extending all of them and pruning back to the top *k* each step. It reliably finds higher-probability full sequences and is the workhorse of machine translation and summarization, where there is roughly one correct answer. For open-ended generation it tends to produce safe, generic text and can collapse the beams onto near-duplicates.\n\n**Temperature reshapes the distribution before you sample from it** by dividing the logits by a scalar T inside the softmax. T below 1 sharpens the distribution and concentrates mass on the top tokens (more conservative); T above 1 flattens it and hands probability to the long tail (more diverse and more error-prone). T = 1 leaves the model's native distribution untouched, and T approaching 0 collapses back to greedy.\n\n**Top-k sampling truncates the candidate set to the k highest-probability tokens**, renormalizes, and samples from just those. It kills the long tail of absurd tokens, but a fixed k is a blunt instrument: when the model is confident, k is too generous, and when it is unsure, k is too stingy.\n\n**Top-p (nucleus) sampling truncates by cumulative probability mass instead of by count** — it keeps the smallest set of tokens whose probabilities sum to p (say 0.9) and samples from that. The candidate set breathes: it shrinks to a couple of tokens when the model is certain and expands to dozens when it is not, which is why top-p is the most widely used default for chat and creative generation. In practice teams stack a modest temperature with top-p and leave the rest alone.\n\n| Method | Determinism | Diversity | Best for | Failure mode |\n|---|---|---|---|---|\n| Greedy | Deterministic | None | Short factual answers, code | Repetition, blandness |\n| Beam search (k) | Deterministic | Low | Translation, summarization | Generic, near-duplicate beams |\n| Temperature (T) | Stochastic | Tunable | Global creativity knob | High T -> incoherence |\n| Top-k | Stochastic | Medium | Cutting the absurd tail | Fixed k mis-sizes the set |\n| Top-p / nucleus | Stochastic | Adaptive | Chat, open-ended text | Very high p -> drift |\n\n```svg\n\n \n Decoding: from one distribution to the next token\n The model outputs P(next token). The sampler decides what to do with it.\n\n \n 1 - Temperature reshapes the softmax\n \n \n vocabulary (sorted by logit)\n \n \n \n \n \n T < 1 sharp\n \n \n \n \n \n \n T > 1 flat\n softmax(z / T): low T concentrates mass, high T spreads it\n\n \n 2 - Top-k and top-p truncate the tail\n \n \n \n \n \n \n \n \n \n \n \n \n \n top-k = 3 (keep 3, fixed count)\n \n \n top-p = 0.9 (keep until mass = 0.9, adaptive)\n gray/black tokens are discarded, then the rest renormalized\n\n \n \n The pipeline, in order\n logits -> divide by temperature T -> softmax -> truncate (top-k or top-p) -> renormalize -> sample\n\n \n Want reliable / factual?\n low T, greedy or small top-k,\n or beam search\n\n \n Want creative / varied?\n T around 0.8-1.0 with\n top-p around 0.9\n\n \n Key intuition\n temperature reshapes the curve;\n top-k / top-p clip its tail\n\n```\n\nThe mistake most people make is treating decoding as an afterthought — a single "temperature" slider to nudge when output feels off. It is better understood as the interface between a fixed probabilistic model and the text you actually want. Greedy and beam search ask *what is most probable*; temperature, top-k, and top-p ask *how much of the model's uncertainty should I let through, and in what shape*. Read decoding through a shape-the-distribution lens rather than a pick-the-best-word lens, and every parameter stops being a magic number and becomes a deliberate statement about how much risk you want the model to take on each token.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account