Tutorials › Statistical Learning (Graduate) › Softmax Cross-Entropy and Multiclass Probabilities

Multiclass and Structured Losses · Tutorial 575 of 989

Softmax Cross-Entropy and Multiclass Probabilities

See how softmax cross-entropy turns class scores into probabilities and why its conditional risk is minimized by the true class-probability vector.

Advanced 10 min read

What You'll Learn

  • Derive the softmax probability vector from a vector of class scores
  • Express cross-entropy as the negative log-likelihood of a categorical label
  • Write the conditional cross-entropy risk for a given true probability vector
  • Prove the conditional risk is minimized by the true class probabilities
  • Distinguish finite-score minimizers from probability minimizers when some classes have zero probability
  • Compute the score gradient and identify a common numerical and conceptual pitfall

From Class Scores to Probabilities

For a classification problem with \(K\) possible classes, a model may first produce a real-valued score for each class. These scores are not probabilities: they may be negative, need not sum to one, and can be shifted by the same constant without changing their relative ordering. The softmax transformation turns them into a probability vector. Cross-entropy then measures how well that vector assigns probability to the observed class.

The central statistical property is about conditional risk. At a fixed feature value \(x\), the true conditional distribution of the label is a probability vector. We will show that among all probability vectors, the expected cross-entropy is minimized exactly by that true vector. This identifies what probability estimation is targeting, independently of how a particular model is fitted.

Definition: Let the class label \(Y\) take values in \(\{1,\ldots,K\}\). At a fixed feature value \(x\), write \(\eta_k(x)=P(Y=k\mid X=x)\), so that \(\eta(x)=(\eta_1(x),\ldots,\eta_K(x))\) lies in the probability simplex \(\Delta_K=\{q\in[0,1]^K:\sum_{k=1}^K q_k=1\}\). For a score vector \(s=(s_1,\ldots,s_K)\in\mathbb{R}^K\), its softmax probability vector is \(q_k(s)=\exp(s_k)/\sum_{j=1}^K\exp(s_j)\).

Every finite score vector gives strictly positive probabilities, \(q_k(s)>0\), and their sum is one. Adding a constant \(c\) to every score leaves the probabilities unchanged, because the factor \(\exp(c)\) cancels between numerator and denominator. Thus scores have a location non-identifiability that does not affect the predicted probabilities.

Cross-Entropy as Categorical Negative Log-Likelihood

For an observed label \(y\), represent the outcome by a one-hot vector \(e_y\): its \(y\)th component is one and every other component is zero. The cross-entropy between \(e_y\) and a probability vector \(q\) is \(-\sum_{k=1}^K (e_y)_k\log q_k\), which equals \(-\log q_y\). It penalizes assigning a small probability to the class that actually occurred.

This loss is also the negative log-likelihood for a categorical observation. If the model assigns probabilities \(q_1,\ldots,q_K\), the likelihood of observing \(Y=y\) is \(q_y\); taking the negative logarithm gives \(-\log q_y\). For scores, substituting the softmax formula gives a convenient expression:

$$ \ell_{\mathrm{CE}}(s,y) =-\log q_y(s) =\log\left(\sum_{j=1}^K \exp(s_j)\right)-s_y. $$

The term \(\log(\sum_j\exp(s_j))\) is the log-sum-exp of the scores. This form is useful both for derivations and computation. In numerical implementations, it is common to subtract the largest score \(m=\max_j s_j\) inside the exponential sum, using \(\log(\sum_j\exp(s_j))=m+\log(\sum_j\exp(s_j-m))\). The identity follows by factoring \(\exp(m)\) out of the sum and helps avoid overflow.

Worked Example: Converting Scores and Scoring an Observed Class

Consider three class scores \(s=(\log 4,\log 2,0)\). Compute the softmax probabilities and the cross-entropy if the observed class is class 2.

Exponentiating the scores gives \((4,2,1)\), whose sum is \(7\). Therefore the probability vector is \(q=(4/7,2/7,1/7)\). The probabilities are positive and sum to one: \(4/7+2/7+1/7=1\).

For an observed class 2, the loss is the negative logarithm of its assigned probability: \(-\log(2/7)=\log(7/2)\approx1.252763\). Equivalently, the score formula gives \(\log(4+2+1)-\log 2=\log 7-\log 2=\log(7/2)\). Both calculations agree.

Conditional Risk and Its Minimizer

At a fixed \(x\), suppose the model reports a probability vector \(q\in\Delta_K\). The label is random with conditional probabilities \(\eta(x)\). The conditional cross-entropy risk is the expected loss over that label distribution:

