Cross-entropy, KL and entropy: what the loss measures, and how it pushes the logits

Homework 1, questions 1.3.2–1.3.3, with n classes. Drag a logit up or down; pick a target y, and click a class name to make it the true class. The arrows show which way gradient descent moves each logit.

Logits, probabilities, target, and the three quantities of the loss Left: one column per class with a draggable logit bar above and, below it, the predicted probability beside the target probability; an arrow on each logit shows the gradient-descent step, minus p plus y. Right top: the cross-entropy drawn as a bar split into the target's entropy and the KL divergence, beside the per-class terms of the cross-entropy. Right bottom: four losses against the margin of the true class, with the current margin marked.
classes target y
ε
steps use learning rate 1 on the logits alone

1. Cross-entropy is entropy plus KL

With the homework’s notation, the target is a distribution y and the model’s prediction is p = softmax(z). The KL divergence measures how far p is from y; it splits into two sums, one of which does not involve p at all:

That is the bar on the right: the grey part, H(y), is fixed by the target; only the pink KL part responds when you drag the logits. So minimizing cross-entropy and minimizing KL are the same problem, and their gradients are equal. With a one-hot target H(y) = 0, only one term of the sum survives, and cross-entropy is the negative log-likelihood of the true class, −log ptrue. With a smoothed or blended target the grey part is not zero, so the loss can never go below it: that floor is the target’s own uncertainty.

2. The gradient on the logits is p − y

Question 1.3.3 asks for ∂CE/∂z. Putting softmax’s Jacobian (softmax) through the chain rule, everything cancels to

That is the arrow on each logit, drawn as the descent step −(pi − yi): a class the model over-predicts is pushed down, one it under-predicts is pushed up, and the push is exactly the size of the error, never damped by a flat slope. That is why cross-entropy trains where squared error on the same outputs stalls (squared error vs. cross-entropy). The readout checks the formula against finite differences of the loss itself. Homework 2.1’s binary cross-entropy (task 3.4) is the n = 2 case with a sigmoid: −[y log p + (1 − y) log(1 − p)], gradient p − y on its one logit.

3. Against a one-hot target, cross-entropy never stops pushing

Write the margin of the true class as m = ztrue − log Σj≠true ezj (for two classes, just z1 − z2). Then, for any number of classes, the one-hot cross-entropy is exactly log(1 + e−m): the logistic curve in the lower plot. It falls forever but never reaches zero, so gradient descent keeps widening the gap, driving ptrue toward 1 and the logits toward infinity. Press Keep stepping with a one-hot target and watch the margin marker walk off to the right. Hinge loss, by contrast, is satisfied at m = 1, and 0-1 loss has no gradient anywhere.

Label smoothing gives the true class 1 − ε + ε/n and every other class ε/n. Now the loss has a minimum where p = y exactly, and that takes only a finite gap between the logits:

In terms of the margin, that is m* = log[(1 − ε + ε/n)/(ε − ε/n)], the gap less log(n − 1). The dashed curve is the smoothed loss with the other logits held equal; its lowest point is marked, and stepping now settles there. So smoothing is a regularizer on the output: it forbids certainty. Mixup’s blended targets do the same from the data side, asking for 60% cat and 40% dog on an image that is 60% cat and 40% dog.