Softmax (multinomial logistic) regression
Logistic regression for many classes: one linear score per class, normalised by the softmax.
Animated explanation
The idea
With more than two classes (digits 0–9, ten kinds of plankton), give each class its own linear score . The class with the highest score wins.
To get probabilities, exponentiate the scores and divide by their sum: the softmax. Training minimises the cross-entropy, which simply asks the model to put high probability on the right class. Softmax plus cross-entropy is also exactly the output layer of almost every neural network classifier, so this lesson pays off later.
Softmax versus one-vs-rest
An alternative is one-vs-rest: train K separate binary logistic regressions ("class k or not?") and pick the most confident. It works, but the K probabilities need not sum to one and are trained independently. Softmax trains all classes jointly and gives a proper distribution. With two classes softmax reduces exactly to logistic regression (only the difference of the two scores matters).
Overparameterisation and temperature
Adding the same vector to every leaves the probabilities unchanged, so the weights are not unique; L2 regularisation picks the smallest solution. Dividing scores by a temperature before the softmax makes the distribution sharper () or flatter (), a trick used in model distillation and in sampling from language models.
The algorithm, step by step
- 1Encode labels as one-hot vectors .
- 2Initialise and to zeros.
- 3Repeat: ; row-wise; ; .
- 4Predict .
The maths
Softmax
Cross-entropy
Gradient (matrix form)
Boundary between classes j and k
Strengths and weaknesses
Strengths
- Proper probability distribution over K classes
- Convex, fast, interpretable per class
- The standard output layer for neural classifiers
Weaknesses
- Linear (convex polygon) decision regions
- Assumes classes are mutually exclusive (use sigmoids for multi-label)
Reach for it when…
Check your understanding
Lambdian Lab
Softmax regression on three classes
Implement multinomial logistic regression.
- softmax(Z): row-wise, numerically stable.
- one_hot(y, K).
- fit_softmax(X, y, K, lr, steps): gradient descent; return (W, b) with W of shape (d, K).
- predict(X, W, b): the most probable class.