Speed

Average Over a Tiny Batch

Compute binary cross-entropy across a four-row batch with mixed positive and negative labels by selecting each row's true-class probability.

The problem

In binary classification, a model outputs a single number p_pos per row: the predicted probability that the row belongs to class 1. The probability that the row belongs to class 0 is the complement 1−ppos1 - p_{\text{pos}}.

When a tiny batch contains both positive rows (y=1y = 1) and negative rows (y=0y = 0), we cannot simply take −log⁡(ppos)-\log(p_{\text{pos}}) for every row. For a negative row (y=0y = 0), a low p_pos like 0.25 is a confident right prediction because the model assigns 1.0−0.25=0.751.0 - 0.25 = 0.75 to the actual label 0.

The binary cross-entropy formula handles both cases in one expression and averages across the NN rows in the batch:

ptrue,i=yipi+(1−yi)(1−pi),L=1N∑i=0N−1−log⁡(ptrue,i)p_{\text{true},i} = y_i p_i + (1 - y_i)(1 - p_i), \qquad L = \frac{1}{N}\sum_{i=0}^{N-1} -\log(p_{\text{true},i})

Work through the numbers

Consider a four-row batch with labels y = [1.0, 0.0, 1.0, 0.0] and baseline positive probabilities p0 = [0.80, 0.25, 0.40, 0.70] at pull equal to 0.0:

  • Row 0 (y=1y = 1, ppos=0.80p_{\text{pos}} = 0.80): ptrue=0.80p_{\text{true}} = 0.80, row loss −log⁡(0.80)=0.22314-\log(0.80) = 0.22314
  • Row 1 (y=0y = 0, ppos=0.25p_{\text{pos}} = 0.25): ptrue=1.0−0.25=0.75p_{\text{true}} = 1.0 - 0.25 = 0.75, row loss −log⁡(0.75)=0.28768-\log(0.75) = 0.28768
  • Row 2 (y=1y = 1, ppos=0.40p_{\text{pos}} = 0.40): ptrue=0.40p_{\text{true}} = 0.40, row loss −log⁡(0.40)=0.91629-\log(0.40) = 0.91629
  • Row 3 (y=0y = 0, ppos=0.70p_{\text{pos}} = 0.70): ptrue=1.0−0.70=0.30p_{\text{true}} = 1.0 - 0.70 = 0.30, row loss −log⁡(0.30)=1.20397-\log(0.30) = 1.20397

Rows 0 and 1 are both right predictions (ptrue>0.50p_{\text{true}} > 0.50) with small losses (0.22314 and 0.28768). Row 2 is a hesitant positive miss (ptrue=0.40p_{\text{true}} = 0.40, loss 0.91629), and Row 3 is a confident false alarm (ppos=0.70p_{\text{pos}} = 0.70 when y=0y = 0, so ptrue=0.30p_{\text{true}} = 0.30 and loss is 1.20397). Summing the four losses gives 2.63109, and dividing by 4 yields the batch mean 0.65777.

The live model

The blue bars on the left show each row's true-class probability p_true, and the red bars on the right show the row loss −log⁡(ptrue)-\log(p_{\text{true}}). The pull slider blends every p_pos toward 0.50 using p_pos = p0 * (1.0 - pull) + 0.5 * pull.

At pull equal to 0.0, p_true is [0.80, 0.75, 0.40, 0.30] and the batch mean is 0.658 (0.65777). Moving pull to 0.25 softens the two wrong rows (p_true rises from 0.40, 0.30 to 0.425, 0.35) while slightly dulling the two right rows (0.725, 0.6875), nudging the batch mean down to 0.650 (0.65044). At pull equal to 0.50, p_true becomes [0.65, 0.625, 0.45, 0.40] and the batch mean is 0.654 (0.65390).