$$ R(q\mid x) =E[-\log q_Y\mid X=x] =-\sum_{k=1}^K \eta_k(x)\log q_k. $$

We use the convention \(0\log 0=0\) in entropy expressions. If \(q_k=0\) for any class with \(\eta_k(x)>0\), the conditional risk is \(+\infty\), since that class can occur but is assigned zero probability. Define the entropy \(H(\eta(x))=-\sum_k\eta_k(x)\log\eta_k(x)\). When \(q_k>0\) wherever \(\eta_k(x)>0\), the risk decomposes into entropy plus Kullback–Leibler divergence:

$$ R(q\mid x) =H(\eta(x))+ D_{\mathrm{KL}}(\eta(x)\Vert q), \qquad D_{\mathrm{KL}}(\eta\Vert q) =\sum_{k:\eta_k>0}\eta_k\log\frac{\eta_k}{q_k}. $$
Theorem: For any true class-probability vector \(\eta\in\Delta_K\), the conditional cross-entropy risk \(R(q)=-\sum_k\eta_k\log q_k\) over \(q\in\Delta_K\) is minimized by \(q=\eta\), and this minimum is \(H(\eta)\). The minimizer is unique, including when some components of \(\eta\) are zero.

Proof. If \(q_k=0\) for some \(k\) with \(\eta_k>0\), then \(R(q)=+\infty\), so it cannot improve on \(q=\eta\), whose risk is the finite entropy \(H(\eta)\). Otherwise, the risk decomposition applies. It remains to establish that \(D_{\mathrm{KL}}(\eta\Vert q)\geq0\), with equality only if \(q=\eta\).

For each \(k\) with \(\eta_k>0\), the elementary inequality \(\log t\leq t-1\), valid for \(t>0\) with equality only at \(t=1\), gives \(\eta_k\log(q_k/\eta_k)\leq q_k-\eta_k\). Summing over the support \(S=\{k:\eta_k>0\}\) yields \[ -D_{\mathrm{KL}}(\eta\Vert q) \leq \sum_{k\in S}q_k-\sum_{k\in S}\eta_k =\sum_{k\in S}q_k-1 \leq 0. \] The final inequality holds because \(q\) is a probability vector. Hence the divergence is nonnegative. For equality, both inequalities must be equalities: the logarithm inequality requires \(q_k/\eta_k=1\) for every \(k\in S\), and \(\sum_{k\in S}q_k=1\) requires zero probability outside \(S\). These conditions together are exactly \(q=\eta\). The risk decomposition now proves the result. \(\square\)

Worked Example: Conditional Risk for a Misspecified Probability Vector

Suppose the true class probabilities are \(\eta=(0.4,0.5,0.1)\), while a model reports \(q=(0.2,0.5,0.3)\). Compute its conditional cross-entropy risk.

The risk weights each possible class loss by its true probability: \[ R(q)=-0.4\log(0.2)-0.5\log(0.5)-0.1\log(0.3). \] Using \(-\log(0.2)=1.609437912\), \(-\log(0.5)=0.693147181\), and \(-\log(0.3)=1.203972804\), the three contributions are approximately \(0.643775165\), \(0.346573590\), and \(0.120397280\). Their sum is \(1.110746036\), so \(R(q)\approx1.1107460\), or \(1.1107\) to four decimal places.

The true probability vector has risk \(H(\eta)\), which is smaller. The theorem explains why: any departure from \(\eta\) adds the nonnegative quantity \(D_{\mathrm{KL}}(\eta\Vert q)\).

What the Minimizer Means for Softmax Scores

The theorem optimizes over the full simplex, including its boundary. A finite softmax score vector, however, can only produce probabilities strictly greater than zero. If every component of \(\eta(x)\) is positive, the true probability vector is attainable: choose \(s_k=\log\eta_k(x)\), for which \(\sum_j\exp(s_j)=\sum_j\eta_j(x)=1\), and softmax returns \(q=\eta(x)\). More generally, all score vectors \(s_k=\log\eta_k(x)+c\) give the same probabilities.

If some true class probabilities are zero, no finite score vector can represent \(\eta(x)\) exactly. The minimum over the closed simplex is still \(H(\eta(x))\), but finite softmax scores only approach it. For example, \(q^{(\epsilon)}=(1-\epsilon)\eta+\epsilon(1/K,\ldots,1/K)\), for \(0<\epsilon<1\), has positive components and converges to \(\eta\) as \(\epsilon\) decreases to zero. For each \(k\) with \(\eta_k>0\), \(q_k^{(\epsilon)}\) converges to \(\eta_k\), so the finite sum defining \(R(q^{(\epsilon)})\) converges to \(H(\eta)\). Thus the infimum over finite scores equals the simplex minimum, even though it need not be attained by finite scores.

