Skip to content
Lambdian

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 zk=wk⊤x+bkz_k = \mathbf{w}_k^\top\mathbf{x} + b_k. 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 wk\mathbf{w}_k leaves the probabilities unchanged, so the weights are not unique; L2 regularisation picks the smallest solution. Dividing scores by a temperature TT before the softmax makes the distribution sharper (T<1T<1) or flatter (T>1T>1), a trick used in model distillation and in sampling from language models.

The algorithm, step by step

  1. 1
    Encode labels as one-hot vectors Y∈{0,1}n×KY \in \{0,1\}^{n\times K}.
  2. 2
    Initialise W∈Rd×KW \in \mathbb{R}^{d\times K} and b\mathbf{b} to zeros.
  3. 3
    Repeat: Z=XW+bZ = XW + \mathbf{b}; P=softmax⁡(Z)P = \operatorname{softmax}(Z) row-wise; W←W−η X⊤(P−Y)/nW \leftarrow W - \eta\,X^\top(P - Y)/n; b←b−η 1⊤(P−Y)/n\mathbf{b} \leftarrow \mathbf{b} - \eta\,\mathbf{1}^\top(P-Y)/n.
  4. 4
    Predict arg⁡max⁡kPik\arg\max_k P_{ik}.

The maths

Softmax

softmax⁡(z)k=ezk∑jezj\operatorname{softmax}(\mathbf{z})_k = \frac{e^{z_k}}{\sum_j e^{z_j}}

Cross-entropy

J(W)=−1n∑i∑kYiklog⁡PikJ(W) = -\frac1n\sum_{i}\sum_k Y_{ik}\log P_{ik}

Gradient (matrix form)

∇WJ=1nX⊤(P−Y)\nabla_W J = \frac1n X^\top(P - Y)

Boundary between classes j and k

(wj−wk)⊤x+(bj−bk)=0(\mathbf{w}_j - \mathbf{w}_k)^\top\mathbf{x} + (b_j - b_k) = 0

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…

Multi-class problems where a linear model is good enough, or as the final layer of a network.

Check your understanding

Question 1

Q1What happens to softmax probabilities if you add 100 to every score?

Question 2

Q2For a 3-class problem, what is the gradient of the cross-entropy with respect to score zkz_k for one example?

Question 3

Q3An image can contain both a cat and a dog. Should the output use softmax?

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.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.