Speed

Same Correct Count Can Have Different Loss

Scale signed margins to see why thresholded accuracy stays constant while mean cross-entropy changes with prediction confidence.

The problem

Suppose four observations receive signed margin scores 0.5, 1.0, 1.5, and -0.2 for their true classes. Passing a scaled score through the sigmoid function produces a probability above 0.5 whenever the score is positive, and below 0.5 whenever it is negative:

pi=σ(scale⋅zi)=11+e−scale⋅zi,L=1N∑i=0N−1−log⁡(pi)p_i = \sigma(\text{scale} \cdot z_i) = \frac{1}{1 + e^{-\text{scale} \cdot z_i}}, \qquad L = \frac{1}{N}\sum_{i=0}^{N-1} -\log(p_i)

Multiplying all four scores by any positive scale factor leaves their signs unchanged: rows 0, 1, and 2 remain positive, and row 3 remains negative.

Consequently, a threshold rule at 0.5 always gets the exact same correct count of 3 out of 4 rows (accuracy 0.75), regardless of whether scale is 0.5, 1.0, 2.0, or 8.0. Mean cross-entropy behaves differently: because −log⁡(p)-\log(p) varies continuously with pp, for modest scales up to about 3.25 pushing the three positive margins higher reduces their losses more than sharpening the negative margin -0.2 increases row 3's loss.

Work through the numbers

With the scores unscaled (scale equal to 1.0), the probabilities σ(zi)\sigma(z_i) are 0.62246, 0.73106, 0.81757, and 0.45017. Three of those four numbers exceed 0.5, so the correct count is 3. Their per-row surprises −log⁡(pi)-\log(p_i) are 0.47408, 0.31326, 0.20141, and 0.79814. Adding those four values gives 1.78689, and dividing by 4 produces a mean cross-entropy of 0.44672.

Now compare scale equal to 0.5 against scale equal to 2.0. At scale equal to 0.5, the scaled scores [0.25, 0.5, 0.75, -0.1] map to probabilities 0.56218, 0.62246, 0.67918, and 0.47502. Three probabilities still exceed 0.5, yet the mean cross-entropy is 0.54532. At scale equal to 2.0, the probabilities become 0.73106, 0.88080, 0.95257, and 0.40131; the correct count is still 3, while the mean cross-entropy drops to 0.35045.

The live model

The three blue bars mark the rows with p>0.5p > 0.5, the red bar marks row 3 (p≤0.5p \le 0.5), and the vertical brown line marks the 0.5 decision threshold. Dragging scale from 0.5 to 8.0 stretches every probability away from 0.5 without ever crossing the brown line.

At scale equal to 0.5 the correct count is 3 and the mean loss is 0.545. At scale equal to 1.0 the mean loss is 0.447; at 1.5 it drops to 0.386; and at 2.0 it reaches 0.350.

The fall in mean loss stops as you drag further right. Row 3 is on the wrong side, and its loss grows without bound as the scores are stretched: the mean is smallest near a scale of 3.25 (0.3239, or 0.325 at the slider stops 3.0 and 3.5) and then rises again, reaching 0.45060 (0.451) at the slider endpoint scale equal to 8.0 (and about 0.533 at 10, above 1.0 at 20), while the correct count is still 3. Confidence only lowers cross-entropy while the confident rows are right.

At scale 1.0 three of four probabilities exceed 0.5 (correct count 3) and the mean cross-entropy loss is 0.447.
let scale = scene.parameter("scale", 1.0, 0.5, 8.0, 0.5);
let p = (input * scale).sigmoid();
let correct = p.gt(0.5).to(DType.F64).sum().reshape(1);
let loss = p.log().neg().mean().reshape(1);
let answer = torch.cat(torch.list().add(correct).add(loss), 0);
out.set("probabilities", p).set("correct", correct).set("mean_loss", loss);
{
  "probabilities": [
    0.6224593312018546,
    0.7310585786300049,
    0.8175744761936437,
    0.45016600268752216
  ],
  "correct": [
    3
  ],
  "mean_loss": [
    0.44672270476566844
  ]
}
Watch the parameter change

Your turn

At scale = 1.0 the starter reports a loss of 0.25, which is the zero-one error rate (1/41 / 4) and stays completely flat across all slider stops; by hand the four-row mean cross-entropy at scale = 1.0 is 0.44672270. Fix loss so it measures the batch mean cross-entropy, returning the two-element answer vector [correct, loss].

The starter replaces mean cross-entropy with the zero-one error rate 0.25, missing how confidence changes the loss.
let scale = scene.parameter("scale", 1.0, 0.5, 8.0, 0.5);
let p = (input * scale).sigmoid();
let correct = p.gt(0.5).to(DType.F64).sum().reshape(1);
let loss = p.le(0.5).to(DType.F64).mean().reshape(1);
let answer = torch.cat(torch.list().add(correct).add(loss), 0);
out.set("probabilities", p).set("correct", correct).set("mean_loss", loss);
{
  "probabilities": [
    0.6224593312018546,
    0.7310585786300049,
    0.8175744761936437,
    0.45016600268752216
  ],
  "correct": [
    3
  ],
  "mean_loss": [
    0.25
  ]
}

Summary loss must measure the batch mean cross-entropy rather than the zero-one error fraction, keeping the two-element answer [correct, loss]. Check tests new score tables and scales.

Restoring −log⁡(p)-\log(p) moves the reported loss from the flat 0.25 to the confidence-sensitive 0.44672270, so the default run returns [3.0, 0.44672270]. Check also evaluates two unseen score vectors at scale values 1.5 and 0.5, rejecting models that return the unnormalized sum of losses, ignore scale, or return a hardcoded constant.

Read the result carefully

Because thresholded counts are step functions of the scores, their derivative is zero almost everywhere and undefined at the threshold itself. Gradient-based training uses cross-entropy precisely because it rewards moving probabilities in the right direction even before or after a row crosses the 0.5 boundary. Conversely, a lower cross-entropy on a batch does not guarantee that more rows crossed the threshold; both numbers answer distinct questions.

Recap

  • Multiplying non-zero scores [0.5, 1.0, 1.5, -0.2] by any positive scale keeps their signs fixed, so correct stays at 3 out of 4.
  • As scale rises from 0.5 to 1.0 to 2.0, the mean cross-entropy falls from 0.54532 to 0.44672 to 0.35045, reaches a minimum of 0.3239 near scale = 3.25, and turns back up to 0.45060 at scale = 8.0.
  • Hard classification counts only check which side of 0.5 each probability lands on, whereas cross-entropy measures how close each true-class probability is to 1.0.