Talk to us

Understand LLMs from Zero#29

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

TokenMoveLogite to the powerProbabilityA7.39
66%
B2.72
24%
C1.11
9.9%

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

NumberJust divide by the sume to the powerSoftmax
2.0÷ 0 (impossible)7.3972.7%
1.0÷ 0 (impossible)2.7226.8%
−3.0÷ 0 (impossible)0.0500.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 …

262,144logits
28,756above 0
22.13the largest
−14.53the smallest
−990,957sum of the logits
4,607,000,777sum of “e to the power”

Gold dots are logits above 0, blue dots are negative. Only 28,756 are gold; 89% are negative.

TokenLogite to the powerProbability
refrigerator22.134,096,067,28689%
fridge19.54306,484,0026.7%
cooler17.8254,836,4781.2%
coldest17.2731,760,8990.69%
back16.9122,048,6930.48%
freezer16.8420,591,7420.45%
ice16.1810,631,1040.23%
bottom15.515,466,4140.12%
⋮
Parsing−14.534.91 × 10^−7see 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%

refrigerator
89%
fridge
6.7%
cooler
1.2%
coldest
0.69%
back
0.48%
all others
2.1%

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

A
73%
B
27%

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 logitsFactor 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?

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

Finished the episode?

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…