Temperature

By the Syncfusion DevTutorials team · Last updated

How temperature reshapes the model's chances before a token is picked: low temperature is sharp and safe, high temperature is flat and creative.

In the last lesson, you learned that temperature 0 means "always pick the top token". But temperature is more than just an on-off switch. It is a dial you can turn. Changing it changes how much the model is willing to pick the less likely tokens.

The main idea: temperature divides every logit by a number before softmax. A low temperature makes the top token even more likely. A high temperature spreads the chances out more evenly.

A Real-World Example

Think about a group of friends picking a film to watch together.

When everyone is tired, they pick the safe choice: the film that most people already like. When everyone is feeling adventurous, they are happy to try an unusual film from near the bottom of the list.

The list of films is the same on both nights. Only the group's mood changes.

Temperature works just like that mood.

  • The list of films is the model's logits for the next token.
  • A tired, careful mood is a low temperature: the favourite wins almost every time.
  • An adventurous mood is a high temperature: unusual choices get a real chance.
  • The list itself does not change. Temperature only changes how the list is used.

In simple words: a low temperature plays it safe, a high temperature takes more risks.

What You Will Learn

In this lesson, you will learn:

  1. How temperature changes the logits.
  2. What low, normal, and high temperatures do to the chances.
  3. Why a very low temperature becomes greedy decoding.
  4. How temperature changes the variety of sampled tokens.
  5. How to choose a temperature for a task.

The code boxes show the maths on small lists. In the AI sandbox, the Sampling tab applies temperature to the real model's scores while you drag a slider, and the Settings tab sets the real temperature for the Chat tab.

How Temperature Works

Temperature is one small step, placed right before softmax:

  1. Take the logits.
  2. Divide every logit by the temperature.
  3. Run softmax as usual.

The same logits at three temperatures. Low is sharp, 1 is unchanged, high is flat.

Dividing by a small number makes the gaps between logits bigger, so the top token pulls further ahead. Dividing by a big number makes the gaps smaller, so the chances even out.

The same logits at three temperatures

function softmax(logits) {
  const exps = logits.map((l) => Math.exp(l));
  const total = exps.reduce((a, b) => a + b, 0);
  return exps.map((e) => (e / total * 100).toFixed(1) + "%");
}

const logits = [6.2, 3.1, 2.5, 1.0];  // " Paris", " the", " a", " located"

for (const temperature of [0.5, 1, 2]) {
  const scaled = logits.map((l) => l / temperature);
  console.log("T = " + temperature + ":", softmax(scaled).join("  "));
}

Expected output:

T = 0.5: 99.7%  0.2%  0.1%  0.0%
T = 1: 93.0%  4.2%  2.3%  0.5%
T = 2: 69.3%  14.7%  10.9%  5.1%
  • At T = 1, the chances are exactly the model's own. Temperature 1 changes nothing.
  • At T = 0.5, the top token takes almost everything.
  • At T = 2, the other tokens get a real share.

In simple words: temperature stretches or squashes the gaps between logits.

Very Low Temperature Is Greedy

As the temperature gets close to 0, the top token's chance gets close to 100%. At 0 itself you cannot divide by zero, so AI services simply treat temperature 0 as "always pick the top token".

Lower and lower temperatures

function topChance(logits, temperature) {
  const scaled = logits.map((l) => l / temperature);
  const exps = scaled.map((s) => Math.exp(s));
  const total = exps.reduce((a, b) => a + b, 0);
  return (exps[0] / total * 100).toFixed(2) + "%";
}

const logits = [3.0, 2.9, 2.8];

for (const temperature of [1, 0.5, 0.1, 0.01]) {
  console.log("T = " + temperature + ": top token gets", topChance(logits, temperature));
}

Expected output:

T = 1: top token gets 36.72%
T = 0.5: top token gets 40.18%
T = 0.1: top token gets 66.52%
T = 0.01: top token gets 100.00%

Even when the logits are very close together, a low enough temperature makes the top token certain.

Temperature and Variety

Temperature matters most when you sample. The next code box samples 20 tokens at a low temperature and at a high temperature, using the same seed, and counts how many different tokens come out.

How many different tokens in 20 samples?

