One-Hot Selects One Term
Multiply a four-class log-penalty vector by a one-hot target and verify that every non-target class contributes exactly zero.
The problem
In standard single-label classification, each training row has one known ground-truth class. Suppose a four-class problem has true class 1. We encode that hard label as the one-hot target vector y = [0.0, 1.0, 0.0, 0.0], placing all probability mass on index 1 and zero on indices 0, 2, and 3.
Meanwhile, a classifier outputs positive probabilities for all four classes: p0 = [0.45, 0.25, 0.20, 0.10]. Every class has a finite negative log-probability . How does the full cross-entropy formula treat the three wrong classes?
Because for every non-target class, multiplying by annihilates three of the four terms. Only the active class survives the dot product, collapsing the entire multi-class sum to a single number: .
Work through the numbers
First compute the unweighted surprises at the default setting boost equal to 0.0, where p = [0.45, 0.25, 0.20, 0.10]. Class 0 has , class 1 has , class 2 has , and class 3 has . Summing all four unweighted surprises would give 6.09683.
Now multiply each surprise by its target weight from y = [0.0, 1.0, 0.0, 0.0]:
- Class 0:
- Class 1:
- Class 2:
- Class 3:
Adding the four weighted terms [0.0, 1.38629, 0.0, 0.0] produces 1.38629. Even though the model put its highest probability (0.45) on wrong class 0 and its lowest (0.10) on wrong class 3, neither number enters the final loss once is fixed.
The live model
The blue bars on the left show the four class probabilities p, and the red bars on the right show the weighted terms . Only row 1 has a visible red bar; rows 0, 2, and 3 sit at 0.0. The boost slider shifts probability mass toward the one-hot target according to p = p0 * (1.0 - boost) + y * boost.
At boost equal to 0.0, is 0.25 and the cross-entropy sum is 1.386. Moving boost to 0.4 raises to 0.55 while shrinking the other probabilities to [0.27, 0.12, 0.06], dropping the cross-entropy sum to 0.598. At the upper endpoint boost equal to 0.8, reaches 0.85 and the loss falls to 0.163.

let boost = scene.parameter("boost", 0.0, 0.0, 0.8, 0.2);
let y = input.select(0, 0);
let p0 = input.select(0, 1);
let p = p0 * (1.0 - boost) + y * boost;
let terms = y * p.log().neg();
let ce = terms.sum().reshape(1);
let answer = torch.cat(torch.list().add(terms).add(ce), 0);
out.set("p", p).set("terms", terms).set("ce", ce);
{
"p": [
0.45,
0.25,
0.2,
0.1
],
"terms": [
0,
1.3862943611198906,
0,
0
],
"ce": [
1.3862943611198906
]
}Your turn
At boost = 0.0 the starter reports terms = [0.79851, 1.38629, 1.60944, 2.30259] and a sum of 6.09683 because all four classes remain unmasked; by hand only class 1 is active, so the weighted terms are [0.0, 1.38629, 0.0, 0.0] and ce = 1.38629. Fix terms so non-target classes contribute 0.0 before summing into ce.

let boost = scene.parameter("boost", 0.0, 0.0, 0.8, 0.2);
let y = input.select(0, 0);
let p0 = input.select(0, 1);
let p = p0 * (1.0 - boost) + y * boost;
let terms = p.log().neg();
let ce = terms.sum().reshape(1);
let answer = torch.cat(torch.list().add(terms).add(ce), 0);
out.set("p", p).set("terms", terms).set("ce", ce);
{
"p": [
0.45,
0.25,
0.2,
0.1
],
"terms": [
0.7985076962177716,
1.3862943611198906,
1.6094379124341003,
2.3025850929940455
],
"ce": [
6.096825062765808
]
}Under a one-hot target, non-target classes must contribute 0.0 to terms and ce. Check tests unseen one-hot targets and boost values.
With the one-hot target weighting restored, the default run outputs terms = [0.0, 1.38629436, 0.0, 0.0] and ce = 1.38629436. The check reruns the selection with the target at index 2 on [0.30, 0.30, 0.25, 0.15] at boost = 0.2 and at index 0 on [0.20, 0.40, 0.25, 0.15] at boost = 0.6.
Read the result carefully
Selecting a single term is why software libraries often write one-hot cross-entropy as instead of computing a full vector product. That shortcut is algebraically identical to whenever y is a one-hot vector.
However, notice what one-hot cross-entropy ignores: if stays fixed at 0.25, a model that splits the remaining 0.75 mass evenly ([0.25, 0.25, 0.25, 0.25]) gets the exact same 1.38629 loss as a model that concentrates 0.73 on a single wrong class ([0.73, 0.25, 0.01, 0.01]). One-hot cross-entropy judges how much probability lands on the true class, not how the leftover mass is arranged among the wrong classes.
Recap
- A one-hot target
yplaces1.0on the true class and0.0on every other class. - Multiplying by zeroes out all non-target terms, reducing to .
- Under a one-hot target, rearranging probability mass among the wrong classes without changing leaves the cross-entropy loss unchanged.