Speed

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 −log⁡(pk)-\log(p_k). How does the full cross-entropy formula treat the three wrong classes?

H(y,p)=−∑k=0K−1yklog⁡(pk)=−log⁡(pk∗)H(y, p) = -\sum_{k=0}^{K-1} y_k \log(p_k) = -\log(p_{k^*})

Because yk=0.0y_k = 0.0 for every non-target class, multiplying −log⁡(pk)-\log(p_k) by yky_k annihilates three of the four terms. Only the active class k∗=1k^* = 1 survives the dot product, collapsing the entire multi-class sum to a single number: −log⁡(p1)-\log(p_1).

Work through the numbers

First compute the unweighted surprises −log⁡(pk)-\log(p_k) at the default setting boost equal to 0.0, where p = [0.45, 0.25, 0.20, 0.10]. Class 0 has −log⁡(0.45)=0.79851-\log(0.45) = 0.79851, class 1 has −log⁡(0.25)=1.38629-\log(0.25) = 1.38629, class 2 has −log⁡(0.20)=1.60944-\log(0.20) = 1.60944, and class 3 has −log⁡(0.10)=2.30259-\log(0.10) = 2.30259. Summing all four unweighted surprises would give 6.09683.

Now multiply each surprise by its target weight yky_k from y = [0.0, 1.0, 0.0, 0.0]:

  • Class 0: 0.0×0.79851=0.00.0 \times 0.79851 = 0.0
  • Class 1: 1.0×1.38629=1.386291.0 \times 1.38629 = 1.38629
  • Class 2: 0.0×1.60944=0.00.0 \times 1.60944 = 0.0
  • Class 3: 0.0×2.30259=0.00.0 \times 2.30259 = 0.0

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 p1=0.25p_1 = 0.25 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 −yklog⁡(pk)-y_k \log(p_k). 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, p1p_1 is 0.25 and the cross-entropy sum is 1.386. Moving boost to 0.4 raises p1p_1 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, p1p_1 reaches 0.85 and the loss falls to 0.163.

Four classes with one-hot target at class 1 and candidate probabilities 0.45, 0.25, 0.20, 0.10 zero out three terms and leave 1.386.
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.

Four classes with one-hot target at class 1; the starter omits multiplying by y and sums all four log penalties.
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 −log⁡(pk∗)-\log(p_{k^*}) instead of computing a full vector product. That shortcut is algebraically identical to −∑kyklog⁡(pk)-\sum_k y_k \log(p_k) whenever y is a one-hot vector.

However, notice what one-hot cross-entropy ignores: if p1p_1 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 y places 1.0 on the true class k∗k^* and 0.0 on every other class.
  • Multiplying −log⁡(pk)-\log(p_k) by yky_k zeroes out all non-target terms, reducing −∑kyklog⁡(pk)-\sum_k y_k \log(p_k) to −log⁡(pk∗)-\log(p_{k^*}).
  • Under a one-hot target, rearranging probability mass among the wrong classes without changing pk∗p_{k^*} leaves the cross-entropy loss unchanged.