function seededRandom(seed) {
  let a = seed >>> 0;
  return () => {
    a = (a + 0x6d2b79f5) >>> 0;
    let t = a;
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

const tokens = [" Paris", " the", " a", " located", " known", " home"];
const logits = [6.2, 3.1, 2.5, 1.0, 0.8, 0.5];

function sampleMany(temperature, n) {
  const exps = logits.map((l) => Math.exp(l / temperature));
  const total = exps.reduce((a, b) => a + b, 0);
  const random = seededRandom(5);
  const picked = [];
  for (let i = 0; i < n; i++) {
    let r = random() * total;
    let j = 0;
    while (r > exps[j]) { r -= exps[j]; j++; }
    picked.push(tokens[j]);
  }
  return picked;
}

for (const temperature of [0.3, 1.5]) {
  const picked = sampleMany(temperature, 20);
  console.log("T = " + temperature + ": different tokens =", new Set(picked).size);
}

Expected output:

T = 0.3: different tokens = 1
T = 1.5: different tokens = 3

At 0.3, every sample is the top token. At 1.5, the samples also include some unusual tokens. That is where "creative" replies come from, and also where odd mistakes can come from.

Easy Way to Remember: divide by T. A small T makes the leader stronger. A big T gives everyone a chance. T = 1 changes nothing. T = 0 is greedy.

Watching the Top Token's Share

The share of the top token is a simple way to see how "safe" a temperature is.

Top token share from low to high temperature

const logits = [6.2, 3.1, 2.5, 1.0];

for (const temperature of [0.2, 0.7, 1.0, 1.5, 2.0]) {
  const exps = logits.map((l) => Math.exp(l / temperature));
  const total = exps.reduce((a, b) => a + b, 0);
  const share = Math.round((exps[0] / total) * 100);
  console.log(("T = " + temperature).padEnd(8), "#".repeat(Math.round(share / 5)), share + "%");
}

Expected output:

T = 0.2  #################### 100%
T = 0.7  #################### 98%
T = 1    ################### 93%
T = 1.5  ################ 80%
T = 2    ############## 69%

As the temperature rises, the leader's share falls, and the rest of the list gets more room.

Try It in the AI Sandbox

Press the button. The sandbox opens on the Sampling tab and gets the real model's scores. Drag the Temperature slider and watch the bars change. Then press Sample 20 times at a low temperature and at a high temperature.

Then try the real setting in Chat: open Settings, set temperature to 0.2, ask for a short story, clear the chat, and ask again at 1.5.

Choosing a Temperature

There is no single best value. Pick a temperature based on the job you are doing.

TaskGood temperatureWhy
Extracting data, JSON, classification0You need the same, safest answer every time.
Answering facts, writing code0 to 0.3Odd tokens can mean wrong facts or broken code.
Everyday chat, explanations0.5 to 0.8A little variety sounds more natural.
Brainstorming, stories, names0.8 to 1.2More variety gives more ideas.
Wild experimentsabove 1.2Very varied, but often strange or broken.

You can turn this table into code, so an app can pick the right temperature for each job.

Pick a temperature for each job

const temperatureFor = {
  extract: 0,
  code: 0.2,
  chat: 0.7,
  brainstorm: 1.0,
};

for (const job of ["extract", "chat", "brainstorm"]) {
  console.log(job.padEnd(10), "->", temperatureFor[job]);
}

Expected output:

extract    -> 0
chat       -> 0.7
brainstorm -> 1

A high temperature does not make a model smarter or more creative in a deep sense. It only makes unlikely tokens more likely. Above about 1.5, small models often produce nonsense. Raise the temperature slowly and read the output carefully.

Common mistakes

MistakeWrong ideaCorrect ideaWhat goes wrong
High temperature for JSON"More creativity gives better JSON."Odd tokens break the format. JSON needs 0.Quotes and brackets go missing, and parsing fails.
Thinking T = 1 is "random""Temperature 1 makes the model random."T = 1 keeps the model's own chances. Sampling is what picks at random.You misread what the setting does.
Very high temperature for ideas"Temperature 2 gives the best ideas."Very high values give nonsense, especially with small models.Replies become strange words with no meaning.
Low temperature for brainstorming"I want safe ideas, so use 0."Temperature 0 gives the same few ideas every time.Every request returns the same list.
Temperature fixes facts"Lower temperature makes answers true."It makes answers more likely, not more true.You trust a confident wrong answer.
Changing several settings at once"I raised temperature and top-p and it got worse."Change one setting at a time to see its effect.You cannot tell which change caused the problem.

Exercises

Each exercise ends with a value on the last line. Press Check to compare your answer with the expected one.

What to Remember

  • Temperature divides every logit by a number before softmax.
  • T = 1 keeps the model's own chances.
  • A low T sharpens the chances. T = 0 is greedy decoding.
  • A high T flattens the chances, so unusual tokens get picked more.
  • A higher temperature means more variety, and also more mistakes.
  • Use 0 for data and tests, a low value for facts and code, and a higher value for ideas.

Next: Top-K, a simple way to stop the model from ever picking very unlikely tokens.