📝 Softmax
Description
< What is it? >
Softmax converts a vector of arbitrary real-valued scores, called logits, into a probability distribution over mutually exclusive classes:
Here, is the vector of logits. is the logit for the specific class whose probability is being calculated, while refers to every logit as runs from to in the denominator. In other words, identifies the target class and is a temporary index used to sum over all classes.
-
Example: for , the probability of the first class is
The output for every class is between and , and all output probabilities sum to . It is commonly used at the output of a multiclass classifier.
Key points
< From logits to probabilities >
- A logit is an unnormalized model score. Softmax compares every logit with all the others, so raising one class's probability lowers the relative probability of the others.
- Softmax preserves ranking: the class with the largest logit also has the largest probability.
- Example: logits become approximately . The first class is most likely, and the three probabilities sum to .
< Temperature >
A positive temperature can make the distribution softer or sharper:
- produces a flatter, less confident distribution
- produces a sharper, more confident distribution
- For any , it preserves the predicted class because the logit ordering does not change
< Numerical stability >
Exponentials can overflow for large logits. A stable implementation subtracts the largest logit before applying the exponential:
- Example: suppose an image classifier has logits
cat = 1000anddog = 999. Computing can overflow, but subtracting the maximum changes the logits to . Softmax then safely produces approximately for cat and dog.
This gives exactly the same probabilities. During training for single-label classification, pass logits directly to a numerically stable cross-entropy loss instead of applying softmax manually first.
Comparison
< Softmax vs sigmoid >
| Softmax | Sigmoid | |
|---|---|---|
| Output | One probability distribution that sums to | Independent values between and |
| Best for | One mutually exclusive class | One binary label or multiple independent labels |
| Example | Choose exactly one: cat, dog, or bird | An image can be a cat, indoors, and sunny at the same time |
| Typical loss | Multiclass cross-entropy | Binary cross-entropy |
Related ideas
- Activation Functions explains why neural networks use non-linear functions.
- Sigmoid is the usual output function for binary or multi-label classification.
- Transformer uses softmax to turn attention scores into attention weights.
- Confidence Calibration uses temperature scaling to adjust predicted probabilities.