Decoding and constraints / 6 of 9
Sampling and temperature
What you’ll learn
- Explain the main idea of “Sampling and temperature” in your own words.
- Trace a concrete example and identify the assumptions behind it.
- Apply the principle in a practice problem and recognize a common failure.
The idea, intuitively
Decoding chooses tokens from model scores. Temperature rescales logits before softmax: lower positive values concentrate probabilities; higher values flatten them. It changes sampling behavior, not the knowledge stored in weights.
Separate what a model learned during training from what an application supplies at runtime. Generation settings change how outputs are selected, while architecture and precision affect resource needs. The labs use small transparent calculations so you can understand mechanisms without downloading a model or calling an API.
Under the hood
Compute softmax(z/T) using a maximum-subtraction trick for numerical stability. Top-k and top-p can further restrict candidates. Greedy decoding selects a maximum-score token; APIs may implement zero temperature differently.
Lower temperature does not guarantee factual accuracy or complete determinism across systems.
A worked example
With logits [2,1,0], lowering temperature makes the first token more likely. The educational distribution below uses fixed toy logits, not an LLM.
At T = 1, softmax assigns those candidates about 67%, 24%, and 9%. At T = 0.5, dividing by the smaller temperature doubles the gaps between scores, yielding about 87%, 12%, and 2% after rounding. The ranking stays the same, but the leading candidate is sampled more often.
How to use this way of thinking
Compare one setting at a time using fixed inputs and a written quality rubric. Record the checkpoint, tokenizer, decoding options, and hardware. Report failures and resource costs alongside successful examples. Do not infer factual reliability from confidence, fluency, or parameter count alone.
See the mechanism
Predict what happens to the leading candidate at 0.3 and 1.8. Draw samples at each setting and compare the percentages with the observed counts.
Opening the experiment…
Try it yourself
Compare the maximum-token probability at T=0.3 and T=1.5.
Key takeaways
- The probability distribution becomes flatter
- Lower temperature does not guarantee factual accuracy or complete determinism across systems.
- Check the assumptions with a small example before applying this idea to a larger system.
Check your understanding
Use feedback to learn. Quiz scores and lesson completion are tracked separately.
Go to lesson completionFurther reading
Original papers and reference material for this school. External resources may be in English and require internet access.
Hugging Face: generation configuration (opens a new tab)llama.cpp project (opens a new tab)Finish this lesson
Mark this lesson complete when you’re ready to continue.