Speed

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 z=[z0,z1,z2]z = [z_0, z_1, z_2] rather than probabilities. To evaluate cross-entropy, we first map those logits to positive probabilities that sum to 1.0 via the softmax function:

pk=ezk∑j=0K−1ezj,L=−∑k=0K−1yklog⁡(pk),∂L∂zk=pk−ykp_k = \frac{e^{z_k}}{\sum_{j=0}^{K-1} e^{z_j}}, \qquad L = -\sum_{k=0}^{K-1} y_k \log(p_k), \qquad \frac{\partial L}{\partial z_k} = p_k - y_k

Pairing cross-entropy with softmax is standard across machine learning because when we differentiate LL with respect to each scaled logit zkz_k, the exponential in softmax and the logarithm in −log⁡(pk)-\log(p_k) 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: e2.0=7.38906e^{2.0} = 7.38906, e1.0=2.71828e^{1.0} = 2.71828, and e0.0=1.00000e^{0.0} = 1.00000. 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 k=1k = 1, the cross-entropy loss is −log⁡(p1)=−log⁡(0.24473)=1.40761-\log(p_1) = -\log(0.24473) = 1.40761 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 (y0=0.0y_0 = 0.0): 0.66524−0.0=+0.665240.66524 - 0.0 = +0.66524
  • Class 1 (y1=1.0y_1 = 1.0): 0.24473−1.0=−0.755270.24473 - 1.0 = -0.75527
  • Class 2 (y2=0.0y_2 = 0.0): 0.09003−0.0=+0.090030.09003 - 0.0 = +0.09003

Notice that the three gradient entries sum to 0.66524−0.75527+0.09003=0.000000.66524 - 0.75527 + 0.09003 = 0.00000.

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 (1/T1 / T), multiplying the raw logits before softmax, and grad is the gradient ∂L/∂z\partial L / \partial z 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].
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 ∂L/∂z\partial L / \partial z.

Logits [2.0, 1.0, 0.0] with target class 1; the starter reverses the logit gradient as y - p instead of p - y.
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: znew=z−η gradz_{\text{new}} = z - \eta\,\text{grad}. Subtracting a negative gradient increases z1z_1, while subtracting positive gradients (+0.66524 and +0.09003) decreases z0z_0 and z2z_2.

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 zz (with scale equal to 1/T1 / T) to probabilities pk=ezk/∑jezjp_k = e^{z_k} / \sum_j e^{z_j}.
  • Combining cross-entropy −∑kyklog⁡(pk)-\sum_k y_k \log(p_k) with softmax yields the exact gradient ∂L/∂z=p−y\partial L / \partial z = p - y with respect to the scaled logits.
  • Under gradient descent, the true class logit is pulled up by 1−pk∗1 - p_{k^*} and each wrong class logit is pushed down in proportion to pkp_k.