๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #285

Temperature (Sampling)

Temperature is a sampling hyperparameter that controls how "confident" or "random" an LLM's next-token choices are โ€” a single number with an outsized effect on generated text's character.

Formula

\[ P(w_i) = \frac{e^{z_i/T}}{\sum_j e^{z_j/T}} \]

This is exactly the softmax formula from Softmax Function, with every logit \(z_i\) divided by a temperature \(T\) before exponentiating. \(T=1\) reproduces standard softmax exactly.

What Different Temperature Values Do

TemperatureEffect on the DistributionGenerated Text Character
\(T \to 0\)Sharpens toward the single highest-probability token (approaches argmax)Highly deterministic, repetitive, "safe"
\(T = 1\)Unchanged โ€” the model's raw learned distributionBalanced
\(T > 1\)Flattens the distribution, making less-likely tokens relatively more probableMore random, varied, sometimes less coherent

Numerical Example

Logits \([2.0, 1.0, 0.1]\) at \(T=1\) (standard softmax, from Softmax Function): \([0.659, 0.242, 0.099]\). At \(T=0.5\) (dividing logits by 0.5, i.e. doubling them to \([4.0,2.0,0.2]\) before softmax): the distribution sharpens considerably, e.g. approximately \([0.843, 0.140, 0.017]\) โ€” the top token becomes much more dominant. At \(T=2\) (halving the logits to \([1.0,0.5,0.05]\)): the distribution flattens toward more uniform, e.g. approximately \([0.475, 0.288, 0.237]\).

Code

import torch
import torch.nn.functional as F

logits = torch.tensor([2.0, 1.0, 0.1])

for T in [0.5, 1.0, 2.0]:
    scaled_probs = F.softmax(logits / T, dim=0)
    print(f"T={T}: {scaled_probs}")
# T=0.5: sharper, more confident distribution
# T=1.0: the model's original, unmodified distribution
# T=2.0: flatter, more uniform, more "random" distribution

When to Use Which Temperature

Task TypeTypical Temperature
Factual Q&A, code generation, mathLow (0โ€“0.3) โ€” favors reliability and determinism
Creative writing, brainstormingHigher (0.7โ€“1.2) โ€” favors variety and novelty

Common Mistakes

  • Setting temperature very high expecting purely "more creative" output without downside โ€” excessive temperature can degrade coherence and factual reliability, not just increase variety.
  • Confusing temperature with top-k or top-p sampling โ€” temperature reshapes the entire probability distribution's sharpness; top-k/top-p (next two notes) instead restrict which tokens are even eligible to be sampled from, a distinct and complementary mechanism.

Interview Relevance

Q: "Why would you use a low temperature for a code-generation task but a higher one for creative writing?" Code generation typically benefits from reliability and correctness โ€” a low temperature sharpens the distribution toward the model's most confident (and typically most likely to be syntactically/logically correct) predictions. Creative writing benefits from variety and novelty โ€” a higher temperature flattens the distribution, giving less-likely but potentially more interesting or original word choices a real chance of being sampled.

Practice Question

As \(T \to 0\), what does the sampling process approach โ€” a random draw, or a deterministic choice? Explain using the softmax formula.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Temperature (Sampling) โ€“ FAQs

Quick answers about learning Temperature (Sampling) in Deep Learning.

This free note from CodingNow 2.0 explains Temperature (Sampling) in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Temperature (Sampling), is 100% free with no signup required.
With focused practice, most students grasp Temperature (Sampling) in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now