LLM Lab

Probability & softmax

A language model ends in probabilities: how likely each next token is. Softmax is how raw scores become those probabilities.

Where this maths shows up in a transformer
Embeddings (vectors)→Q·Kᵀ scores (matrices)→Softmax (probability)→Cross-entropy loss (logs)→Backprop (derivatives)→LayerNorm (mean, variance)

Probability distributions

A probability distribution assigns a number between 0 and 1 to each possible outcome, and the numbers add up to 1. The expected value is the average of the outcomes weighted by their probabilities.

Softmax in three moves

A network outputs plain scores called logits, which can be negative or huge. Softmax turns them into a distribution in three moves: take e to the power of each score (now all positive), add them up, divide each by the total. Adding the same number to every logit changes nothing; only differences matter. Softmax appears twice in a transformer: inside attention, and at the end to pick the next token.

Drag the logits and the temperature in the lab and follow each column.

A worked example

Take logits (2, 1, 0). Exponentiate: e² ≈ 7.39, e¹ ≈ 2.72, e⁰ = 1, which add up to 11.11. Divide each by the total: 0.665, 0.245 and 0.090. The largest logit gets the largest share, but the others keep some probability. Add 10 to all three logits and the answer is exactly the same, because only the differences between logits matter. Programs use that fact and subtract the largest logit first so the exponentials never overflow.

matz = 2→ez = 7.39÷ 11.11 →66.5%
floorz = 1→ez = 2.72÷ 11.11 →24.5%
moonz = 0→ez = 1.00÷ 11.11 →9.0%
Softmax on the logits (2, 1, 0): exponentiate, add up (11.11), divide. The order is kept, and the three shares add up to 100%.

Temperature

Dividing the logits by T before softmax changes how peaked the result is. With T = 0.5 the logits become (4, 2, 0) and the probabilities (0.867, 0.117, 0.016): sharper. With T = 2 they become (1, 0.5, 0) and the probabilities (0.506, 0.307, 0.186): flatter. As T approaches 0, all the probability goes to the top logit, which is greedy decoding.

T = 0.587% 12% 2%
T = 167% 24% 9%
T = 251% 31% 19%
The same logits at three temperatures. Low T piles the probability onto the top word; high T spreads it out. The order never changes.

In a language model

At every step the final layer outputs one logit per vocabulary entry, about 32,000 for LLaMA and 50,000 for GPT-2, and one softmax turns them all into the next-token distribution. Softmax also appears inside every attention head, once per row of the score matrix, to turn scores into weights.

Easy to confuse

Check yourself · 1 of 3

Which of these can be a probability distribution over 3 outcomes?

Check yourself · 2 of 3

What does softmax do to the scores (2, 1, 0)?

Check yourself · 3 of 3

Temperature T = 0.5 divides every logit by 0.5 before softmax. What happens to the distribution?

Read more in the books

  • Deep Learning, Goodfellow, Bengio, Courville: Ch. 3, “Probability and Information Theory”.
  • Mathematics for Machine Learning, Deisenroth, Faisal, Ong: Ch. 6, “Probability and Distributions”.
  • Dive into Deep Learning, Zhang, Lipton, Li, Smola: Ch. 2, “Preliminaries”: probability and statistics.
All books
Lablogits to probabilities
How to read this
  • Each row is one candidate next word. Drag its slider to change its logit z, the raw score the model produced.
  • The middle column shows ez/T: every score turned into a positive number.
  • Those numbers are divided by the total at the bottom. The result is the probability (in teal) and its bar; the probabilities always add up to 100%.
  • The temperature slider divides every logit by T first: below 1 the top word gains probability, above 1 the distribution flattens.