Softmax Regression
Softmax regression, also called multinomial logistic regression, extends logistic regression to classes. Each class has a weight vector and a bias . They give the class a score called a logit, . The softmax turns the logits into probabilities:
Each lies in , and the probabilities sum to one. Stacking the as rows gives , so the logits are . The prediction is the argmax rule of linear discriminants. The decision regions are therefore convex, with linear boundaries.The label is encoded one-hot, as a vector with a single at the true class . Maximum likelihood under the categorical distribution gives the cross-entropy loss of one sample:
Here is the vector of predicted probabilities and is the logit of the true class. The training loss sums over the samples. The gradients for one sample are
For a whole dataset, put the probabilities and the one-hot labels in the rows of . The weight gradient is then . The loss is convex in and has no closed-form minimizer. Training uses gradient methods or Newton’s method, as in the binary case.Three facts connect the model to its neighbors:
- Shift invariance: adding the same vector to every and the same constant to every leaves every unchanged. One class is redundant, and fixing and removes the redundancy.
- Two classes: with , with the sigmoid . This is logistic regression with and .
- Separable data: as in logistic regression, the weights grow without bound. An penalty keeps them finite.
Intuition
Logistic regression scores one class against the other. Softmax regression gives every class its own linear score and turns all the scores into probabilities at once. Exponentiating makes every score positive and turns differences into ratios. If one logit exceeds another by , its class is times as likely. Dividing by the sum then makes the probabilities add to one. The largest logit gets the largest share, and the other classes keep some probability. The name “softmax” refers to this smoothing. The output is a smoothed one-hot vector that marks the argmax, so “soft argmax” would be more accurate.
The loss looks only at the probability of the true class. Its gradient is the gap between the predicted distribution and the one-hot target. Gradient descent raises the true class’s logit and lowers every other logit. Each logit moves in proportion to its error.
Properties
(Redundant Parameters) Without a penalty the minimizer is not unique, and an penalty selects the weights that sum to zero.
By shift invariance, every weight matrix has a family of copies, for any , with the same loss. The Hessian is therefore singular along these directions. The penalty is smallest at , so the penalized minimizer has . Unpenalized biases stay redundant, which does not affect predictions. Statistics software usually fixes a baseline class instead (ISLR, sec. 4.3.5). Each then gives the log-odds of class against class . A different baseline changes every coefficient and no prediction.
(The Error Signal) The logit gradient equals for the true class and for each wrong class .
The entries of sum to zero. The weight update is a rank-one matrix: each class row moves along , by an amount set by that class’s error. The gradient vanishes only when , which needs infinitely large logit gaps. So the loss of a sample is never exactly zero. The simple form comes from a cancellation. The softmax Jacobian is , where is when and otherwise. The derivative of multiplies it into . Libraries therefore fuse the softmax and the loss into one function that takes logits, such as PyTorch’s CrossEntropyLoss.
(Temperature) Dividing the logits by a temperature changes how confident the probabilities are without changing the predicted class.
The tempered softmax is . As it approaches the one-hot argmax, and as it approaches the uniform distribution . For logits , gives , gives , and gives . Temperature scaling (Guo et al. 2017) fits one on validation data by minimizing log loss, which calibrates overconfident networks. Knowledge distillation (Hinton, Vinyals and Dean 2015) trains a small model on a large model’s outputs at . Language models sample the next token at a chosen temperature, with lower giving more predictable text.
Linear scores split the plane into three argmax regions. The bars show the softmax probabilities at a moving probe point. Changing changes the bars but not the regions.
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{grn}{HTML}{2F9E44}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}, xshift=3cm]
\draw (-3.0,-3.0) rectangle (3.0,3.0);
\draw[very thick] (0.06,-0.16) -- (1.04,3.00);
\draw[very thick] (0.06,-0.16) -- (-3.00,-0.82);
\draw[very thick] (0.06,-0.16) -- (2.90,-3.00);
\draw[densely dotted] (0.06,-0.16) circle (1.7);
\fill[hot] (1.75,2.36) rectangle (1.93,2.54);
\node[anchor=west, inner sep=1pt] at (1.97,2.45) {class $1$};
\fill[cold] (-2.35,1.81) rectangle (-2.17,1.99);
\node[anchor=west, inner sep=1pt] at (-2.13,1.90) {class $2$};
\fill[grn] (-1.00,-2.59) rectangle (-0.82,-2.41);
\node[anchor=west, inner sep=1pt] at (-0.78,-2.50) {class $3$};
\draw (4.55,-2.7) -- (7.85,-2.7);
\node[below] at (5.15,-2.7) {$p_1$};
\node[below] at (6.20,-2.7) {$p_2$};
\node[below] at (7.25,-2.7) {$p_3$};
\draw (4.50,-2.7) -- (4.50,1.10);
\draw (4.50,-2.70) -- (4.40,-2.70) node[left] {\footnotesize $0$};
\draw (4.50,-0.80) -- (4.40,-0.80) node[left] {\footnotesize $0.5$};
\draw (4.50,1.10) -- (4.40,1.10) node[left] {\footnotesize $1$};
\node[anchor=west] at (3.9,3.3) {$p_k = e^{z_k/T} / \sum_j e^{z_j/T}$, \ $z_k = \mathbf w_k^T\mathbf x + b_k$};
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.6s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.02);
\node[above] at (5.15,0.02) {\footnotesize $0.72$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.14);
\node[above] at (6.20,-2.14) {\footnotesize $0.15$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.18);
\node[above] at (7.25,-2.18) {\footnotesize $0.14$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {In region $1$, class $1$ has the highest score and the largest probability.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.6s = "true", 2.82s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.63,0.50) circle (4pt);
\fill (1.63,0.50) circle (2pt);
\node[inner sep=1pt] at (1.99,0.71) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.01);
\node[above] at (5.15,-0.01) {\footnotesize $0.71$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.04);
\node[above] at (6.20,-2.04) {\footnotesize $0.17$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.25);
\node[above] at (7.25,-2.25) {\footnotesize $0.12$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.33, -0.71)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves toward the boundary between regions $1$ and $2$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.82s = "true", 3.04s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.30,1.00) circle (4pt);
\fill (1.30,1.00) circle (2pt);
\node[inner sep=1pt] at (1.60,1.30) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.25);
\node[above] at (5.15,-0.25) {\footnotesize $0.65$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-1.71);
\node[above] at (6.20,-1.71) {\footnotesize $0.26$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.34);
\node[above] at (7.25,-2.34) {\footnotesize $0.09$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.98, 0.07, -0.94)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves toward the boundary between regions $1$ and $2$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.04s = "true", 3.26s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (0.82,1.36) circle (4pt);
\fill (0.82,1.36) circle (2pt);
\node[inner sep=1pt] at (1.12,1.66) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.71);
\node[above] at (5.15,-0.71) {\footnotesize $0.52$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-1.22);
\node[above] at (6.20,-1.22) {\footnotesize $0.39$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.38);
\node[above] at (7.25,-2.38) {\footnotesize $0.08$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.76, 0.47, -1.06)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves toward the boundary between regions $1$ and $2$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.26s = "true", 5.66s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (0.56,1.46) circle (4pt);
\fill (0.56,1.46) circle (2pt);
\node[inner sep=1pt] at (0.35,1.82) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.96);
\node[above] at (5.15,-0.96) {\footnotesize $0.46$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-0.96);
\node[above] at (6.20,-0.96) {\footnotesize $0.46$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.38);
\node[above] at (7.25,-2.38) {\footnotesize $0.08$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.63, 0.63, -1.07)$};
\node[anchor=west] at (-3.0,-3.75) {On the boundary, $z_1 = z_2$, so $p_1 = p_2 = 0.46$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.66s = "true", 5.88s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (0.30,1.52) circle (4pt);
\fill (0.30,1.52) circle (2pt);
\node[inner sep=1pt] at (0.30,1.94) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-1.22);
\node[above] at (5.15,-1.22) {\footnotesize $0.39$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-0.70);
\node[above] at (6.20,-0.70) {\footnotesize $0.53$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.38);
\node[above] at (7.25,-2.38) {\footnotesize $0.08$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.48, 0.78, -1.05)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $2$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.88s = "true", 6.1s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-0.31,1.49) circle (4pt);
\fill (-0.31,1.49) circle (2pt);
\node[inner sep=1pt] at (-0.42,1.90) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-1.72);
\node[above] at (5.15,-1.72) {\footnotesize $0.26$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-0.23);
\node[above] at (6.20,-0.23) {\footnotesize $0.65$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.35);
\node[above] at (7.25,-2.35) {\footnotesize $0.09$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.11, 1.03, -0.91)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $2$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.1s = "true", 6.32s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-0.87,1.26) circle (4pt);
\fill (-0.87,1.26) circle (2pt);
\node[inner sep=1pt] at (-0.66,0.89) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.05);
\node[above] at (5.15,-2.05) {\footnotesize $0.17$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,0.01);
\node[above] at (6.20,0.01) {\footnotesize $0.71$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.26);
\node[above] at (7.25,-2.26) {\footnotesize $0.12$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.27, 1.16, -0.66)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $2$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.32s = "true", 8.32s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-1.08,1.09) circle (4pt);
\fill (-1.08,1.09) circle (2pt);
\node[inner sep=1pt] at (-0.79,0.79) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.15);
\node[above] at (5.15,-2.15) {\footnotesize $0.15$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,0.04);
\node[above] at (6.20,0.04) {\footnotesize $0.72$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.19);
\node[above] at (7.25,-2.19) {\footnotesize $0.13$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.43, 1.16, -0.52)$};
\node[anchor=west] at (-3.0,-3.75) {In region $2$, class $2$ has the largest probability.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.32s = "true", 8.54s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-1.33,0.81) circle (4pt);
\fill (-1.33,0.81) circle (2pt);
\node[inner sep=1pt] at (-1.69,1.02) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.24);
\node[above] at (5.15,-2.24) {\footnotesize $0.12$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-0.01);
\node[above] at (6.20,-0.01) {\footnotesize $0.71$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.05);
\node[above] at (7.25,-2.05) {\footnotesize $0.17$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.64, 1.13, -0.30)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.54s = "true", 8.76s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-1.63,-0.02) circle (4pt);
\fill (-1.63,-0.02) circle (2pt);
\node[inner sep=1pt] at (-2.05,-0.02) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.34);
\node[above] at (5.15,-2.34) {\footnotesize $0.09$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-0.50);
\node[above] at (6.20,-0.50) {\footnotesize $0.58$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-1.46);
\node[above] at (7.25,-1.46) {\footnotesize $0.33$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.98, 0.83, 0.26)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.76s = "true", 8.98s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-1.39,-1.04) circle (4pt);
\fill (-1.39,-1.04) circle (2pt);
\node[inner sep=1pt] at (-1.76,-1.25) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.35);
\node[above] at (5.15,-2.35) {\footnotesize $0.09$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-1.50);
\node[above] at (6.20,-1.50) {\footnotesize $0.32$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-0.45);
\node[above] at (7.25,-0.45) {\footnotesize $0.59$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-1.04, 0.19, 0.82)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.98s = "true", 9.2s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-0.76,-1.65) circle (4pt);
\fill (-0.76,-1.65) circle (2pt);
\node[inner sep=1pt] at (-0.55,-1.29) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.27);
\node[above] at (5.15,-2.27) {\footnotesize $0.11$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.07);
\node[above] at (6.20,-2.07) {\footnotesize $0.17$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,0.04);
\node[above] at (7.25,0.04) {\footnotesize $0.72$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.79, -0.40, 1.06)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point moves into region $3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.2s = "true", 11.2s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-0.42,-1.80) circle (4pt);
\fill (-0.42,-1.80) circle (2pt);
\node[inner sep=1pt] at (-0.31,-1.39) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.19);
\node[above] at (5.15,-2.19) {\footnotesize $0.14$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.20);
\node[above] at (6.20,-2.20) {\footnotesize $0.13$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,0.08);
\node[above] at (7.25,0.08) {\footnotesize $0.73$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.61, -0.63, 1.08)$};
\node[anchor=west] at (-3.0,-3.75) {In region $3$, class $3$ has the largest probability.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.2s = "true", 11.42s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (-0.05,-1.86) circle (4pt);
\fill (-0.05,-1.86) circle (2pt);
\node[inner sep=1pt] at (-0.05,-1.44) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-2.06);
\node[above] at (5.15,-2.06) {\footnotesize $0.17$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.28);
\node[above] at (6.20,-2.28) {\footnotesize $0.11$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,0.04);
\node[above] at (7.25,0.04) {\footnotesize $0.72$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (-0.40, -0.82, 1.05)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point returns to region $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.42s = "true", 11.64s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (0.81,-1.69) circle (4pt);
\fill (0.81,-1.69) circle (2pt);
\node[inner sep=1pt] at (1.02,-2.06) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-1.50);
\node[above] at (5.15,-1.50) {\footnotesize $0.32$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.36);
\node[above] at (6.20,-2.36) {\footnotesize $0.09$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-0.44);
\node[above] at (7.25,-0.44) {\footnotesize $0.59$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.15, -1.12, 0.77)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point returns to region $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.64s = "true", 11.86s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.56,-0.97) circle (4pt);
\fill (1.56,-0.97) circle (2pt);
\node[inner sep=1pt] at (1.92,-1.18) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.52);
\node[above] at (5.15,-0.52) {\footnotesize $0.57$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.34);
\node[above] at (6.20,-2.34) {\footnotesize $0.09$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-1.44);
\node[above] at (7.25,-1.44) {\footnotesize $0.33$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (0.74, -1.07, 0.19)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point returns to region $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.86s = "true", 12.08s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.76,-0.12) circle (4pt);
\fill (1.76,-0.12) circle (2pt);
\node[inner sep=1pt] at (2.18,-0.12) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.03);
\node[above] at (5.15,-0.03) {\footnotesize $0.70$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.24);
\node[above] at (6.20,-2.24) {\footnotesize $0.12$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.04);
\node[above] at (7.25,-2.04) {\footnotesize $0.17$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.03, -0.72, -0.36)$};
\node[anchor=west] at (-3.0,-3.75) {The probe point returns to region $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.08s = "true", 14.48s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.02);
\node[above] at (5.15,0.02) {\footnotesize $0.72$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.14);
\node[above] at (6.20,-2.14) {\footnotesize $0.15$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.18);
\node[above] at (7.25,-2.18) {\footnotesize $0.14$};
\node[anchor=west] at (3.9,2.6) {$T = 1.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {Now the probe point stays fixed and the temperature $T$ changes.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.48s = "true", 14.72s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.40);
\node[above] at (5.15,0.40) {\footnotesize $0.82$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.34);
\node[above] at (6.20,-2.34) {\footnotesize $0.10$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.37);
\node[above] at (7.25,-2.37) {\footnotesize $0.09$};
\node[anchor=west] at (3.9,2.6) {$T = 0.74$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {Dividing the scores by a small $T$ sharpens the probabilities.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.72s = "true", 14.96s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.74);
\node[above] at (5.15,0.74) {\footnotesize $0.91$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.51);
\node[above] at (6.20,-2.51) {\footnotesize $0.05$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.53);
\node[above] at (7.25,-2.53) {\footnotesize $0.04$};
\node[anchor=west] at (3.9,2.6) {$T = 0.55$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {Dividing the scores by a small $T$ sharpens the probabilities.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.96s = "true", 15.2s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.96);
\node[above] at (5.15,0.96) {\footnotesize $0.96$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.63);
\node[above] at (6.20,-2.63) {\footnotesize $0.02$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.64);
\node[above] at (7.25,-2.64) {\footnotesize $0.02$};
\node[anchor=west] at (3.9,2.6) {$T = 0.41$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {Dividing the scores by a small $T$ sharpens the probabilities.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.2s = "true", 17.8s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,1.07);
\node[above] at (5.15,1.07) {\footnotesize $0.99$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.68);
\node[above] at (6.20,-2.68) {\footnotesize $0.01$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.68);
\node[above] at (7.25,-2.68) {\footnotesize $0.00$};
\node[anchor=west] at (3.9,2.6) {$T = 0.30$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {At $T = 0.3$ the largest bar grows to $0.99$. The regions do not move.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 17.8s = "true", 18.04s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.92);
\node[above] at (5.15,0.92) {\footnotesize $0.95$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.60);
\node[above] at (6.20,-2.60) {\footnotesize $0.03$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.61);
\node[above] at (7.25,-2.61) {\footnotesize $0.02$};
\node[anchor=west] at (3.9,2.6) {$T = 0.44$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {A large $T$ flattens the probabilities toward $1/3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.04s = "true", 18.28s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.56);
\node[above] at (5.15,0.56) {\footnotesize $0.86$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.42);
\node[above] at (6.20,-2.42) {\footnotesize $0.07$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.44);
\node[above] at (7.25,-2.44) {\footnotesize $0.07$};
\node[anchor=west] at (3.9,2.6) {$T = 0.65$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {A large $T$ flattens the probabilities toward $1/3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.28s = "true", 18.52s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,0.09);
\node[above] at (5.15,0.09) {\footnotesize $0.73$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-2.18);
\node[above] at (6.20,-2.18) {\footnotesize $0.14$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-2.21);
\node[above] at (7.25,-2.21) {\footnotesize $0.13$};
\node[anchor=west] at (3.9,2.6) {$T = 0.95$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {A large $T$ flattens the probabilities toward $1/3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.52s = "true", 18.76s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.36);
\node[above] at (5.15,-0.36) {\footnotesize $0.61$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-1.95);
\node[above] at (6.20,-1.95) {\footnotesize $0.20$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-1.98);
\node[above] at (7.25,-1.98) {\footnotesize $0.19$};
\node[anchor=west] at (3.9,2.6) {$T = 1.39$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {A large $T$ flattens the probabilities toward $1/3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.76s = "true", 19s = "false", 22.4s = "false", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.71);
\node[above] at (5.15,-0.71) {\footnotesize $0.52$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-1.78);
\node[above] at (6.20,-1.78) {\footnotesize $0.24$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-1.81);
\node[above] at (7.25,-1.81) {\footnotesize $0.23$};
\node[anchor=west] at (3.9,2.6) {$T = 2.04$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {A large $T$ flattens the probabilities toward $1/3$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 19s = "true", 22.4s = "true", repeats}}}]
\draw[thick] (1.71,0.25) circle (4pt);
\fill (1.71,0.25) circle (2pt);
\node[inner sep=1pt] at (2.12,0.35) {$\mathbf x$};
\filldraw[draw=black, fill=hot] (4.80,-2.7) rectangle (5.50,-0.95);
\node[above] at (5.15,-0.95) {\footnotesize $0.46$};
\filldraw[draw=black, fill=cold] (5.85,-2.7) rectangle (6.55,-1.67);
\node[above] at (6.20,-1.67) {\footnotesize $0.27$};
\filldraw[draw=black, fill=grn] (6.90,-2.7) rectangle (7.60,-1.69);
\node[above] at (7.25,-1.69) {\footnotesize $0.27$};
\node[anchor=west] at (3.9,2.6) {$T = 3.00$};
\node[anchor=west] at (3.9,2.0) {\footnotesize $\mathbf z = (1.08, -0.51, -0.57)$};
\node[anchor=west] at (-3.0,-3.75) {At $T = 3$ the bars are flatter. The argmax and the regions stay the same.};
\end{scope}
\end{tikzpicture}
\end{document}(Numerical Stability) Implementations subtract the largest logit before exponentiating.
The exponential overflows double precision for and single precision for . Shift invariance allows subtracting from every logit. The largest term is then , so nothing overflows and the sum is at least . The loss uses the log-sum-exp identity . Computing directly also avoids taking when underflows.
(Exclusive Classes Only) Softmax assumes each input has exactly one class, so tasks with several labels per input need separate sigmoids.
The softmax probabilities sum to one, so raising one class lowers the others. Softmax therefore suits mutually exclusive classes, such as the digit in an image. A photo tagged with both “beach” and “dog” breaks the assumption. Multi-label tasks use independent sigmoid outputs, one binary logistic regression per label. The one-vs-rest scheme also trains binary classifiers on exclusive classes. Its probabilities do not sum to one. Its scores are fit separately, so they are not on a common scale. Softmax regression trains all classes jointly and avoids both problems.
Derivation
We want the gradient of the cross-entropy and the reduction to two classes. Write the loss as . The derivative of the log-sum-exp term is
The derivative of with respect to is when and otherwise, which is . So . Since , the chain rule gives and . Stacking the rows gives . The log-sum-exp is a convex function of , and is linear in the parameters, so is convex.
For , dividing the numerator and denominator of by gives
The cross-entropy then equals the binary cross-entropy, with class as the positive label.
Examples
(Easy) Cat, Dog or Bird
A photo classifier outputs logits for the classes cat, dog and bird. The photo shows a dog.
Compute the probabilities and the loss. What changes if every logit increases by ? The exponentials are , and , which sum to . The probabilities are , so the model predicts cat. The loss is . The log-sum-exp form gives the same value, . A cat photo would have cost only . Adding to every logit leaves unchanged. A naive single-precision computation would fail, though, because exceeds the largest float, about .
(Medium) One Gradient Step on a News Article
A topic model sorts articles into sports, politics and tech from two word-frequency features. Its current weights are
A tech article has , so .Compute the loss and the gradient, take one step with , and recompute the loss. The logits are , and the probabilities are . The loss is . The error is , and the weight gradient is After the step, each logit for this changes by . The comes from the bias. The new logits are , and the new probabilities are . The loss falls to . Politics, the most probable wrong class, lost the most probability.
(Hard) Calibrating an Overconfident Model
A trained topic model gives these logits for (sports, politics, tech) on four validation articles. Article 1 has and is sports. Article 2 has and is politics. Article 3 has and is politics. Article 4 has and is sports.
What is the accuracy for any temperature? Compare the mean log loss at and , and find the best . The model predicts sports, sports, politics and sports, so it gets articles 1, 3 and 4 right. Dividing by keeps the order of the logits, so the accuracy is at every temperature. At the true-class probabilities are . The mean log loss is , and article 2 alone contributes . At they become , with mean log loss . The three correct articles now cost more, but the confident mistake costs only . Minimizing the mean log loss over gives and a loss of . In practice, temperature scaling is fit on thousands of validation examples.
See Also
- 4.3. Logistic Regression, the two-class case.
- 3. Activation Functions, where softmax appears as the output activation of a network.
- 2. Neural Networks, whose classification heads are softmax regression on learned features.
- 2.2.4. Log Loss, the cross-entropy used to evaluate predicted probabilities.
- 1. Shannon Entropy, for cross-entropy as an information quantity.
- 2.2b.6 Multinomial Distribution, whose one-trial case is the categorical distribution behind the loss.