Softmax and temperature

How a model turns raw scores into a probability distribution, and how temperature makes it more or less adventurous.

Key idea
Logits, the softmax function, and sampling temperature
Real world
You rate three restaurants 8, 7 and 5 out of 10. You don't always go to the 8; you pick each with a probability that favours higher ratings. Softmax is a rule for turning ratings into those probabilities.
In an LLM
The model's last layer outputs one score, a logit, for every token in its vocabulary. Softmax turns those 100,000 scores into the next-token distribution.

From scores to probabilities

Logits can be any number: negative, zero, large. Probabilities must be positive and add to 1. Softmax does it in two steps:

  1. Exponentiate each score: eze^{z} is always positive, and bigger scores get much bigger.
  2. Divide by the total, so everything adds up to 1.
example · three logits

Logits 2, 1 and 0:

Logitelogite^{\text{logit}}Probability
A27.397.39 ÷ 11.11 = 66.5%
B12.7224.5%
C01.009.0%

Each extra point of logit multiplies the weight by e≈2.7e \approx 2.7.

example · the restaurants

Treat the ratings 8, 7 and 5 as logits. Softmax gives about 70%, 26% and 4%. You mostly go to your favourite, sometimes to the second, and rarely to the third, which is about how people actually behave, and why softmax is also used to model human choices.

Temperature

Before the softmax, logits can be divided by a temperature TT:

  • T=1T = 1: the model's own distribution.
  • TT below 1: gaps grow, so the top choice dominates. As T→0T \to 0, it always picks the top choice.
  • TT above 1: gaps shrink, so the distribution flattens and rare choices come up more.

With the logits above, T=0.5T = 0.5 gives 87%, 12% and 1.6%.

Try it

Logits (any real number)
Probabilities after softmax
Paris80.9%
Lyon11.0%
France6.6%
the1.5%
sum1.00
entropy0.95 bits
Temperature 1 gives the model’s own probabilities. Drag it down to sharpen them (entropy falls) or up to flatten them (entropy rises). Or shift all the logits at once.
“The capital of France is ___”. The model outputs a score (logit) per token; softmax turns scores into probabilities.

Only differences matter

Adding the same number to every logit changes nothing, because it multiplies every eze^z by the same factor, which cancels when you divide by the total. So a logit has no meaning on its own, only relative to the others. The All logits +1 button in the widget does exactly this: every bar stays put.

Where this shows up

  • A language model's policy is a softmax over its vocabulary: the RL loop.
  • Temperature controls exploration during RL sampling: exploration.
  • Softmax's simple gradient is what makes policy gradient work: policy gradient.
going deeper

Definition

softmax⁡(z)i=ezi/T∑jezj/T\operatorname{softmax}(z)_i = \frac{e^{z_i / T}}{\sum_j e^{z_j / T}}

Its log has a famously simple gradient:

∂log⁡softmax⁡(z)a∂zj=1[j=a]−pj\frac{\partial \log \operatorname{softmax}(z)_a}{\partial z_j} = \mathbb{1}[j = a] - p_j

Raise the chosen token's logit, lower every token's logit in proportion to its probability. That single line is the update in policy gradient.

In code, softmax is computed after subtracting the largest logit, which changes nothing mathematically (only differences matter) but stops eze^z from overflowing.

Related

  • probability - A list of possible outcomes and how likely each one is, adding up to 1. A language model is a machine for producing these.
  • entropy - How uncertain a distribution is, measured as the average surprise of its outcomes. Zero means certain; higher means more spread out.