Dive into Deep Learning · §3.1
Softmax Regression: Linear Models for Categorical Probabilities
one-hot encoding · affine logits · softmax · shift invariance · cross-entropy · \hat{\mathbf{y}} - \mathbf{y} gradient · temperature
Motivation & Failure Mode
Sorting clothing photos into q = 10 unordered categories via continuous scalar regression breaks down immediately:
Target Representation
Consider a 2 \times 2 grayscale image with four pixel features (x_1, x_2, x_3, x_4) and q = 3 classes—cat (1), chicken (2), and dog (3):
The Linear Layer
Softmax regression assigns each class j \in \{1, \ldots, q\} its own weight vector \mathbf{w}_j \in \mathbb{R}^d and bias b_j \in \mathbb{R}:
Probability Normalization
The softmax operator exponentiates each logit and divides by their partition sum:
\hat{y}_j = \mathrm{softmax}(\mathbf{o})_j = \frac{\exp(o_j)}{\sum_{k=1}^q \exp(o_k)}
Structural Properties of Softmax
Exponentials convert additive shifts into a common factor that cancels out:
\mathrm{softmax}(\mathbf{o} + c\mathbf{1})_j = \frac{\exp(c)\exp(o_j)}{\exp(c)\sum_{k=1}^q \exp(o_k)} = \mathrm{softmax}(\mathbf{o})_j
float32 overflow (o > 88.7).Connection to Binary Classification
When q = 2, shift invariance implies that the two probabilities depend solely on the scalar difference o = o_1 - o_2:
\hat{y}_1 = \frac{1}{1 + \exp(-(o_1 - o_2))} = \sigma(o_1 - o_2)
Batched Matrix Formulation
In code, we process a minibatch of n examples with d features in a single GEMM followed by a rowwise softmax:
\mathbf{O} = \mathbf{X}\mathbf{W} + \mathbf{b}, \qquad \hat{\mathbf{Y}} = \mathrm{softmax}(\mathbf{O})
axis=1, keepdims=True).Linear Decision Boundaries
Because softmax preserves logit ordering, class j wins wherever o_j \ge o_k for all k \neq j:
Probabilistic Loss Derivation
The negative log-likelihood of observing true class y (one-hot \mathbf{y} \in \{0, 1\}^q) under \hat{y}_j = P(y = j \mid \mathbf{x}) is the cross-entropy loss:
l(\mathbf{y}, \hat{\mathbf{y}}) = -\sum_{j=1}^q y_j \log \hat{y}_j = -\log \hat{y}_y
Optimization Pitfall: Vanishing Gradients on Blunders
Minimizing squared error \frac{1}{2}\|\hat{\mathbf{y}} - \mathbf{y}\|_2^2 on softmax outputs stalls on confident blunders:
Exact Gradient Derivation
Writing cross-entropy directly in terms of the logits \mathbf{o} via the log-sum-exp function:
l(\mathbf{y}, \hat{\mathbf{y}}) = \log \sum_{k=1}^q \exp(o_k) - o_y
Differentiating with respect to o_j yields the exact residual vector:
\nabla_{\mathbf{o}}\, l = \hat{\mathbf{y}} - \mathbf{y}
Chain Rule to Parameters
Backpropagating through \mathbf{O} = \mathbf{X}\mathbf{W} + \mathbf{b} gives the minibatch parameter gradients:
\nabla_{\mathbf{W}} L = \frac{1}{n}\mathbf{X}^\top(\hat{\mathbf{Y}} - \mathbf{Y}), \qquad \nabla_{\mathbf{b}} L = \frac{1}{n}(\hat{\mathbf{Y}} - \mathbf{Y})^\top\mathbf{1}_n
Why It Is Called Cross-Entropy
In Shannon’s information theory, an event of probability p carries surprisal -\log p:
Accuracy vs. Differentiable Training
Define the classification margin of an example as its true-class logit minus the highest competing logit, m = o_y - \max_{j \neq y} o_j:
Confidence vs. Decision Boundaries
Because cross-entropy keeps decreasing for m > 0, minimizing it pushes predicted probabilities toward 0 and 1 even after every decision is already correct:
Concept Check
Work out the following three questions from the properties of softmax and cross-entropy:
Answers: (1) Subtracting 100 from every logit gives \mathrm{softmax}(2, 0, 0) \approx (0.79, 0.11, 0.11), with ratio e^{102-100} = e^2 \approx 7.39. (2) (0.6, 0.4) gives H(\mathbf{p}) = -0.6\log 0.6 - 0.4\log 0.4 \approx 0.67\text{ nats}, beating (0.9, 0.1) at -0.6\log 0.9 - 0.4\log 0.1 \approx 0.98\text{ nats}. (3) Total loss is 60{,}000 \times 0.10 = 6{,}000\text{ nats}, and each mistake (m \le 0) costs at least \log 2 \approx 0.693\text{ nats}, so at most 6{,}000 / 0.693 \approx 8{,}656 errors (14.4\%).
Section Summary
| Concept | Mathematical Form | Key Property / Practical Role |
|---|---|---|
| One-hot target | \mathbf{y} \in \{0, 1\}^q,\; y_y = 1,\; \sum_j y_j = 1 | Places all pairs of classes at equal distance \sqrt{2}; valid categorical distribution |
| Batched affine logits | \mathbf{O} = \mathbf{X}\mathbf{W} + \mathbf{b} \in \mathbb{R}^{n \times q} | d \cdot q + q parameters (7{,}850 on Fashion-MNIST); boundaries are flat hyperplanes |
| Softmax operator | \hat{y}_j = \exp(o_j) / \sum_{k=1}^q \exp(o_k) | Order-preserving (\operatorname{argmax} \hat{y}_j = \operatorname{argmax} o_j); shift-invariant (\mathrm{softmax}(\mathbf{o}+c\mathbf{1}) = \mathrm{softmax}(\mathbf{o})) |
| Log-odds & q=2 limit | o_i - o_j = \log(\hat{y}_i / \hat{y}_j),\; \hat{y}_1 = \sigma(o_1 - o_2) | Logit differences are log-odds; 2-class softmax equals binary logistic regression |
| Cross-entropy loss | l(\mathbf{y}, \hat{\mathbf{y}}) = -\log \hat{y}_y = \log\sum_k \exp(o_k) - o_y | Negative log-likelihood; convex in (\mathbf{W}, \mathbf{b}); upper-bounds 0\text{--}1 error (\ge \log 2 on mistakes) |
| Logit & weight gradients | \nabla_{\mathbf{o}} l = \hat{\mathbf{y}} - \mathbf{y},\; \nabla_{\mathbf{W}} L = \frac{1}{n}\mathbf{X}^\top(\hat{\mathbf{Y}}-\mathbf{Y}) | Prediction minus target; non-vanishing on confident mistakes (unlike squared error) |
| Temperature scaling | \hat{\mathbf{y}}(T) = \mathrm{softmax}(\mathbf{o} / T),\; T > 0 | Tunes confidence (T \to 0 argmax, T \to \infty uniform) without changing class predictions |