The Softmax Function: Turning Logits into Probabilities
Since episode 1 we have been saying that an LLM gives a probability for the next token. But what the network really puts out is a list of unruly numbers, many of them negative: the logits. The softmax function turns them into probabilities. Move the numbers and see what happens to the bars.
- POINT 1The raw numbers the network puts out are logits: one per token, 262,144 of them in Gemma 3 4B. Many are negative (89% here), and they do not add up to 100%.
- POINT 2Softmax has two steps. ① Raise e (about 2.72) to the power of each logit: every number becomes positive. ② Divide by the sum. Example: 2.0, 1.0, 0.1 → 7.39, 2.72, 1.11 → 66%, 24%, 10%.
- POINT 3What matters is the gaps between the logits. Add the same number to all of them and no probability changes. A gap of 1 means 2.72 times the probability. Multiply all logits by 2 and the result gets sharper; halve them and it gets flatter.
TRY 1
Move the logits, watch the probability bars
In the 3D view, the thin poles at the back are the logits (up for positive, down for negative) and the thick columns in front are the probabilities. Type numbers into the table or drag the sliders: “e to the power” and the probability update at once. With the buttons at the top you can also load logits that Gemma really produced.
Choose a set of numbers
Sum of “e to the power”: 11.2 Sum of the probabilities: 100%
The logits 2.0, 1.0 and 0.1 have become 66%, 24% and 10%. Change a number, or press one of the buttons below.
The probabilities are computed inside this page (e to the power, then divide by the sum). The length of the 3D poles is scaled to the largest logit.
TRY 2
Why dividing by the sum is not enough
“Why not just add them all up and divide by that sum?” Here that idea runs next to softmax. Move the three numbers. They start as 2, 1 and −3.
Sum: 2.0 + 1.0 + (−3.0) = 0.0
| Number | Just divide by the sum | e to the power | Softmax |
|---|---|---|---|
| 2.0 | ÷ 0 (impossible) | 7.39 | 72.7% |
| 1.0 | ÷ 0 (impossible) | 2.72 | 26.8% |
| −3.0 | ÷ 0 (impossible) | 0.050 | 0.5% |
The sum is 0, so you cannot divide. Softmax still gives proper probabilities.
TRY 3
All 262,144 real logits
These are the real logits of Gemma 3 4B after reading the sentence below. One dot is one token (512 × 512 = 262,144). The brighter the dot, the larger the logit. Switch the view to see them sorted, and as probabilities.
The sentence
To keep the milk cold, put it in the …
Gold dots are logits above 0, blue dots are negative. Only 28,756 are gold; 89% are negative.
| Token | Logit | e to the power | Probability |
|---|---|---|---|
| refrigerator | 22.13 | 4,096,067,286 | 89% |
| fridge | 19.54 | 306,484,002 | 6.7% |
| cooler | 17.82 | 54,836,478 | 1.2% |
| coldest | 17.27 | 31,760,899 | 0.69% |
| back | 16.91 | 22,048,693 | 0.48% |
| freezer | 16.84 | 20,591,742 | 0.45% |
| ice | 16.18 | 10,631,104 | 0.23% |
| bottom | 15.51 | 5,466,414 | 0.12% |
| ⋮ | |||
| Parsing | −14.53 | 4.91 × 10^−7 | see below |
The probability of the lowest token, “Parsing”. It is not 0%: 13 zeros follow the decimal point before the first other digit.
0.0000000000000107%
Times 1 is Gemma's normal output. “refrigerator” is at 89%.
The logits were read straight from llama.cpp for Gemma 3 4B (Q4_K_M). The probabilities were computed from them by “e to the power, then divide by the sum”; they differ from the probabilities llama.cpp itself returns by at most 0.008%. The probabilities for other multipliers were computed in advance from all 262,144 logits (in steps of 0.05). This multiplier leads to “temperature” in episode 70 (temperature = 1 ÷ multiplier).
TRY 4
The gap sets the factor
Imagine a world with only two tokens, A and B. If A's logit is larger than B's by some gap, how many times more probable is A? Move the slider to change the gap.
× 2.72
With a gap of 1.00, A is 2.72 times as probable as B (e to the power of 1.00). The size of the logits themselves does not matter.
| Gap between the logits | Factor between the probabilities |
|---|---|
| 0.0 | × 1.00 |
| 0.5 | × 1.65 |
| 1.0 | × 2.72 |
| 2.0 | × 7.39 |
| 3.0 | × 20.09 |
| 5.0 | × 148 |
The real example: the logit of refrigerator is 2.59 above that of fridge, and their probabilities are 89% and 6.7%: about 13.4 times. However many other tokens there are, the ratio of two probabilities depends only on the gap between their two logits.
CHECK
Check yourself
Three questions on what this episode showed.
Q1What are the two steps of softmax?
Q2You add 100 to every logit. What happens to the probabilities?
Q3What is the probability of the token with the smallest logit?
NEXT #30
Loss Function and Cross-Entropy: Measuring an LLM's Mistakes
Next time we measure, with a single number, how far off these probabilities are: loss functions and cross-entropy.
Logits and probabilities: Gemma 3 4B (measured with llama.cpp). The example numbers (2.0, 1.0, 0.1 and so on) are examples for explanation.
FINISH
COMMENTS
Comments & questions
Something unclear, something you want to know more about, or just your thoughts: write it here. We answer quickly, and other learners can reply too.
Loading…