1. What does a classifier's last layer output?
Return to our fruit-guessing machine. This time, start with three possible classes: apple, banana and watermelon. After the hidden layers, the final affine layer produces one score per class. Suppose the scores are:
These raw scores are called logits. Apple has the highest score, followed by watermelon and banana. But the scores are not probabilities:
- is greater than one, and is negative.
- Their sum is , rather than one.
- A logit alone does not tell us the probability assigned to a class.
For mutually exclusive classes, we want a distribution: nonnegative values that add up to one. Lesson 10's cross-entropy compares that distribution with the true label. Practical loss implementations may accept logits directly and perform the conversion internally, but the mathematical probability interpretation is the same.
2. Repair the negative values: shift, then normalise?
One idea is to add to every score so the minimum becomes zero:
Divide by the new sum, :
But why shift the minimum to zero? Adding instead makes the minimum one:
The choice of offset changes the probabilities. A rule that always subtracts the minimum would also fail when all scores are equal: every shifted score would be zero.
We want a transformation that makes scores positive and gives the same distribution when a common constant is added to every logit. It should depend on relative score differences rather than the arbitrary choice of zero.
This is stronger than preserving the ranking alone. A small gap and a large gap can share the same ranking but should not necessarily produce the same distribution.
3. Exponentiate, then normalise: Softmax
The exponential function maps every finite real number to a positive value:
It is increasing, so it preserves the order of scores. Negative scores become small positive weights rather than negative probabilities.
Exponentiate our three logits:
Their total is approximately . Divide each exponential by that total. This is Softmax:
| Class | Logit | Probability | |
|---|---|---|---|
| Apple | 2.1 | 8.166 | 0.551 |
| Banana | -0.5 | 0.607 | 0.041 |
| Watermelon | 1.8 | 6.050 | 0.408 |
| Sum | 3.4 | 14.822 | 1.000 |
In exact arithmetic, each probability is positive and the sum is one. With finite precision, very small probabilities may underflow to zero, and rounding can make displayed percentages sum to slightly more or less than 100%.
Why a common shift cancels
Add the same constant to every logit:
The factor cancels. Softmax therefore ignores a common offset, unlike the shift-and-divide approach above.
The ratio of two class probabilities depends on their score difference:
Apple's score exceeds watermelon's by , so its probability is about times as large. Comparing percentage differences between raw logits would be misleading: shifting every logit changes those percentages while leaving Softmax unchanged.
Watch the four-fruit classifier
Now return to the four classes from Lesson 7. Adjust the features to change the class scores, and watch Softmax convert them into probabilities.
Most likely fruit:
Unrounded probabilities sum to one; displayed percentages may differ slightly due to rounding.
At a fixed temperature, the highest logit also has the highest probability. Softmax provides a distribution rather than only selecting the largest score. These are model-assigned probabilities, not a guarantee that the model's confidence accurately matches real-world frequencies.
For mutually exclusive classes, the classes compete for a total probability of one. When several labels can independently be true, a separate Sigmoid per label is often a more appropriate output setup.
4. Temperature controls the sharpness
Divide the logits by a positive temperature before applying Softmax:
The ratio now becomes:
- Lower temperature makes score gaps larger after scaling, sharpening the distribution.
- Higher temperature makes those gaps smaller, flattening the distribution.
- At , we recover ordinary Softmax.
Temperature 0.5
Temperature 1
Temperature 2
For these particular logits, gives apple roughly 64%, not nearly 100%. How sharp the result becomes depends on both the temperature and the original score gaps.
Probability sum:
For fixed logits, positive temperature preserves the ranking. If we always choose the largest probability, temperature alone does not change that choice. If we sample from the distribution, changing temperature changes how often lower-scoring alternatives are selected.
As grows without bound, the distribution approaches uniform. As approaches zero from above, probability concentrates on the largest logit. If several logits tie for the maximum, they share that limiting probability. Equal logits always produce a uniform distribution.
Language models use this idea when sampling the next token: a sharper distribution tends to repeat high-scoring choices, while a flatter one admits more alternatives. Temperature changes the sampling distribution; it does not by itself guarantee correctness, creativity or a particular writing style. A zero-temperature setting is commonly treated as a special decoding choice, since the formula above divides by and requires .
Keep the calculation numerically stable
A score of 1,000 causes trouble if we directly calculate in standard floating-point arithmetic. Use the common-shift property to subtract the largest score first:
For positive , every exponent is nonpositive and at least one is zero. The largest exponential is one, so the calculation avoids exponential overflow. For example, logits become shifted scores without changing the distribution.
The same idea appears in the stable log-sum-exp expression:
For a correct class at ordinary temperature, cross-entropy can be computed directly from logits as:
This avoids taking the logarithm of a tiny, rounded probability. The existing shared Softmax helper used by these experiments already subtracts the maximum score before exponentiating.
5. What you have discovered
Softmax connects class scores to a probability distribution.
- Logits are arbitrary real scores, not probabilities.
- Exponentiation makes finite scores positive; normalising the resulting weights makes their sum one.
- A common offset cancels. Relative score differences determine probability ratios.
- Temperature changes sharpness while preserving the ranking of fixed logits.
- Sampling and selecting the maximum are different decisions; temperature affects them differently.
- Subtracting the maximum avoids exponential overflow and supports stable cross-entropy calculations.
We now have predictions, probabilities and a loss. The next lesson asks how an optimiser uses gradients to update the parameters, and how large each step should be.