- 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:
- Exponentiate each score: is always positive, and bigger scores get much bigger.
- Divide by the total, so everything adds up to 1.
Logits 2, 1 and 0:
| Logit | Probability | ||
|---|---|---|---|
| A | 2 | 7.39 | 7.39 ÷ 11.11 = 66.5% |
| B | 1 | 2.72 | 24.5% |
| C | 0 | 1.00 | 9.0% |
Each extra point of logit multiplies the weight by .
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 :
- : the model's own distribution.
- below 1: gaps grow, so the top choice dominates. As , it always picks the top choice.
- above 1: gaps shrink, so the distribution flattens and rare choices come up more.
With the logits above, gives 87%, 12% and 1.6%.
Try it
Only differences matter
Adding the same number to every logit changes nothing, because it multiplies every 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.
Definition
Its log has a famously simple gradient:
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 from overflowing.