For K-class softmax regression with P(y=k∣x)=ewk⊤x/∑jewj⊤x, derive the gradient of the negative log-likelihood with respect to wk. How do you show that the parameters are redundant (not identifiable), and how is this fixed?

Supervised learning, Multinomial logistic regression
Level L3 Intermediate. Length Long. Nature Mat Mathematical.

What's being asked

The derivation of ∂L/∂wk=∑i(pik−yik)xi and the shift-invariance of softmax.

Key idea

With one-hot labels yik, the loss L=−∑i∑kyiklog⁡pik has gradient ∑i(pik−yik)xi with respect to wk. Adding the same vector to every wk leaves the pik unchanged.

Steps

  1. Write zik=wk⊤xi and pik=ezik/∑jezij. The loss is L=−∑i∑kyiklog⁡pik, with yik=1 for the true class.
  2. Since ∑kyik=1, L=∑i[log⁡∑jezij−∑kyikzik].
  3. Differentiate with respect to zik: the first term gives ezik/∑jezij=pik, the second gives −yik. So ∂L/∂zik=pik−yik.
  4. Apply the chain rule with ∂zik/∂wk=xi: ∇wkL=∑i(pik−yik)xi. In matrix form, ∇WL=X⊤(P−Y).
  5. Redundancy: replace every wk by wk+v. Each zik gains v⊤xi, and this factor cancels in the ratio, so pik is unchanged. The minimiser is therefore not unique (the Hessian has d zero eigenvalues).
  6. Fixes: fix one class as the reference with wK=0 (giving K−1 log-odds against it), or add an L2 penalty λ2∑k∥wk∥2, which picks the solution with ∑kwk=0.

Common mistakes

How long your answer should be

One page; 6–8 minutes.

Last checked on 4 December 2026.

Read this

Previous in Supervised learning: Write a distribution in exponential-dispersion form p(y∣θ)=exp⁡{yθ−b(θ)+c(y)}. How do you show that the Bernoulli and Poisson distributions fit this form, what is the canonical link in each case, and why does a canonical link make the log-likelihood gradient equal to X⊤(y−μ)?
Next in Supervised learning: Fit the points (0,1),(1,3),(2,3) with (a) a quadratic y=a+bx+cx2 and (b) a straight line. Find each fit and its residual sum of squares. Why is the quadratic exact, and what goes wrong if you try a cubic?