What is the softmax function?
The softmax function turns a vector of raw scores, called logits, into a probability distribution. It exponentiates each score and divides by the sum of all the exponentials, so every output lies between 0 and 1 and the outputs add up to 1. Larger scores get larger probabilities, and the gap between scores sets how lopsided the result is.
Softmax is the standard output layer for multi-class classification, from digit recognition to ImageNet's 1,000 classes. Language models apply it over their whole vocabulary to pick the next token, and attention layers apply it to decide how much each token attends to the others. It is almost always paired with cross-entropy loss during training.
Key ideas
- Softmax maps any vector of real numbers to probabilities that sum to 1.
- It preserves the ranking of the logits, so the top logit is always the top class.
- Only differences between logits matter; shifting them all by a constant changes nothing.
- Temperature below 1 sharpens the distribution, and above 1 flattens it.
- Subtracting the largest logit before exponentiating prevents overflow.
- For two classes, softmax is the same as a sigmoid of the logit difference.
Short takes on this topic
Common questions
Why is it called softmax?
It is a smooth, differentiable version of the argmax function. Argmax puts all the weight on the largest score, while softmax gives most of the weight to it and some to the rest. As the temperature approaches 0, softmax approaches argmax.
Why does softmax use the exponential function?
The exponential is positive for every input, so negative logits still give valid probabilities. It also turns differences into ratios, which makes logits act as unnormalized log-probabilities. That pairs neatly with the logarithm in cross-entropy loss and gives the simple gradient P − y.
Are softmax outputs true probabilities?
They form a valid distribution, but they are not guaranteed to match how often the model is right. Large neural networks are often overconfident: a 95% softmax output may be correct much less than 95% of the time. Calibration methods such as temperature scaling correct this after training.
Where is softmax used besides the output layer?
Attention layers in transformers apply softmax to the scores between a query and every key, producing weights that sum to 1 over the sequence. Policy networks in reinforcement learning use it to turn action scores into a probability of taking each action.
Related explainers
- Loss Functions — How networks measure their mistakes
- Classification vs Regression — Two kinds of prediction and when to use each
- Logarithms & Exponentials — The math behind cross-entropy, softmax, and information theory
- How Self-Attention Works — Query, key, value — the core of self-attention
- Entropy & Cross-Entropy — Measuring surprise, uncertainty, and prediction quality
- Neural Networks — Neurons, layers, and how networks learn
References
- Goodfellow, Bengio & Courville (2016), "Deep Learning", section 6.2.2.3: Softmax Units for Multinoulli Output Distributions
- Zhang et al., "Dive into Deep Learning", Softmax Regression
- Hinton, Vinyals & Dean (2015), "Distilling the Knowledge in a Neural Network"
- Guo et al. (2017), "On Calibration of Modern Neural Networks"
Last updated