Probability & softmax
A language model ends in probabilities: how likely each next token is. Softmax is how raw scores become those probabilities.
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.
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.
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
- Logits are not probabilities: they can be negative and do not sum to anything in particular.
- Softmax keeps the order of the scores. It never makes a lower logit more likely than a higher one; temperature only changes how far apart they end up.
Which of these can be a probability distribution over 3 outcomes?
A distribution has no negative entries and sums to 1. 0.6 + 0.3 + 0.1 = 1; the first set sums to 1.1, and the third has negatives.
What does softmax do to the scores (2, 1, 0)?
Softmax exponentiates each score, then divides by the total. The biggest score gets the biggest probability, and the results always sum to 1.
Temperature T = 0.5 divides every logit by 0.5 before softmax. What happens to the distribution?
Dividing by a number below 1 stretches the gaps between logits, and exp turns bigger gaps into much bigger ratios.
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.