Foundations to Frontier
Tests
D. Classical Machine Learning › D2 Linear Classifiers

Multinomial (Softmax) Regression

Softmax regression extends logistic regression to K classes: each class gets a linear score, and the softmax function turns the K scores into probabilities that sum to one. It is fitted by minimising the cross-entropy, a convex problem with a simple gradient Xᵀ(P − Y). It is the output layer of nearly every neural classifier. This page covers the model, the softmax and its numerical stability, identifiability (only score differences matter), the gradient and Hessian, temperature, the K = 2 reduction to logistic regression, and the comparison with one-vs-rest. In the exercises you implement softmax regression by gradient descent, check identifiability, calibration-in-the-large and the K = 2 case; then you classify rover terrain into four types and compare softmax with one-vs-rest logistic regression on accuracy, log loss and calibration.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

One score per class, then share out the probability

To tell sand from gravel from bedrock, give every terrain type its own linear score from the measurements. The highest score is the best guess. To turn scores into probabilities, exponentiate each score (making it positive) and divide by the total, so they add up to 1. That is the softmax. Training adjusts the scores so the true class gets high probability.

Softmax: three scores become three probabilities
class As = 0.560.453
class Bs = 0.560.453
class Cs = -1.020.093
entropy of the prediction: 0.939 nats (max 1.099)
Colour: the most probable class; opacity: how confident. Boundaries are where two scores tie, so they are straight lines meeting at a point, and dividing every score by a temperature changes the confidence but not the boundaries. Adding the same constant to all three scores changes nothing at all.
Why engineers care

Multi-class decisions are everywhere: terrain type for a rover, fault mode for a spacecraft subsystem, object class for a camera. Softmax regression is the simplest calibrated multi-class model and the final layer of image, speech and language networks. Understanding it means understanding every classifier’s last step.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Softmax probabilities always…
  2. 2. The predicted class is the one with…
  3. 3. In the demo, raising the temperature…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Softmax by hand

Scores for one rock image: sand 2, gravel 1, bedrock 0.

Class Score s eˢ Probability eˢ/Σ
sand 2 7.389 0.665
gravel 1 2.718 0.245
bedrock 0 1.000 0.090
total 11.107 1
  • Shift invariance. Adding 100 to every score leaves the probabilities unchanged. That is why the stable implementation subtracts the maximum score first.
  • Loss. If the rock is truly gravel, the cross-entropy is −log 0.245 = 1.406.
  • Gradient. For these scores it is p − y = (0.665, −0.755, 0.090): raise gravel’s score and lower the others in proportion to their probabilities.

The steps in plain English:

  1. Give every class k a weight vector wₖ; score sₖ = wₖ·x.
  2. Probabilities pₖ = e^(sₖ) / Σⱼ e^(sⱼ). Subtract the largest score first, for numerical safety.
  3. Loss = −log p(true class), averaged over examples (cross-entropy).
  4. Gradient for wₖ = Σᵢ (pᵢₖ − yᵢₖ)xᵢ / n; take steps until the loss stops falling.
  5. Predict the class with the largest probability, and use the probabilities for risk-aware decisions.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Softmax of scores (0, 0)?
  2. 2. Cross-entropy when the true class has probability 1?
  3. 3. Gradient entry for the true class is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The model and its training

Model: P(y = k | x) = exp(xᵀwₖ) / Σⱼ exp(xᵀwⱼ), with k = 1…K.

Identifiability. Adding the same vector c to every wₖ leaves all probabilities unchanged. Either fix one class’s weights at 0 (the reference class, as statsmodels does) or add an L2 penalty, which picks the solution with Σₖ wₖ = 0 (as scikit-learn does).

Cross-entropy and gradient. L(W) = −(1/n) Σᵢ log P(yᵢ | xᵢ), with ∇_W L = Xᵀ(P − Y)/n, where Y is the one-hot label matrix. L is convex in W. The Hessian is the Kronecker-structured (1/n) Σᵢ (diag(pᵢ) − pᵢpᵢᵀ) ⊗ xᵢxᵢᵀ. Newton’s method is affordable only for small p·K; L-BFGS or gradient descent are the usual choices.

K = 2. The model reduces to logistic regression with β = w₁ − w₀.

Log-odds between classes: log(Pⱼ/Pₖ) = xᵀ(wⱼ − wₖ). The boundaries between pairs of classes are hyperplanes.

Temperature. softmax(s/T) keeps the argmax but changes confidence. Fitting a single T on validation data is temperature scaling, the standard calibration fix for neural networks.

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(solver="lbfgs", C=1e4).fit(X, y)        # multinomial (softmax) by default for K > 2
clf.predict_proba(X_new)
import scipy.special as sp; sp.softmax(scores, axis=1); sp.log_softmax(scores, axis=1)

Use it when classes are mutually exclusive and you want calibrated probabilities from a linear model. For multi-label problems (several labels can be true at once), use independent sigmoids instead.

