6. Softmax: scores into probabilities
A model's raw scores can be any numbers. The softmax turns them into probabilities that add up to 1, and the temperature you set in an API call decides how sharp they are.
The GPT-style model's five highest raw scores for the word after the words chosen, as bars. Each score z becomes e^z, always positive and growing fast; the five are added up; and each is divided by the sum, so the five new bars add up to 1. The model itself divides by the sum over all its words, which the last step names.
The idea
The model's last layer gives every word in its vocabulary a raw score, a logit: any number, positive or negative. Bigger means more likely, but scores are not probabilities: they do not add up to 1, and they can be negative.
The softmax fixes that in two moves. Raise e to the power of each score: every result is positive, and bigger scores grow much faster than smaller ones. Then divide each by their sum: now they add up to 1. The order never changes; the top score stays the top probability.
Attention uses the same function on a different set of scores, how well a word matches each other word, to decide how much to look at each (chapter 9). It is in every attention head of every layer, and once more at the very end.
Temperature
Divide the scores by a number T before the softmax, and you change how sharp the result is. That number is the temperature of an API call:
At T = 1.0, “likes” gets 32.6 %; the five words shown hold 99.8 % of all the probability.
The same model's probabilities for the next word, with the raw scores divided by a temperature T before the softmax. Move the slider: below 1 the top word takes almost everything; above 1 the bars even out. The five most likely words are shown; the probabilities are taken over all the model's words.
A worked example: after “The cat”
After “The cat”, the model's top raw scores are 9.42 for “likes”, 9.11 for “sees” and 9.03 for “eats”. Raised to powers of e they become 12,349, 9,053 and 8,361: close scores, but the gaps have grown. Divided by the sum over all 201 words, the probabilities are 0.326, 0.239 and 0.221.
At T = 0.5 the same scores give the top word 0.414; at T = 2 they give it 0.244.
The formula
- \(z_i\)
- the raw score of word i
- \(e^{z_i}\)
- e, about 2.718, raised to that score: always positive
- \(\sum_{j}\)
- added up over every word j of the vocabulary
- \(T\)
- the temperature: 1 leaves the scores as they are, below 1 sharpens, above 1 flattens
Why it matters
The temperature setting in an LLM API is this T. Near 0 the top word takes nearly all the probability, so the answer comes out almost the same every time: good for extraction and classification. At 1 the model draws from its own probabilities; above 1 it picks unlikely words more often, which is more varied and more often wrong. Top-k and top-p settings cut off the long tail of unlikely words before the draw. The log probabilities some APIs return are the logarithms of these softmax outputs.
Where it fails
- Never exactly zero. e to any power is above 0, so every word keeps some probability; at a high temperature, words from the long tail get drawn. That is what top-k and top-p are for.
- Confident is not correct. A probability close to 1 says the model is sure of the next word, not that the sentence is true.
- Big numbers overflow. e to the power of 1000 is too large for a computer. Implementations subtract the largest score from all of them first, which changes nothing in the result; our engine does so too.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
A softmax by hand
Three words have the raw scores [2, 1, 0]. What probability does the softmax give the first? Three decimals.
The same scores at T = 2
Divide the scores [2, 1, 0] by a temperature of 2 before the softmax. What probability does the first word get now? Three decimals.
Temperature near 0
What happens to the probabilities as the temperature gets close to 0?
Only two words
After “The cat”, the model's two highest raw scores are 9.42 for “likes” and 9.11 for “sees”. If these were the only two words, what probability would the softmax give the first? Three decimals.
In the paper
The softmax appears twice in the paper's model. Section 3.2.1 applies “a softmax function to obtain the weights on the values” in every attention head, and section 3.4 uses “the usual learned linear transformation and softmax function to convert the decoder output to predicted next-token probabilities”.
In the paper: section 3.2.1, 3.4