Derive categorical negative log-likelihood and its logit gradient, then change target smoothing and temperature without losing the normalization contract.
ConceptCore MLTraining & research
Published ·8 min read·Core·Foundation
Visual quick review
Explain why cross-entropy still supplies a large logit gradient when the true class has near-zero predicted probability, while squared error through a saturating probability output can supply almost none.
Learning objective
Predictions share normalization; targets and temperature change the corrective signal
Read it this way:Normalize class predictions together. Targets specify which outcomes to reward. Smoothing spreads target mass, reducing extreme pushes. Temperature changes predictions and rescales the logit gradient. The separate binary example shows cross-entropy correcting confident errors while squared error's sigmoid gate nearly closes. These are specified toy settings. At predicted probability 0.01 for a positive label, which gradient remains large?
Sources and provenance
Full explanation 8 min read
Summary
Categorical cross-entropy is negative log-likelihood when one class label is observed from a predicted categorical distribution.
Softmax maps finite logits to that distribution; differentiating their composition gives a prediction-minus-target logit gradient.
Stable evaluation comes from shifted log-normalization, not from the gradient formula alone.
The math, briefly
Let z∈RC be one logit vector and y its observed class index.
Define pi=ezi/∑jezj.
Then
L=−logpy=−zy+logj∑ezj,∂zi∂L=pi−1[i=y].
The derivative of the log-normalizer is ezi/∑jezj=pi.
The observed-class term contributes −1 only at index y.
For logits (log3,0) and class zero, p=(3/4,1/4), loss is log(4/3), and gradient is (−1/4,1/4).
The diagram instead uses a binary true-class probability q=0.01.
Cross-entropy’s true-logit gradient is q−1=−0.99.
Half squared error on the sigmoid probability introduces another derivative factor, giving magnitude q(1−q)2=0.009801.
Learning objective
Cross-entropy and squared-error logit gradients as true-class probability changes
Read it this way: move left toward confident error. Cross-entropy's gradient approaches minus one. Probability-MSE includes the saturating sigmoid derivative, so its magnitude approaches zero. The dashed curve describes this binary comparison, not every possible squared-error model.
Check: Why is the cross-entropy logit gradient large where the probability-squared-error gradient nearly vanishes?
Reference drawing
Learning objective
Predictions share normalization; targets and temperature change the corrective signal
Derivation
Read it this way: move left toward confident error. Cross-entropy's gradient approaches minus one. Probability-MSE includes the saturating sigmoid derivative, so its magnitude approaches zero. The dashed curve describes this binary comparison, not every possible squared-error model.
Check: Why is the cross-entropy logit gradient large where the probability-squared-error gradient nearly vanishes?
Read it this way:Normalize class predictions together. Targets specify which outcomes to reward. Smoothing spreads target mass, reducing extreme pushes. Temperature changes predictions and rescales the logit gradient. The separate binary example shows cross-entropy correcting confident errors while squared error's sigmoid gate nearly closes. These are specified toy settings. At predicted probability 0.01 for a positive label, which gradient remains large?
What an interviewer expects you to say
State the observation model, derive the log-normalizer gradient, declare the reduction, and explain stable evaluation.
For independent examples, summed negative log-likelihood is the joint training objective; a mean divides its gradients by sample count.
For multilabel tasks with independent binary targets, categorical normalization changes the model and is generally inappropriate.
Change the Requirement
Smooth the Target
New task: Replace a one-hot target with t=(1−ε)ey+ε1/C, where 0≤ε≤1.
Picture and change: Optimize −∑itilogpi; normalization ∑iti=1 preserves gradient p−t.
Check: With p=(0.75,0.25), class zero, and ε=0.1, target (0.95,0.05) gives gradient (−0.20,0.20).
Use when: A deliberately softened training target is appropriate.
The target and its interpretation have changed; improved calibration is not guaranteed by the algebra.
Change Temperature
New task: Use probabilities pT=softmax(z/T), with fixed T>0.
Picture and change: The chain rule adds 1/T: ∇zL=(pT−ey)/T.
Check: For z=(log3,0) and T=2, pT,0=3/(1+3)≈0.6340.
The gradient is approximately (−0.1830,0.1830).
Use when: The task specifies temperature-scaled training or inference probabilities.
Distillation may additionally scale its loss; state that convention separately.
Common confusions
Wrong API input. A logits-based cross-entropy API does not expect already normalized probabilities.
Wrong task. Multiclass normalization and independent multilabel Bernoulli losses encode different outcome spaces.
Unnormalized targets. For general target weights t, the gradient is (∑iti)p−t, not automatically p−t.
Numerical stability: the LSE trick
Let m=maxizi and evaluate the shifted expression:
For the loss itself, use (m−zy)+log∑iezi−m.
This avoids exponentiating positive shifted logits and avoids subtracting two large log-normalized quantities afterward.
For logits (1000,1000), class-zero loss is log2, despite naive exponentiation overflowing.
Nonfinite logits, unrepresentable differences, and all-masked rows still need a declared policy.
Why MSE fails on classification
The heading describes sigmoid saturation in the displayed example, not a universal impossibility theorem.
Squared probability error is related to the Brier score and can be a meaningful evaluation or training objective.
Its optimization behavior differs because the sigmoid derivative can suppress a confident-error logit update.
Cross-entropy avoids that extra factor for logits, but parameter gradients can still vanish through other network operations.
Reconstruct
Does the simple logit gradient guarantee numerical stability or successful training?
No. Naive exponentiation can overflow before a gradient is computed.
Stable normalization addresses that arithmetic issue; optimization, model suitability, and upstream derivatives remain separate concerns.