Worked Example: Entropy, Excess Risk, and the Probability Target

Let \(\eta=(0.5,0.3,0.2)\), and compare the true vector with \(q=(0.6,0.25,0.15)\). First compute the minimum conditional risk: \[ H(\eta)=-0.5\log(0.5)-0.3\log(0.3)-0.2\log(0.2). \] Using \(\log(0.5)=-0.693147181\), \(\log(0.3)=-1.203972804\), and \(\log(0.2)=-1.609437912\), the contributions are \(0.346573590\), \(0.361191841\), and \(0.321887582\). Their sum is approximately \(1.029653014\), which rounds to \(1.029653\) at six decimal places.

The risk of \(q\) is \[ R(q)=-0.5\log(0.6)-0.3\log(0.25)-0.2\log(0.15). \] The three contributions are approximately \(0.255412812\), \(0.415888308\), and \(0.379423997\), giving \(R(q)\approx1.050725117\). The excess over the minimum is therefore approximately \(1.050725117-1.029653014=0.021072103\), or \(0.021072\) to six decimal places.

The divergence gives the same excess: \[ D_{\mathrm{KL}}(\eta\Vert q) =0.5\log(0.5/0.6)+0.3\log(0.3/0.25)+0.2\log(0.2/0.15). \] The terms are approximately \(-0.091160778\), \(0.054696467\), and \(0.057536414\), which sum to \(0.021072103\). This verifies the risk decomposition numerically. A negative term is possible; it is the sum, not each individual term, that is guaranteed to be nonnegative.

Score Gradients and a Common Pitfall

The score form of the loss gives a simple derivative that is central to fitting softmax models. For a fixed observed class \(y\), differentiate the log-sum-exp term with respect to score \(s_j\). The chain rule gives \[ \frac{\partial}{\partial s_j}\log\left(\sum_{k=1}^K\exp(s_k)\right) =\frac{\exp(s_j)}{\sum_{k=1}^K\exp(s_k)} =q_j(s). \] The derivative of \(-s_y\) with respect to \(s_j\) is \(-1\) when \(j=y\) and zero otherwise. Therefore,

$$ \frac{\partial \ell_{\mathrm{CE}}(s,y)}{\partial s_j} =q_j(s)-\mathbf{1}\{y=j\}. $$

This derivative is the predicted probability minus the observed one-hot indicator. Its components sum to zero, since \(\sum_j q_j(s)=1\) and \(\sum_j\mathbf{1}\{y=j\}=1\). That agrees with the fact that adding the same constant to every score leaves the loss unchanged.

A common mistake is to say that softmax cross-entropy always has a finite-score minimizer at the true conditional probabilities. The statement confuses two optimization domains. Over all probability vectors in the closed simplex, the unique minimizer is always \(q=\eta\). Over finite score vectors, softmax probabilities are strictly positive, so exact attainment requires every \(\eta_k\) to be positive. When some true probabilities vanish, finite scores can approach the optimum but cannot attain it. This distinction matters when interpreting a model's fitted scores, especially in settings where certain classes are impossible at a given feature value.

Cross-entropy is therefore more than a convenient penalty for incorrect labels: its conditional target is the full class-probability vector. This is distinct from choosing only a class decision. Probability estimates retain information about uncertainty, while a final class prediction may reduce the vector to a single label. The conditional minimization result explains precisely why cross-entropy is used when the modeling goal is to estimate multiclass probabilities.

Check Your Understanding

Work through these questions using the definitions and derivations above.

  1. For scores \(s=(0,\log 3)\), compute the softmax probability of class 2 and the cross-entropy if class 1 is observed.
  2. Write the conditional cross-entropy risk when \(\eta=(0.2,0.3,0.5)\) and \(q=(0.2,0.5,0.3)\).
  3. Why does the conditional risk have a unique minimizer even when one component of \(\eta\) is zero?
  4. Can a finite softmax score vector represent \(\eta=(1,0,0)\) exactly? Explain the distinction between the simplex minimum and the finite-score infimum.
  5. Derive the score gradient for a class \(j\) that is not the observed class \(y\), and explain why the gradient components sum to zero.
Clinical Biostats