Scores into probabilities: softmax
How does a model turn raw scores into "70% sure"?
How do you turn scores that can be negative into probabilities?
Give GPT-2 the text “The cat sat on the” and its last layer hands back 50,257 numbers, one score for every token in its vocabulary. Take three of them: say “ mat” scores 3, “ floor” scores 2 and “ banana” scores −1. How do you turn those scores into probabilities, numbers that are never negative and add up to 1?
The usual way to turn a set of numbers into shares is to divide each one by the grand total. Here the grand total is 3 + 2 − 1 = 4, so “ banana” gets −1 ÷ 4 = −25%. A negative probability makes no sense.
Pause and predict Scores can be negative. How would you turn them into shares that are never negative and add up to 1?
Every language model meets this problem at the end of every step. You know from Matrices what that final step looks like: one big matrix multiply that produces one score per word in the vocabulary. Each score is a dot product, so it can be any number: 4.2, 0, −3.7.
Raw scores like these are awkward to use. A score of 4.2 does not tell you that the model is 70% sure of anything, and you cannot build a die with a face that is −3.7 wide.
And during training you cannot measure how wrong the model was without knowing how much belief it put on the right answer.
The model needs to turn a list of arbitrary scores into probabilities. The function that does it is called softmax.
Softmax runs in two places in a model: at the very end, every time a token is generated, and inside every attention layer, where it decides how much each word listens to each other word.
Softmax itself never picks anything. It only produces the probabilities. Picking is a separate step, which you meet below as sampling.
The problem
First, names for the two ends of the conversion.
- Logits
- The raw scores a model produces, one per possible answer, before they are turned into probabilities. Bigger means “fits better”. They can be negative and they do not add up to anything in particular.
- For example: Prompt: “My favourite pet is a ___”. Scores: cat → 4.2, dog → 2.1, car → −0.7.
- Probability distribution
- A list of numbers that says how belief is shared out among the options. Two rules: no number is negative, and together they add up to exactly 1 (that is, 100%).
- For example: cat 0.88, dog 0.11, car 0.01. A fair die is [⅙, ⅙, ⅙, ⅙, ⅙, ⅙].
- The problem
- Turn any list of scores into a probability distribution, keeping the order: a higher score must get a higher probability.
- The naive fix
- Divide each score by the sum of the scores. That is how you would turn counts into percentages.
- Why it fails
- Try it: 4.2 + 2.1 − 0.7 = 5.6, so car gets −0.7 ÷ 5.6 = −12.5%. A negative probability is nonsense. And if the scores happen to sum to 0, you divide by zero.
- The idea
- First make every score positive in a way that keeps the order: raise e (about 2.718) to the power of the score. ez is positive for every z. Then divide by the sum.
- The trade-off
- Exponentials grow violently. Big scores overflow (we fix that in a deep dive), and the largest score tends to grab most of the probability (we control that with temperature).
A mental model
Softmax is two steps. Both are one line of code.
Replace each score z by ez. Negative scores become small positive numbers. Large scores become very large ones.
Divide each result by the total, so everything adds up to 1.
The exponential does something more useful than just removing minus signs. It turns gaps between scores into ratios between probabilities:
| score gap between two words | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| how many times more likely the higher one is | 1× | 2.7× | 7.4× | 20× |
Every extra point of score multiplies the odds by about 2.7. So a slightly better score gives a clearly better probability, and a much better score wins almost everything.
That is the “max” in the name. The “soft” is because the losers still keep a little. Mathematically, nothing is ever exactly 0. Even “ banana” kept 1% in the example at the top.
Once you have probabilities, choosing a word is easy to picture:
- Sampling
- Rolling a weighted die. Each option gets a face as wide as its probability. Roll, and see where it lands.
- For example: With cat 0.88, dog 0.11, car 0.01, out of 100 rolls you expect roughly 88 cats, 11 dogs and 1 car, though rarely exactly those numbers, because each roll is random.
Let's see the numbers
The pet example by hand. Logits: cat 4.2, dog 2.1, car −0.7.
Pause and predict Cat leads dog by 2.1 points. What share does softmax give cat?
Temperature: divide the logits first
High temperature is often described as making the model “think more creatively”. What it does is simpler. Dividing every logit by a number T before softmax shrinks or stretches the gaps:
| temperature | logits ÷ T | cat | dog | car |
|---|---|---|---|---|
| T = 0.5 (sharper) | 8.4, 4.2, −1.4 | 0.985 | 0.015 | 0.000 |
| T = 1 | 4.2, 2.1, −0.7 | 0.885 | 0.108 | 0.007 |
| T = 2 (flatter) | 2.1, 1.05, −0.35 | 0.696 | 0.244 | 0.060 |
The order of the three words never changes; only how decisive the distribution is changes. This is the “temperature” setting you have seen in LLM APIs.
(At T = 0.5, car is not truly 0: it is 0.00005.)
The loss for this prediction
If the correct word was “cat”: loss = −ln(0.885) = 0.12. The model was nearly right, and is barely penalised.
If the correct word was “dog”: loss = −ln(0.108) = 2.22, eighteen times larger.
The loss uses only the probability the model gave to the correct word. The probabilities of the other words affect it only because they all share the same 100%.
The math
Why only differences matter. Add a constant c to every logit. Every ez gets multiplied by the same factor ec, top and bottom of the fraction, and it cancels. That is what the “add 100” switch showed.
What you should remember
- Logits are raw scores: any number, any sign. A probability distribution is non-negative and sums to 1. Softmax converts one into the other.
- Softmax = exponentiate, then divide by the total. The exponential makes everything positive and turns score gaps into probability ratios (+1 ≈ 2.7× more likely).
- Only differences between logits matter. Adding a constant to all of them changes nothing, which is also the trick that keeps the computation from overflowing.
- Temperature divides the logits first. A low T makes the distribution sharp and the output predictable; a high T makes it flat and the output varied. The ranking never changes.
- Sampling is rolling a die weighted by those probabilities. Cross-entropy loss is the surprise at the right answer: −ln(pcorrect). Training pushes it down.
Where this appears in a real LLM
- In this lesson
- 3 to 5 candidate words
- Logits set by hand with sliders
- One softmax
- In a real system
- One logit for every token in the vocabulary: about 50,000 in GPT-2, 100,000 or more in recent models
- Logits computed by the whole network from your prompt
- One softmax per generated token at the output, plus one per token per attention head in every layer