Speed

One Small Probability Raises the Mean

See how a single confident wrong prediction dominates the batch mean cross-entropy because negative log-probability curves sharply upward near zero.

The problem

Suppose a model evaluates four labelled rows and assigns high probabilities 0.9, 0.8, and 0.9 to the true class on the first three rows. On row 3, however, its base probability of 0.5 is multiplied by a factor scale equal to 0.1, leaving only 0.05 for the actual outcome. Under zero-one accuracy at threshold 0.5, three out of four rows are right. Under cross-entropy, that single confident miss changes the batch summary dramatically.

The reason is the curvature of −log⁡(p)-\log(p). When pp drops from 0.9 to 0.8, the penalty rises by only 0.11778. When pp drops from 0.5 to 0.05, the penalty jumps from 0.69315 to 2.99573. Because the negative logarithm grows without bound as pp approaches zero, one tiny probability can outweigh several well-scored rows in the batch mean:

ℓˉ=1N∑i=0N−1−log⁡(pi)>−log⁡ ⁣(1N∑i=0N−1pi)\bar{\ell} = \frac{1}{N}\sum_{i=0}^{N-1} -\log(p_i) > -\log\!\left(\frac{1}{N}\sum_{i=0}^{N-1} p_i\right)

Work through the numbers

At the default setting scale equal to 0.1, the four true-class probabilities are 0.9, 0.8, 0.9, and 0.05. Their individual row losses −log⁡(p)-\log(p) are approximately 0.10536, 0.22314, 0.10536, and 2.99573. The first three rows together sum to 0.43386, with an average of 0.14462 across those three rows.

Row 3 alone contributes 2.99573, which is nearly seven times the combined loss of the first three rows. Adding all four row losses gives 3.42960, and dividing by 4 produces the batch mean cross-entropy 0.85740. Notice that averaging the probabilities first gives (0.9+0.8+0.9+0.05)/4=0.6625(0.9 + 0.8 + 0.9 + 0.05) / 4 = 0.6625, whose negative logarithm is only 0.41173. Because −log⁡(p)-\log(p) is strictly convex, the mean of the losses is always larger than the loss of the mean probability whenever the rows differ.

The live model

The three blue bars show the stable losses of rows 0, 1, and 2, while the red bar shows row 3 and the vertical green line marks the four-row mean. The scale slider multiplies row 3's base probability of 0.5 before taking −log⁡(p)-\log(p).

At scale equal to 0.1, row 3 has probability 0.05, row loss 2.996, and batch mean 0.857. Moving scale to 0.4 raises row 3's probability to 0.20, cutting its loss to 1.609 and pulling the batch mean down to 0.511. At scale equal to 0.7 the batch mean is 0.371, and at the upper endpoint scale equal to 1.0 row 3 has probability 0.50, row loss 0.693, and batch mean 0.282.

Row losses 0.105, 0.223, 0.105 and 2.996 at scale 0.1 pull the batch mean up to 0.857.
let scale = scene.parameter("scale", 0.1, 0.1, 1.0, 0.3);
let p = input.clone();
p.set(3, input.value(3) * scale);
let row_loss = p.log().neg();
let mean_loss = row_loss.mean().reshape(1);
let answer = torch.cat(torch.list().add(row_loss).add(mean_loss), 0);
out.set("row_loss", row_loss).set("mean_loss", mean_loss);
{
  "row_loss": [
    0.10536051565782628,
    0.2231435513142097,
    0.10536051565782628,
    2.995732273553991
  ],
  "mean_loss": [
    0.8573992140459633
  ]
}
Watch the parameter change

Your turn

At scale = 0.1 the starter reports a batch summary of 0.41173 because it averages the four probabilities before taking the logarithm; by hand the mean of the four row losses is 3.42960/4=0.857403.42960 / 4 = 0.85740. Fix mean_loss so it averages the per-row losses across the batch, keeping the answer vector of row losses followed by their mean.

The starter takes -log(mean(p)) instead of averaging the four row losses, understating the batch mean as 0.412 instead of 0.857.
let scale = scene.parameter("scale", 0.1, 0.1, 1.0, 0.3);
let p = input.clone();
p.set(3, input.value(3) * scale);
let row_loss = p.log().neg();
let mean_loss = p.mean().log().neg().reshape(1);
let answer = torch.cat(torch.list().add(row_loss).add(mean_loss), 0);
out.set("row_loss", row_loss).set("mean_loss", mean_loss);
{
  "row_loss": [
    0.10536051565782628,
    0.2231435513142097,
    0.10536051565782628,
    2.995732273553991
  ],
  "mean_loss": [
    0.4117347211217598
  ]
}

Summary loss must be the batch mean of the individual row losses rather than the loss of the mean probability. Check tests new batch sizes and outlier scales.

Averaging the per-row losses instead of the probabilities raises the reported mean from the understated 0.41173 to 0.85739921, and the default run returns [0.10536052, 0.22314355, 0.10536052, 2.99573227, 0.85739921]. Check also runs unseen probability vectors of 4 and 5 rows at scale values 0.4 and 0.7, rejecting models that return the unnormalized sum or divide by a hardcoded 4.

Read the result carefully

Sensitivity to small probabilities is both a strength and a hazard of cross-entropy. During training it forces the model to pay attention to confident mistakes rather than ignoring a rare miss. On noisy evaluation data, however, a single mislabelled example scored near zero can inflate the batch average, so inspecting the distribution of row losses alongside the mean prevents a single outlier from being misread as uniform degradation.

Recap

  • The negative logarithm −log⁡(p)-\log(p) is steep near zero: p=0.05p = 0.05 yields loss 2.99573, whereas p=0.9p = 0.9 yields 0.10536.
  • A single row at p=0.05p = 0.05 raises the four-row mean cross-entropy from 0.14462 on the first three rows to 0.85740 overall.
  • By convexity, the average of −log⁡(p)-\log(p) across rows (0.85740) is strictly greater than −log⁡(pˉ)-\log(\bar{p}) (0.41173).