🐍 Exercise 1: softmax regression
Implement a stable softmax, one-hot encoding, cross-entropy and softmax regression by gradient descent; recover known weights up to the shift ambiguity, check that the mean predicted probabilities equal the class frequencies, and show that with K = 2 the weight difference equals logistic regression's coefficients.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Gradient of the cross-entropy with respect to W is…
  2. 2. Softmax weights are identifiable only up to…
  3. 3. Temperature scaling changes…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory and the alternatives

Generative link. If x | y = k ~ N(μₖ, Σ) with a shared covariance, then P(y | x) is exactly softmax-linear (LDA, D2.28). Softmax regression is the discriminative counterpart: it models P(y | x) directly with fewer assumptions.

Maximum entropy. Among all conditional distributions whose expected feature values match the data, the softmax model has maximum entropy. This is why NLP calls it the “maximum entropy classifier” (D3.38).

Calibration-in-the-large. With an intercept, the score equations give Σᵢ pᵢₖ = nₖ: the mean predicted probability of each class equals its frequency.

One-vs-rest (OvR): fit K separate binary logistic models.

  • They are trained on different problems, so their outputs do not sum to 1. Renormalising helps, but the result is not a proper joint model.
  • Regions where every model says “not me” are resolved arbitrarily.
  • OvR is easy to parallelise, and it is the usual reduction for binary-only learners such as SVMs.

Other reductions: one-vs-one (K(K − 1)/2 pairwise models, used by libsvm) and error-correcting output codes.

Interview questions.

  • Softmax or sigmoid outputs? Softmax for mutually exclusive classes. Independent sigmoids for multi-label problems.
  • Why subtract the max? e^1000 overflows. Shift invariance makes the subtraction free, and log-softmax = s − logsumexp(s) is stable too.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Softmax regression is also called…
  2. 2. Raw one-vs-rest probabilities…
  3. 3. One-vs-one with K = 5 classes trains how many models?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Classifying terrain for a rover

History.

  • Luce’s choice axiom (1959) and McFadden’s conditional logit (1974; Nobel Prize 2000) used the softmax for discrete choice.
  • Bridle (1990) named the “softmax” for neural-network outputs.
  • Berger, Della Pietra and Della Pietra (1996) popularised maximum-entropy models in NLP.
  • Guo et al. (2017) showed that modern networks are overconfident and fixed it with temperature scaling.

Exercise 1. Three classes, n = 1500. The recovered weights relative to class 0 are within 0.3 of the truth (sampling error). The mean predicted probabilities match the class frequencies to 0.002. The cross-entropy is 0.621, against log 3 = 1.099 for uniform guessing. With K = 2, the weight difference equals logistic regression’s coefficients to within 0.02.

Exercise 2: rover terrain. Setup: 4 classes (sand, regolith, gravel, bedrock) from thermal inertia, albedo and roughness; Gaussian classes with shared covariance, so the true model is softmax. Train on 400 points; 20 repetitions, each scored on 4000 test points.

Method Accuracy Log loss Top-label ECE
Softmax regression 0.670 0.730 0.026
One-vs-rest logistic (renormalised) 0.642 0.845 0.113
  • The joint model wins on every metric. Its gain in log loss (0.115) and calibration (4 times better) is larger than its gain in accuracy (2.8 points).
  • OvR’s raw probabilities are off from summing to 1 by 0.31 on average. The middle classes (regolith, gravel) are not separable from “the rest” by a single line, so their binary models are poor, and renormalisation cannot repair that.
🐍 Exercise 2 (research): terrain classes, softmax versus one-vs-rest
Simulate four terrain classes as Gaussian blobs with shared covariance in three features; compare multinomial softmax regression and renormalised one-vs-rest logistic regression by test accuracy, log loss and top-label calibration error, and measure how far raw OvR probabilities are from summing to one.

Softmax regression in AI, robotics and aerospace

  • Neural network outputs: the final layer of image, speech and language classifiers.
  • Terrain and fault classification: calibrated multi-class probabilities for planners and FDIR.
  • Attention: softmax over similarity scores in transformers (E12).
  • Policy networks: softmax over discrete actions in reinforcement learning.

References

  1. D. McFadden (1974). “Conditional logit analysis of qualitative choice behavior.” In Frontiers in Econometrics.
  2. J. S. Bridle (1990). “Probabilistic interpretation of feedforward classification network outputs.” Neurocomputing, NATO ASI F68.
  3. A. L. Berger, S. A. Della Pietra, V. J. Della Pietra (1996). “A maximum entropy approach to natural language processing.” Computational Linguistics 22(1).
  4. C. Guo, G. Pleiss, Y. Sun, K. Q. Weinberger (2017). “On calibration of modern neural networks.” ICML.
  5. C. M. Bishop (2006). Pattern Recognition and Machine Learning, section 4.3.4. Springer.