Four binary rows with labels [1, 0, 1, 0] and positive probabilities [0.80, 0.25, 0.40, 0.70] give true-class probabilities [0.80, 0.75, 0.40, 0.30] and mean cross-entropy 0.658.
let pull = scene.parameter("pull", 0.0, 0.0, 0.50, 0.25);
let y = input.select(1, 0);
let p0 = input.select(1, 1);
let p_pos = p0 * (1.0 - pull) + 0.5 * pull;
let p_true = y * p_pos + (1.0 - y) * (1.0 - p_pos);
let losses = p_true.log().neg();
let mean_loss = losses.mean().reshape(1);
let answer = torch.cat(torch.list().add(losses).add(mean_loss), 0);
out.set("p_true", p_true).set("losses", losses).set("mean_loss", mean_loss);
{
  "p_true": [
    0.8,
    0.75,
    0.4,
    0.30000000000000004
  ],
  "losses": [
    0.2231435513142097,
    0.2876820724517809,
    0.916290731874155,
    1.203972804325936
  ],
  "mean_loss": [
    0.6577722899915204
  ]
}

Your turn

At pull = 0.0 the starter reports mean_loss = 2.63109, which is the unnormalized sum of the four row losses; by hand the batch average is 2.63109/4=0.657772.63109 / 4 = 0.65777. Fix mean_loss so it averages losses over the batch regardless of how many rows the batch contains.

Four binary rows with labels [1, 0, 1, 0]; the starter sums row losses instead of averaging.
let pull = scene.parameter("pull", 0.0, 0.0, 0.50, 0.25);
let y = input.select(1, 0);
let p0 = input.select(1, 1);
let p_pos = p0 * (1.0 - pull) + 0.5 * pull;
let p_true = y * p_pos + (1.0 - y) * (1.0 - p_pos);
let losses = p_true.log().neg();
let mean_loss = losses.sum().reshape(1);
let answer = torch.cat(torch.list().add(losses).add(mean_loss), 0);
out.set("p_true", p_true).set("losses", losses).set("mean_loss", mean_loss);
{
  "p_true": [
    0.8,
    0.75,
    0.4,
    0.30000000000000004
  ],
  "losses": [
    0.2231435513142097,
    0.2876820724517809,
    0.916290731874155,
    1.203972804325936
  ],
  "mean_loss": [
    2.6310891599660815
  ]
}

Compute mean_loss as the batch average of losses across all rows in the batch. Check tests unseen binary batches of varying row counts and pull settings.

With the batch average restored, the default run returns [0.22314355, 0.28768207, 0.91629073, 1.20397280, 0.65777229]. The check reruns the batch mean on a four-row batch at pull = 0.25 and on a five-row batch at pull = 0.50, rejecting models that divide by a hardcoded 4.0.

Read the result carefully

Dividing by the batch size NN makes the loss scale invariant to whether a mini-batch contains 4 rows, 32 rows, or 256 rows. Without averaging over NN, doubling the batch size would double the loss and double the gradient step size unless the learning rate were manually rescaled.

Notice also how Row 1 (y=0,ppos=0.25y = 0, p_{\text{pos}} = 0.25) and Row 3 (y=0,ppos=0.70y = 0, p_{\text{pos}} = 0.70) behave: for negative rows, a high p_pos means a low ptrue=1−pposp_{\text{true}} = 1 - p_{\text{pos}} and therefore a high cross-entropy penalty.

Recap

  • For a binary label y∈{0,1}y \in \{0, 1\}, the true-class probability is ptrue=yppos+(1−y)(1−ppos)p_{\text{true}} = y p_{\text{pos}} + (1 - y)(1 - p_{\text{pos}}).
  • Each row's binary cross-entropy is −log⁡(ptrue)-\log(p_{\text{true}}), which equals −log⁡(ppos)-\log(p_{\text{pos}}) when y=1y = 1 and −log⁡(1−ppos)-\log(1 - p_{\text{pos}}) when y=0y = 0.
  • Averaging −log⁡(ptrue)-\log(p_{\text{true}}) across the NN rows keeps the batch loss independent of the batch size.