Temperature Sampling
Ask a model for the next word and it does not hand you one — it hands you a probability for every token in its vocabulary. Temperature sampling is the dial that decides how faithfully you follow that distribution when you pick.
Dividing before the softmax
The model's raw scores are divided by a temperature T before the softmax turns them into probabilities. The effect is purely one of contrast:
- T below 1 sharpens the distribution — likely tokens get likelier, and as T approaches zero sampling collapses into greedy decoding.
- T = 1 leaves the model's own distribution untouched.
- T above 1 flattens it, handing real probability mass to tokens the model scored as unlikely.
Choosing a value, and what breaks
The right setting follows the task, not the model. Pulling a date out of a contract or completing a line of code wants 0.0–0.3: there is one right answer and invention is a defect. A dialogue turn or a set of headline variants wants 0.7–1.0, because a deterministic chat partner returns the same bland sentence every time and falls into repetition loops. Push past that and syntax frays, invented words appear, and confident-sounding false statements arrive — temperature redistributes probability, it never adds knowledge. One practical trap: it rescales the entire tail, including tokens that are simply wrong, which is why it is normally paired with top-k or nucleus sampling to cut the tail off first. In Hugging Face's generate() the parameter is silently ignored unless sampling is actually switched on.
Temperature sampling draws the next token from a model's output distribution after dividing the logits by a temperature T, which rescales how peaked that distribution is. Values below 1 concentrate probability on likely tokens and approach greedy decoding; values above 1 flatten it and admit unlikely ones. It is the primary control over the fidelity-versus-diversity trade-off in generation: set too low it yields repetitive, generic text, set too high it yields incoherence.
Picture a cook with a trusted recipe. Follow it to the letter and every meal is identical — reliable, and a little dull. Improvise instead and dinner gets more interesting, with the occasional disaster.
Temperature is that dial for software writing text. The program always has several possible next words in mind, some more likely than others. A low temperature makes it stick to the safest choice almost every time, which is what you want when accuracy matters. A higher one lets it take chances, so the writing feels more varied and more human. Turn it too high and the sentences stop making sense altogether.