1.7 — From binary to multi-class: the softmax
Goal: Derive the softmax as the multinomial-logit MLE for K-class classification and connect it to the binary case and to the log-sum-exp identity.
1. Why this session exists
The sigmoid (1.2) handled . For we need a probability-simplex output. The softmax is the canonical generalisation. It will play four roles:
- The natural multinomial extension of the sigmoid (this session).
- The MLE of a multinomial-logit observation model (this session).
- The output head of essentially every modern classifier and language model (Chapters 5–8).
- The gradient sink with the cleanest backward pass in all of deep learning, , generalising 1.2 — the topic of 1.8.[1]
2. Setup
Labels: . We will mostly work with the one-hot encoding,
Per-class linear scores ("logits"):
stacked into a vector . The weight matrix is with rows ; the bias is .
3. The softmax
Define the softmax as
Three properties.
Probability simplex. Each entry is in , and the entries sum to :
So is a valid distribution over the classes.
Shift invariance. From 0.10 §4, for any scalar . We use this to stabilise the computation by subtracting .
Reduction to sigmoid. For with logits ,
So the two-class softmax depends only on the difference of logits, which is the sigmoid of the difference. Setting recovers the binary sigmoid; the binary case is over-parameterised, which is why for we usually use one logit and .
4. The multinomial-logit observation model
Postulate that, conditional on ,
This is the multinomial-logit model — the multi-class generalisation of the Bernoulli logistic model in 1.2 (which collapses out of ).
The data likelihood for a single example with one-hot label :
i.e. just the predicted probability of the correct class — the product is for every except the true class.
5. MLE = cross-entropy
The log-likelihood for examples is
Negating and averaging gives the multi-class cross-entropy training loss:
The same construction as 1.2 (BCE) and 1.1 (least squares): negative average log-likelihood of an observation model. For one-hot the inner sum reduces to picking out the predicted probability of the true class, .
6. Cross-entropy on logits via log-sum-exp
Inserting the definition of softmax,
The right-hand side is the log-sum-exp expression from 0.10 §4. Two consequences.
Stability for free. Pair this loss with the stable log-sum-exp implementation and you get a fused softmax + cross-entropy that never overflows.
Direct link to information theory. is the information content of observing under the model. Summed over the dataset and divided by , it is the empirical cross-entropy between the data distribution and the model (0.6, 0.7). Minimising it is the Cover-and-Thomas "code-length minimisation" reading of MLE.
7. Decision rule and Bayes optimality
At inference, the classifier predicts (since is monotone). If the multinomial-logit model is correctly specified, this rule coincides with the Bayes-optimal classifier that minimises the misclassification rate.
In practice the model is misspecified (the world is not log-linear in the raw features), but the softmax + MLE pairing remains an excellent default — it is convex in , scales linearly with , and gives calibrated probabilities under mild assumptions.
8. Takeaways
- Softmax: smooth, simplex-valued, shift-invariant, reduces to sigmoid for .
- Multinomial-logit model: .
- MLE of this model = multi-class cross-entropy loss.
- Combined with log-sum-exp, the loss admits a stable fused computation directly on logits.
(a) Verify the reduction for and conclude that binary logistic regression is a special case of multi-class softmax regression with .
(b) Show shift-invariance algebraically: . Conclude that the multinomial-logit model has a redundant parameter — shifting all by the same vector and all by the same scalar leaves the model unchanged.
(c) Compute in the two cases and . (We'll need this in 1.8.)
(d) Show that as the inverse temperature , . What is the role of if you instead want to sample from the model?
References
- [1]
Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. — Linear regression (1.1), logistic regression (1.2–1.3), softmax (1.7), MAP / Gaussian priors.
↩