Speed

What Cross-Entropy Is and Is Not

Evaluate binary cross-entropy across four noisy subpopulations and separate reducible calibration shift from irreducible outcome entropy.

The problem

When a trained classifier reports a cross-entropy of 0.60 nats on a validation set, does that number mean the model is poorly calibrated or making avoidable mistakes? Not necessarily.

Cross-entropy H(q,p)H(q, p) is a proper scoring rule: for any true binary probability qq,

H(q,p)=−(qlog⁡(p)+(1−q)log⁡(1−p))≥H(q)=−(qlog⁡(q)+(1−q)log⁡(1−q))H(q, p) = -\bigl(q \log(p) + (1 - q)\log(1 - p)\bigr) \ge H(q) = -\bigl(q \log(q) + (1 - q)\log(1 - q)\bigr)

achieves its minimum when p=qp = q. However, that minimum value is the outcome's own binary entropy H(q)H(q), which is strictly positive whenever 0<q<10 < q < 1. Cross-entropy alone cannot tell you how much of 0.60 is irreducible noise H(q)H(q) and how much is reducible model error H(q,p)−H(q)H(q, p) - H(q).

Work through the numbers

Consider four groups whose true positive rates are q = [0.85, 0.70, 0.55, 0.40]. Suppose a model predicts p = q + shift. At shift equal to 0.0, the model is Bayes-optimal (p=qp = q), yet every group still has positive binary entropy H(q)H(q):

  • Group 0 (q=0.85q = 0.85): −(0.85log⁡(0.85)+0.15log⁡(0.15))=0.42271-(0.85 \log(0.85) + 0.15 \log(0.15)) = 0.42271
  • Group 1 (q=0.70q = 0.70): −(0.70log⁡(0.70)+0.30log⁡(0.30))=0.61086-(0.70 \log(0.70) + 0.30 \log(0.30)) = 0.61086
  • Group 2 (q=0.55q = 0.55): −(0.55log⁡(0.55)+0.45log⁡(0.45))=0.68814-(0.55 \log(0.55) + 0.45 \log(0.45)) = 0.68814
  • Group 3 (q=0.40q = 0.40): −(0.40log⁡(0.40)+0.60log⁡(0.60))=0.67301-(0.40 \log(0.40) + 0.60 \log(0.60)) = 0.67301

Averaging those four numbers gives a mean irreducible floor mh = 0.59868 nats and an excess mce - mh = 0.00000.

Now shift every prediction by shift = +0.10 (p = [0.95, 0.80, 0.65, 0.50]) or shift = -0.10 (p = [0.75, 0.60, 0.45, 0.30]). At shift = +0.10, the mean cross-entropy rises to 0.63362 (excess +0.03494). At shift = -0.10, it rises to 0.62218 (excess +0.02350).

The live model

For each group, the top green bar shows the fixed Bayes floor H(q)H(q) and the bottom blue bar shows the model's cross-entropy H(q,p)H(q, p). The shift slider offsets all four predicted probabilities by p = q + shift from -0.10 to +0.10.

At shift equal to 0.0, every blue bar matches its green bar, mean CE equals mean H(q) = 0.599, and excess is 0.000. Moving shift down to -0.05 or up to +0.05 raises mean CE to 0.605 (excess = 0.006) or 0.606 (excess = 0.007). At the endpoints -0.10 and +0.10, mean CE climbs to 0.622 (excess = 0.024) and 0.634 (excess = 0.035).

Four true binary probabilities 0.85, 0.70, 0.55, 0.40 have irreducible mean entropy 0.599 even when predicted probabilities match q with zero shift.
let shift = scene.parameter("shift", 0.0, -0.10, 0.10, 0.05);
let q = input;
let p = q + shift;
let ce = (q * p.log() + (1.0 - q) * (1.0 - p).log()).neg();
let h = (q * q.log() + (1.0 - q) * (1.0 - q).log()).neg();
let mce = ce.mean().reshape(1);
let mh = h.mean().reshape(1);
let excess = mce - mh;
let answer = torch.cat(torch.list().add(mce).add(mh).add(excess), 0);
out.set("ce", ce).set("h", h).set("mce", mce).set("excess", excess);
{
  "ce": [
    0.4227090878059909,
    0.6108643020548935,
    0.6881388137135884,
    0.6730116670092565
  ],
  "h": [
    0.4227090878059909,
    0.6108643020548935,
    0.6881388137135884,
    0.6730116670092565
  ],
  "mce": [
    0.5986809676459324
  ],
  "excess": [
    0
  ]
}

Your turn

At shift = 0.0 the starter reports mce = 0.33387 and a negative excess = -0.26481 because ce only includes the positive-outcome contribution; by hand the full binary cross-entropy across both outcomes equals the target entropy 0.59868 (excess = 0.0). Fix ce so it evaluates the complete two-term binary cross-entropy for each group.

Four true binary probabilities 0.85, 0.70, 0.55, 0.40; the starter omits the negative-outcome term (1 - q) * log(1 - p).
let shift = scene.parameter("shift", 0.0, -0.10, 0.10, 0.05);
let q = input;
let p = q + shift;
let ce = (q * p.log()).neg();
let h = (q * q.log() + (1.0 - q) * (1.0 - q).log()).neg();
let mce = ce.mean().reshape(1);
let mh = h.mean().reshape(1);
let excess = mce - mh;
let answer = torch.cat(torch.list().add(mce).add(mh).add(excess), 0);
out.set("ce", ce).set("h", h).set("mce", mce).set("excess", excess);
{
  "ce": [
    0.1381410900731087,
    0.2496724607571127,
    0.32881035041559126,
    0.366516292749662
  ],
  "h": [
    0.4227090878059909,
    0.6108643020548935,
    0.6881388137135884,
    0.6730116670092565
  ],
  "mce": [
    0.27078504849886864
  ],
  "excess": [
    -0.32789591914706373
  ]
}

Complete ce so it evaluates the full binary cross-entropy across both positive and negative outcomes. Check tests unseen probability vectors and shift values.

With the full formula restored, the default run at shift = 0.0 returns [0.59868112, 0.59868112, 0.0]. The check reruns the full formula on q = [0.80, 0.65, 0.50, 0.35] at shift = -0.05 and on q = [0.75, 0.60, 0.45, 0.30] at shift = +0.10.

Read the result carefully

Because both positive and negative shifts raise mce above mh, cross-entropy rewards honest probability estimates (p=qp = q). Unlike accuracy, which is unchanged by any shift that stays on the same side of 0.50, cross-entropy strictly penalizes predicting too high (shift = +0.10) and predicting too low (shift = -0.10).

At the same time, cross-entropy is not a standalone measure of model quality across different datasets, nor is it a substitute for task-specific decision costs. A weather task with noisy outcomes (qq near 0.55) will always have a higher cross-entropy floor (0.68814) than a deterministic sensor task (qq near 0.99), even when the weather model is calibrated to perfection.

Recap

  • Binary cross-entropy −(qlog⁡(p)+(1−q)log⁡(1−p))-\bigl(q \log(p) + (1 - q)\log(1 - p)\bigr) is minimized when p=qp = q, making it a strictly proper scoring rule.
  • Even at p=qp = q, cross-entropy equals the target entropy H(q)H(q) (0.59868 across [0.85, 0.70, 0.55, 0.40]), not zero.
  • Cross-entropy measures probabilistic fit relative to an unknown entropy floor; it does not replace calibration tables, slice checks, or asymmetric mistake costs.