From scores to probabilities
An LLM produces one score for each candidate next token. A probability distribution makes scores comparable and lets the decoder draw a token.
Open this chapter with experiments and audio
Why does a model need functions?
A function gives a reproducible output for a specified input. A linear score such as 2x + 1 moves by the same amount whenever x increases by one. Curved functions respond differently at different positions. A network combines many such operations. At the final layer of a language model the result is a set of scores, commonly called logits.
A logit can be negative or greater than one, and logits need not sum to one. If two token candidates score 2 and 1, their ordering tells you which candidate the model favours. It does not tell you what fraction of random selections each should receive. Softmax provides a specific rule for that conversion.
Convert two scores by hand
Subtract the highest score from both. [2, 1] becomes [0, −1].
Exponentials are positive. exp(0) = 1 and exp(−1) ≈ 0.368.
Divide by 1.368. The probabilities are approximately 0.731 and 0.269.
Subtracting the same constant from each score leaves all ratios unchanged. Working with a largest score of zero keeps exponentials manageable on a computer. The difference between the scores matters here. A gap of one gives an odds ratio of e, approximately 2.72.
How does temperature affect the result?
Before softmax, divide each score by a positive temperature. At temperature 1 the scores stay the same. At 0.5 a gap of one becomes a gap of two, which increases the preference for the leading candidate. At 2 that gap becomes 0.5, which gives other candidates more probability. Use the experiment to compare the same candidates under different temperatures.
The ranking stays the same for any positive temperature. Individual draws can still land on a low-probability candidate. A distribution tells you about repeated selections, while one answer is just one realization. A high assigned probability also only describes the model’s preference in this context. Factual verification requires independent evidence.
Reproduce the distribution
import math
scores = [2.0, 1.0]
for temperature in [0.5, 1.0, 2.0]:
values = [math.exp((s - max(scores)) / temperature) for s in scores]
probs = [v / sum(values) for v in values]
print(temperature, [round(p, 3) for p in probs])The first candidate gets approximately 0.881, 0.731 and 0.622. The total remains one before rounding. If you add 100 to each original score and rerun the script, all three output pairs remain the same. This gives you a simple test for a stable softmax implementation.
Two candidates have equal logits. What happens at temperature 0.1?
If these are the only two candidates, each keeps probability 0.5. Temperature scales gaps and there is no gap between them.
Why do logarithms appear again during training? If the correct token gets probability 0.5, its negative natural log probability is about 0.693. If the same token receives probability 0.01, the penalty is about 4.605. This produces a smooth training signal that changes even when the winning candidate stays the same. Chapter 22 uses that property to train a language model one token position at a time.
Use the probability of the observed token to calculate a loss
What does a 60% next-token probability mean?
At one position a small model permits three tokens. Suppose its scores are ln(6), ln(3) and ln(1), where ln is the natural logarithm. Softmax takes the exponential of each score and divides by their sum. The exponential undoes the logarithm here, so the three weights are 6, 3 and 1. Divide by 10 to obtain 0.60, 0.30 and 0.10. The numbers now sum to one, as required for drawing one token.
Greedy decoding always selects the first token here. Unrestricted random sampling selects it about 60 times in 100 independent draws on average, if you hold the input and model fixed. There is a third possibility. A decoder with a cumulative probability cutoff of 0.8 keeps the smallest leading set reaching 0.8. The first token alone reaches 0.6, the first two together reach 0.9. After removing the third, their draw probabilities become 6/9 and 3/9.
Would adding 10 to all three scores affect any of those choices?
Each exponential gains the same factor, exp(10), which cancels in the division. All three probabilities stay the same. The 60% describes a next-token distribution at this position. Estimating the chance a completed answer is factually correct would require a defined correctness test and evidence relating scores to outcomes.
Change temperature and watch the proportions between tokens →
Further reading · PyTorch · Softmax