Cross-Entropy and Softmax
Convert three raw logits into softmax probabilities, compute cross-entropy loss, and verify that the logit gradient equals p - y.
The problem
A linear layer at the end of a classifier outputs raw, unbounded scores called logits rather than probabilities. To evaluate cross-entropy, we first map those logits to positive probabilities that sum to 1.0 via the softmax function:
Pairing cross-entropy with softmax is standard across machine learning because when we differentiate with respect to each scaled logit , the exponential in softmax and the logarithm in cancel out.
Work through the numbers
Take baseline logits z0 = [2.0, 1.0, 0.0] and one-hot target y = [0.0, 1.0, 0.0] (true class 1) at scale equal to 1.0:
First exponentiate each logit: , , and . Their sum is 11.10734. Dividing each exponential by 11.10734 gives the softmax probability vector p = [0.66524, 0.24473, 0.09003].
Because the true class is , the cross-entropy loss is nats. Subtracting the target vector y = [0.0, 1.0, 0.0] from p gives the gradient grad with respect to the scaled logits z:
- Class 0 ():
- Class 1 ():
- Class 2 ():
Notice that the three gradient entries sum to .
The live model
The blue bars on the left show the softmax probabilities p, and the bars on the right show grad (green for the negative true-class gradient, red for the positive wrong-class gradients). Here scale is one over a temperature (), multiplying the raw logits before softmax, and grad is the gradient with respect to the scaled logits z.
At scale equal to 0.5, the scaled logits [1.0, 0.5, 0.0] produce flatter probabilities [0.506, 0.307, 0.186], a lower loss of 1.180, and a gentler gradient [0.506, -0.693, 0.186]. At scale equal to 1.0, loss is 1.408 and grad is [0.665, -0.755, 0.090]. At scale equal to 1.5, the scaled logits [3.0, 1.5, 0.0] concentrate 0.786 on wrong class 0 and leave 0.175 on true class 1, raising loss to 1.741 and steepening grad to [0.786, -0.825, 0.039].
![Logits [2.0, 1.0, 0.0] with target class 1 give softmax probabilities [0.665, 0.245, 0.090], cross-entropy 1.408, and logit gradient p - y = [0.665, -0.755, 0.090].](../assets/ml/cross-entropy-and-softmax--demo.png)
let scale = scene.parameter("scale", 1.0, 0.5, 1.5, 0.5);
let z = input.select(0, 0) * scale;
let y = input.select(0, 1);
let p = z.softmax(0);
let loss = (y * p.log().neg()).sum().reshape(1);
let grad = p - y;
let answer = torch.cat(torch.list().add(loss).add(grad), 0);
out.set("p", p).set("loss", loss).set("grad", grad);
{
"p": [
0.6652409557748218,
0.24472847105479764,
0.09003057317038043
],
"loss": [
1.4076059644443804
],
"grad": [
0.6652409557748218,
-0.7552715289452023,
0.09003057317038043
]
}Your turn
At scale = 1.0 the softmax probabilities are p = [0.66524, 0.24473, 0.09003], so the gradient with respect to the scaled logits z is [0.66524, -0.75527, 0.09003]; the starter reports [-0.66524, +0.75527, -0.09003] with the signs flipped. Fix grad so it points in the direction of steepest ascent .
![Logits [2.0, 1.0, 0.0] with target class 1; the starter reverses the logit gradient as y - p instead of p - y.](../assets/ml/cross-entropy-and-softmax--exercise.png)
let scale = scene.parameter("scale", 1.0, 0.5, 1.5, 0.5);
let z = input.select(0, 0) * scale;
let y = input.select(0, 1);
let p = z.softmax(0);
let loss = (y * p.log().neg()).sum().reshape(1);
let grad = y - p;
let answer = torch.cat(torch.list().add(loss).add(grad), 0);
out.set("p", p).set("loss", loss).set("grad", grad);
{
"p": [
0.6652409557748218,
0.24472847105479764,
0.09003057317038043
],
"loss": [
1.4076059644443804
],
"grad": [
-0.6652409557748218,
0.7552715289452023,
-0.09003057317038043
]
}Compute grad as the gradient of the cross-entropy loss with respect to the scaled logits z so the true class has a negative gradient and wrong classes have positive gradients. Check tests unseen logit vectors and scale values.
With the gradient sign restored, the default run returns [1.40760596, 0.66524096, -0.75527153, 0.09003057]. The check reruns the gradient on logits [1.5, 0.5, -0.5] with target class 0 at scale = 0.5 and on logits [0.0, 1.0, 2.0] with target class 1 at scale = 1.5.
Read the result carefully
Why is a negative gradient (-0.75527) on true class 1 what we want? Because gradient descent updates logits by subtracting a learning rate times the gradient: . Subtracting a negative gradient increases , while subtracting positive gradients (+0.66524 and +0.09003) decreases and .
Furthermore, the push on each wrong logit equals its softmax probability (0.66524 on class 0 versus 0.09003 on class 2): the wrong class that stole the most probability mass receives the strongest downward correction.
Recap
- Softmax maps scaled logits (with
scaleequal to ) to probabilities . - Combining cross-entropy with softmax yields the exact gradient with respect to the scaled logits.
- Under gradient descent, the true class logit is pulled up by and each wrong class logit is pushed down in proportion to .