๐Ÿ“ˆ Machine Learning ยท Lecture 8 of 47

Softmax Regression and Multiclass Classification Strategies

How do we classify into more than two classes? We generalise logistic regression to softmax regression, derive its gradient, and compare it with one-vs-rest and one-vs-one strategies.

Most real classification problems have more than two classes: ten digits, dozens of document categories, thousands of product types, fifty thousand vocabulary words for next-token prediction. Today we extend logistic regression to $K$ classes. The result โ€” softmax regression (multinomial logistic regression) โ€” is also exactly the final layer of almost every neural network classifier.

The softmax function#

Give each class $k$ its own weight vector $\mathbf{w}_k$ and compute a score (logit) $z_k = \mathbf{w}_k^\top\mathbf{x}$. Convert scores to probabilities with the softmax:

$$ P(y = k \mid \mathbf{x}) = \text{softmax}(\mathbf{z})_k = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}} $$

Properties:

  • Outputs are positive and sum to one โ€” a valid categorical distribution.
  • It preserves order: the largest logit gets the largest probability.
  • It is invariant to adding a constant to all logits (so one weight vector is redundant; regularisation resolves this).
  • For $K = 2$, softmax reduces to the sigmoid of the difference of logits.

The name comes from being a smooth ("soft") version of the argmax. Scaling logits by a temperature $T$, $\text{softmax}(\mathbf{z}/T)$, makes the distribution sharper ($T < 1$) or flatter ($T > 1$) โ€” a knob we will meet again in knowledge distillation and language-model sampling.

Loss: categorical cross-entropy#

With a one-hot label $\mathbf{y}$ and predicted probabilities $\mathbf{p}$:

$$ J = -\frac{1}{n}\sum_{i=1}^{n}\sum_{k=1}^{K} y_{ik}\ln p_{ik} = -\frac{1}{n}\sum_{i=1}^{n}\ln p_{i, y_i} $$

โ€” the negative log-probability of the correct class, i.e. the negative log-likelihood under a categorical model.

The gradient#

Remarkably, the gradient with respect to the logits is again "prediction minus target":

$$ \frac{\partial J_i}{\partial \mathbf{z}_i} = \mathbf{p}_i - \mathbf{y}_i $$

so for the weight matrix $\mathbf{W} \in \mathbb{R}^{d \times K}$ (columns $\mathbf{w}_k$):

$$ \nabla_{\mathbf{W}}J = \frac{1}{n}\mathbf{X}^\top(\mathbf{P} - \mathbf{Y}) $$

The loss is convex in $\mathbf{W}$, so training finds the global optimum.

python
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

X, y = load_digits(return_X_y=True)                  # 8x8 digit images, 10 classes
X = X / 16.0
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)

def softmax(Z):
    Z = Z - Z.max(axis=1, keepdims=True)             # numerical stability
    E = np.exp(Z)
    return E / E.sum(axis=1, keepdims=True)

K, d = 10, X.shape[1]
W, b = np.zeros((d, K)), np.zeros(K)
Y = np.eye(K)[y_tr]                                  # one-hot labels
for epoch in range(500):
    P = softmax(X_tr @ W + b)
    G = (P - Y) / len(X_tr)                          # dJ/dZ
    W -= 0.5 * (X_tr.T @ G + 1e-4 * W)
    b -= 0.5 * G.sum(axis=0)
    if epoch % 100 == 0:
        loss = -np.log(P[np.arange(len(y_tr)), y_tr] + 1e-12).mean()
        print(epoch, round(loss, 4))

acc = (softmax(X_te @ W + b).argmax(1) == y_te).mean()
print("test accuracy:", round(acc, 3))

A simple linear softmax model reaches roughly 95% accuracy on this small digits dataset.

Alternative strategies: reducing multiclass to binary#

Any binary classifier can be extended to $K$ classes:

One-vs-Rest (OvR)#

Train $K$ binary classifiers, each separating one class from all others; predict the class with the highest score. Simple and scalable, but each classifier faces an imbalanced problem, and scores from separately trained classifiers may not be comparable.

One-vs-One (OvO)#

Train a classifier for every pair of classes โ€” $K(K-1)/2$ of them โ€” and predict by majority vote. Each classifier trains on less data, which suits algorithms that scale poorly with dataset size (such as kernel SVMs), but the number of models grows quadratically.

StrategyModels trainedProbabilitiesTypical use
Softmax (multinomial)1Coherent, sum to 1Default for linear models and neural nets
One-vs-Rest$K$Need normalisationLinear SVMs, very many classes
One-vs-One$K(K-1)/2$From votesKernel SVMs

Multi-label is different#

In multi-label classification, each example can belong to several classes at once (a photo containing both "beach" and "dog"). Softmax is wrong here because it forces classes to compete. Use independent sigmoids โ€” one binary logistic output per label โ€” with binary cross-entropy summed over labels.

Evaluating multiclass models#

  • Confusion matrix โ€” which classes are confused with which.
  • Per-class precision/recall/F1, combined with macro averaging (treats classes equally โ€” good for imbalanced data) or micro/weighted averaging.
  • Top-k accuracy โ€” whether the true class is among the $k$ most probable (standard for ImageNet with 1,000 classes).
JA
Written by

Janin A Apurba

B.Sc. in CSE, AUST ยท Advanced ICT Officer, CNRS-UNHCR. Teaching AI, ML and Deep Learning to the next generation of engineers and researchers.

Keep learning

Related lectures

๐Ÿ“ˆ Machine Learning

Logistic Regression: Probabilistic Classification Done Right

Despite its name, logistic regression is a classifier โ€” and one of the most reliable. We derive it from log-odds, train it by maximum likelihood, interpret its coefficients and understand its decision boundary.

Beginnerโฑ 5 min#056
๐Ÿ“ˆ Machine Learning

k-Nearest Neighbours: Learning by Similarity

The simplest learning algorithm stores the data and asks the neighbours. We analyse k-NN's biasโ€“variance behaviour, distance choices, scaling, efficient search structures and its surprising theoretical guarantees.

Beginnerโฑ 5 min#064
๐Ÿ“ˆ Machine Learning

Support Vector Machines: Maximum-Margin Classification

Among all separating hyperplanes, SVMs pick the one with the widest margin. We derive the hard- and soft-margin formulations, the hinge loss, support vectors and the role of the C parameter.

Intermediateโฑ 5 min#072