What is the role of temperature in token sampling?
This tests your understanding of how to control the creativity and randomness of a language model's output. A great answer explains that temperature is a divisor applied to the model's logits before the softmax function. Low temperature makes the output more deterministic by sharpening the probability distribution, while high temperature increases randomness by flattening it. A common red flag is vaguely saying it 'controls randomness' without explaining the underlying softmax mechanism.
What's really being asked
This tests your understanding of the mechanism for controlling the randomness of an LLM's output. Interviewers want to see that you can connect a user-facing parameter (temperature) to the underlying math (modifying logits before the softmax function) and explain the practical trade-offs between deterministic and creative outputs.
The full answer
• Definition: Temperature is a hyperparameter, typically ranging from 0.0 to 2.0, that scales the logits before they are passed into the softmax function to calculate token probabilities.
• Mechanism: The core operation is probabilities = softmax(logits / temperature). It directly manipulates the values that determine the final probability distribution for the next token.
• Low Temperature (e.g., 0.2): Dividing by a number less than 1 makes the logits larger, exaggerating the differences between them. This creates a 'sharper' or 'peakier' probability distribution where the model is very likely to select the highest-probability token. A temperature of 0 is commonly handled as greedy decoding by an API, while low positive temperatures can still sample different outputs.
• High Temperature (e.g., 1.5): Dividing by a number greater than 1 makes the logits smaller, shrinking the differences between them. This creates a 'flatter' distribution, giving lower-probability tokens a higher chance of being selected. This leads to more random, creative, or surprising outputs. As temperature approaches infinity, the distribution approaches uniform randomness.
• Practical Use Cases: Use low temperature (~0.1-0.5) for factual, predictable tasks like code generation, summarization, or closed-domain Q&A. Use high temperature (~0.8-1.2) for creative tasks like story writing, brainstorming, or creating marketing copy.
The mistakes people make
• Vagueness: Simply stating "it makes the output more or less random" without explaining the connection to logits and the softmax function. Confusion with other methods: Incorrectly equating temperature with top-p (nucleus) sampling. Temperature reshapes the entire probability distribution, while top-p truncates* it by considering only the most probable tokens that sum to a certain probability mass. Incorrect mechanism: Claiming temperature is a parameter of* the softmax function itself, rather than a divisor for its inputs.
Follow-up the interviewer might ask
• "How does temperature sampling differ from top-k sampling? When would you use one over the other?" • "Describe a scenario where you might use a high temperature but a low top-p. What would be the expected output?"
A concrete example
Imagine a model's logits for the next three tokens are [2.0, 1.0, 0.1].
• At T=1.0: The probabilities are [0.66, 0.24, 0.10]. The most likely token is strongly favored.
• At T=0.5: The scaled logits become [4.0, 2.0, 0.2]. The probabilities get much sharper: [0.86, 0.12, 0.02]. The model becomes very deterministic, almost certainly picking the first token.
• At T=2.0: The scaled logits become [1.0, 0.5, 0.05]. The probabilities flatten to [0.50, 0.30, 0.20]. The other tokens now have a much more reasonable chance of being sampled, leading to more varied output.
Interview question
What is the primary effect of increasing the temperature parameter in a language model's token sampling?
- a.A. It flattens the probability distribution of tokens, leading to more diverse and creative outputs.Correct
- b.B. It sharpens the probability distribution, making the output more deterministic and focused.
- c.C. It filters out tokens below a certain probability threshold, similar to top-p sampling.
- d.D. It increases the overall likelihood of the most probable token being selected.
Why? this is the answer
Increasing the temperature parameter divides the logits by a larger number, which reduces the differences between them, resulting in a flatter probability distribution and thus more random, diverse, and creative outputs. Option B describes the effect of *decreasing* temperature, which sharpens the distribution.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #llm
- #generative ai
- #sampling
- #softmax
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on llm — each one lists the topics its interview covers.
See open roles