A three-class softmax model produces scores (logits) z=(2,1,0) for an input. Compute the class probabilities. If the true class is class 2, what is the cross-entropy loss, and what is the gradient of the loss with respect to z?

Supervised learning, Multinomial logistic regression
Level L2 Elementary. Length Medium. Nature Mat Mathematical.

What's being asked

A numerical softmax and the key identity that the gradient of cross-entropy with respect to the logits is p−y.

Key idea

pk=ezk/∑jezj. For true one-hot label y, the loss −log⁡pc has gradient p−y with respect to z.

Steps

  1. Exponentials: e2=7.389, e1=2.718, e0=1. The sum is 11.107.
  2. Probabilities: p=(0.665,0.245,0.090), which sum to 1.
  3. True class 2 has p2=0.2447, so the loss is −ln⁡0.2447=1.408.
  4. The one-hot label is y=(0,1,0). The gradient is p−y=(0.665,−0.755,0.090). Its components sum to 0, as they must.
  5. Interpretation: a gradient-descent step raises z2 (negative gradient on the true class) and lowers the others, in proportion to their wrongly assigned probability.

Common mistakes

How long your answer should be

Half a page, 3–4 minutes.

Last checked on 4 December 2026.

Read this

Previous in Supervised learning: With p=10 candidate features, how many models does best-subset selection have to consider, and how many model fits does forward stepwise selection need? What changes for p=30, and why might forward stepwise still give a worse model than best subset?
Next in Supervised learning: A linear classifier has w=(2,−1) and b=0.5. What are the hinge loss and the 0–1 loss on the points A=(1,0) with label +1, B=(0,0) with label +1, and C=(1,1) with label −1?