Softmax, logits and the log-sum-exp trick
Turn a logit vector into probabilities with softmax and explain why adding a constant to every logit changes nothing. Compute softmax stably by subtracting the max.
Builds on: What an LLM computes, end to end
What the animation says
Scores to probabilities
At every position, the model outputs one raw score per token in its vocabulary. These scores are called logits. Logits can be negative, or huge. But we need probabilities: positive, and adding up to one. Softmax gets there in two moves. First, raise e to each score. Everything turns positive, and the order stays the same. Then divide each one by the total. Logits of two, one and minus one become about seventy, twenty-six and three and a half percent. Now add ten to every logit. What happens to the probabilities?
Only gaps matter
They don't change at all. Adding ten multiplies every exponential by e to the ten. The total gets the same factor, so it cancels. So the ratio between two tokens depends only on the gap between their logits. A gap of one means about two point seven times more likely. A gap of three means about twenty times. And equal logits always mean equal probabilities. Now try the logits one thousand, nine ninety-nine, nine ninety-seven. Same gaps. What does a direct computation give?
Overflow, underflow, the fix
Not a number. e to the one thousand is far beyond the largest float, so it becomes infinity. Infinity divided by infinity is NaN. Very negative logits fail the other way. Every exponential rounds down to zero, and zero over zero is NaN too. The fix uses what we just learned. Shifting all logits is free, so pick a shift that keeps the numbers safe. Which number should we subtract from every logit?
Worked: 1000, 999, 997
Subtract the max, one thousand. The logits become zero, minus one, minus three. Exponentiate: one, about zero point three seven, and about zero point zero five. The sum is about one point four two. The largest term is exactly one, so nothing overflows. And the sum is never below one, so there's no zero over zero. Divide: about seventy point five, twenty-five point nine, and three point five percent. That's exactly the answer for two, one, minus one. Same gaps, same probabilities.
In code, and in log space
In numpy, the naive line overflows and prints NaN. The stable version needs one extra subtraction. This line takes the max off before exponentiating. For log probabilities, use log-sum-exp: the max, plus the log of the shifted sum. Then each log probability is its logit minus that log-sum-exp. No exponential of a huge number anywhere. Remember: only the gaps between logits matter, and always subtract the max first.
Exponentiate, then normalize
Softmax, shift invariance, log-sum-exp
Worked example: z = (1000, 999, 997)
Stable softmax in numpy
import numpy as np
def softmax(z):
e = np.exp(z - z.max())
return e / e.sum()
def log_softmax(z):
m = z.max()
return z - (m + np.log(np.exp(z - m).sum()))
z = np.array([1000., 999., 997.])
print(np.exp(z) / np.exp(z).sum()) # [nan nan nan] (plus an overflow warning)
print(softmax(z)) # ≈ [0.705 0.259 0.035]
print(np.exp(log_softmax(z))) # same
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses