B · Neural networks
Softmax & Multi-class Classification
Turn a score per class into probabilities that sum to one, and train them with cross-entropy.
Try it: B4 · Softmax & Multi-class
Intuition
Logistic regression answers one yes/no question. Many problems have more than two answers: which digit is this, which of 1,000 objects is in the photo, which word comes next. The model gives each class a score (a logit), any real number, and softmax turns the list of scores into probabilities: all positive, adding up to 1, with the highest score getting the highest probability.
It does this in two moves. Exponentiate every score, which makes them all positive and stretches the gaps between them. Then divide each by the total, so they add to 1. A class that scores 2 more than another gets \(e^2 \approx 7.4\) times its probability, whatever the other scores are.
The math
For scores \(z_1, \dots, z_K\):
\[ \operatorname{softmax}(\mathbf{z})_k = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}} \]Properties
- Shift invariance. Adding the same constant to every score changes nothing: \(e^{z_k + c} / \sum_j e^{z_j + c} = e^{z_k}/\sum_j e^{z_j}\). Only differences between scores matter.
- Two classes give the sigmoid. With scores \(z_1, z_2\): \(\operatorname{softmax}_1 = 1/(1 + e^{-(z_1 - z_2)}) = \sigma(z_1 - z_2)\). Logistic regression is softmax with one score fixed at 0.
- A soft arg-max. Divide the scores by a temperature \(T\): as \(T \to 0\), all probability goes to the largest score (a hard arg-max); as \(T \to \infty\), it spreads evenly. That is where the name comes from.
The loss and its gradient
With one-hot target \(\mathbf{y}\) (1 for the true class \(c\), 0 elsewhere), the cross-entropy loss is the negative log-probability of the true class:
\[ L = -\log p_c = -z_c + \log\sum_j e^{z_j} \]Its gradient with respect to the scores is wonderfully simple:
\[ \frac{\partial L}{\partial z_k} = p_k - y_k \]Push up the true class's score by how far its probability is from 1; push down every other class's score by its probability. This is the same \(p - y\) as in logistic regression, and it is why softmax and cross-entropy are always used together.
Worked example. Scores \((2, 1, 0)\). Exponentials \((7.39, 2.72, 1.00)\), total 11.11, so probabilities \((0.665, 0.245, 0.090)\). If the true class is the second, the loss is \(-\log 0.245 = 1.41\) and the gradient is \((0.665, -0.755, 0.090)\): lower the first score, raise the second, lower the third a little.
Shift the scores to \((102, 101, 100)\): identical probabilities. Divide by \(T = 0.5\), giving \((4, 2, 0)\): probabilities \((0.867, 0.117, 0.016)\), sharper.
Numerical stability
\(e^{1000}\) overflows. Using shift invariance, real code subtracts the largest score first:
\(\operatorname{softmax}(\mathbf{z}) = \operatorname{softmax}(\mathbf{z} - \max_j z_j)\), so every exponent is at most
0. For the loss, it computes \(\log\sum_j e^{z_j}\) the same way ("log-sum-exp") instead of taking the log of a
probability that may have rounded to 0. Libraries provide a combined cross_entropy(logits, target) for
exactly this reason.
Where softmax appears
- Classifier outputs. The last layer of nearly every multi-class network.
- Language models. Choosing the next token is a softmax over the whole vocabulary, often 50,000 to 250,000 classes; the sampling "temperature" is the \(T\) above.
- Attention. Each word's attention weights are a softmax over its similarity to every other word; see Attention.
Pitfalls
- Exclusive classes only. Softmax makes classes compete: more for one means less for the rest. If an example can belong to several classes at once, use an independent sigmoid per class.
- Overconfidence. Networks trained with cross-entropy push scores apart and often give 0.99 to answers that are right far less often. Label smoothing (training toward 0.9 instead of 1) and temperature scaling after training are common fixes.
- Probabilities, not certainty. A softmax always produces a confident-looking distribution, even for inputs unlike anything in training. It has no "none of the above".
History
The formula is the Boltzmann distribution of statistical physics (1868), where \(T\) really is temperature and the scores are negative energies. In statistics it is the multinomial logit model (David Cox and others, 1960s; Daniel McFadden's econometric choice models, which won the 2000 Nobel Prize in economics). John Bridle named it "softmax" for neural networks in 1989.
Connections
Builds on
- Logistic RegressionSoftmax generalises logistic regression from two classes to many; with two classes it is exactly the sigmoid.
- Loss FunctionsIts loss is cross-entropy over all classes, the multi-class form of the logistic loss.
Leads to
- AttentionThe weights come from a softmax over query–key scores, so each word's weights sum to one.
Further reading
- Minimal Neural Network Case Study (Stanford CS231n)The same three spirals, softmax and gradient, worked through in NumPy step by step.
- Softmax classifier and its numeric stability (Stanford CS231n)Where the cross-entropy comes from, and why subtracting the max is safe.
- Neural networks: Multi-class classification (Google Machine Learning Crash Course)Softmax next to one-vs-all, and when each one fits.