TL;DR
A neural network's last layer often produces raw scores, one per class, that can be any real numbers, negative or large. Softmax converts those scores into probabilities: each output is positive, and all of them add up to 1. The largest score gets the largest probability, but the output is a smooth "soft" version of picking the maximum.
Where you'll see it: as the final layer of image classifiers and language models, where it turns scores over every possible next word into a probability distribution.
Intuition
A vote that is shared out in proportion
Suppose three candidates get scores 2, 1 and 0.1 from a panel. You want to split a single unit of attention among them so that the best-scored candidate gets the most, but nobody gets zero just for being second. Softmax does this in two moves. First it makes every score positive by exponentiating it, so a bigger score becomes exponentially bigger. Then it divides each result by the total, so the pieces sum to one.
The exponential is the important part. The gap between scores of 2 and 1 is only one point, but after exponentiating, is about 2.7 times . Scores that are close produce probabilities that are close, and a score that is clearly ahead takes most of the mass. That is why softmax behaves like a smooth version of "take the maximum": it always gives a differentiable answer, and gradients can flow back through it during training.
It is also why the outputs are called "logits" when they come into softmax. A logit is an unnormalised score. With two classes, the difference between the two logits is the log-odds of the first class, and softmax maps logits to probabilities.
flowchart LR A[Raw scores z1, z2, z3] --> B[Exponentiate each one] B --> C[Divide by the sum] C --> D[Probabilities that add to 1]
The math
Definition
For a vector of real scores , the softmax is the vector whose -th entry is
Here is the number of classes, is the score for class , and is the exponential function. Because for every real , every output is strictly positive. Dividing by the sum makes the outputs add to 1, so they form a probability distribution over the classes. The sum in the denominator is the normaliser, and it is what turns raw exponentials into shares.
Two properties that matter
Shift invariance. Adding the same constant to every score does not change the output:
The factor cancels between numerator and denominator. Only differences between scores matter, not their absolute values.
Derivative. Writing , the partial derivative of one output with respect to one score is
where is 1 when and 0 otherwise. Raising the score of a class increases its own probability and decreases every other one. This clean form is why softmax pairs so well with the cross-entropy loss: when the true class is one-hot, the gradient of the loss with respect to the scores collapses to , the predicted probability minus the target.
Worked examples
Scores .
- Exponentiate: , , .
- Sum: .
- Divide each by the sum:
- Check: .
Interpretation: the top score gets about two-thirds of the probability, and the other two share the rest. The score gap of 1 between the first two entries has become a probability ratio of , which is as expected.
Scores . Computing directly overflows ordinary 64-bit floats, which top out near .
- Use shift invariance: subtract the maximum, . The answer is unchanged.
- Exponentiate: , .
- Sum: .
- Divide: , .
Interpretation: subtracting the maximum before exponentiating is the standard trick used in every deep learning library. The largest exponent becomes , so nothing can overflow, and the result is exactly the same mathematically.
Tradeoffs and failure modes
Softmax is the right output when classes are mutually exclusive and you want a distribution. It is the wrong choice in several common cases.
| Situation | Problem | Better choice |
|---|---|---|
| Several labels can be true at once (a photo tagged "beach" and "sunset") | Softmax forces the probabilities to compete, so one label's gain is another's loss | Independent sigmoids, one per label |
| Very many classes, such as a 100,000-word vocabulary | The normaliser sums over every class on every step, which is expensive | Sampled softmax, or Hierarchical Softmax |
| Scores are huge and unscaled | Outputs saturate near 0 and 1, and gradients vanish | Normalise or rescale the inputs; subtract the maximum only to avoid overflow |
| You need a hard decision, not a distribution | Softmax gives probabilities, not a single winner | Take the argmax at inference time |
| Calibration matters for the final probability | Modern networks are often overconfident, and softmax faithfully reports that confidence | Temperature scaling after training |
A useful knob is the temperature : computing with flattens the distribution, and sharpens it. Language models expose this directly as a sampling setting.
History
The exponential form is much older than machine learning. The Boltzmann distribution in 19th-century statistical mechanics has the same shape: energies enter an exponential, and dividing by a partition function normalises them. Gibbs made the general case explicit in the early 1900s.
The name and its use as a neural network output come from John Bridle, who around 1990 described normalised exponential outputs for classifier networks and called them a "soft" version of the maximum. The idea caught on because it gave networks a probability output that could be trained with maximum likelihood and differentiated easily. It became standard in the 2010s as deep classifiers grew, and it is now the default final layer for multi-class problems.
Check yourself
QuestionCompute softmax of . What do you get, and why is that the natural answer?Show answer
Each , the sum is 3, so every output is . With no information distinguishing the classes, the distribution is uniform.
QuestionFor , compute the softmax. Give the first output to three decimal places.Show answer
and , with sum . The first output is , and the second is .
QuestionWhy does softmax depend only on differences between scores? What does that imply about adding 100 to every logit?Show answer
Adding a constant to every score multiplies every exponential by , and the same factor appears in the sum, so it cancels. Adding 100 to every logit leaves the probabilities unchanged, which is why the subtract-the-maximum trick is safe.
QuestionA spam filter outputs logits for 'spam' and 'not spam' and uses softmax. The logits are . Roughly how confident is the model that the message is spam, and what would a larger gap do?Show answer
The difference is 6, so . A larger gap pushes the probability closer to 1 quickly, which is why very large logits make a network look certain even when it is wrong.
QuestionYou are building a tagger where an image can have several true tags at once. Should you use softmax at the output? Justify your answer.Show answer
No. Softmax makes the probabilities compete and sum to 1, so adding confidence to one tag removes it from the others. Several true tags need independent probabilities, one per tag, which is what a sigmoid per output gives.
Sources
- Softmax function (Wikipedia): https://en.wikipedia.org/wiki/Softmax_function
- Goodfellow, Bengio, Courville, Deep Learning (MIT Press), ch. 6: https://www.deeplearningbook.org/