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 .
When a tiny batch contains both positive rows () and negative rows (), we cannot simply take for every row. For a negative row (), a low p_pos like 0.25 is a confident right prediction because the model assigns to the actual label 0.
The binary cross-entropy formula handles both cases in one expression and averages across the rows in the batch:
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 (, ): , row loss
- Row 1 (, ): , row loss
- Row 2 (, ): , row loss
- Row 3 (, ): , row loss
Rows 0 and 1 are both right predictions () with small losses (0.22314 and 0.28768). Row 2 is a hesitant positive miss (, loss 0.91629), and Row 3 is a confident false alarm ( when , so 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 . 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.](../assets/ml/average-over-a-tiny-batch--demo.png)
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 . 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.](../assets/ml/average-over-a-tiny-batch--exercise.png)
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 makes the loss scale invariant to whether a mini-batch contains 4 rows, 32 rows, or 256 rows. Without averaging over , 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 () and Row 3 () behave: for negative rows, a high p_pos means a low and therefore a high cross-entropy penalty.
Recap
- For a binary label , the true-class probability is .
- Each row's binary cross-entropy is , which equals when and when .
- Averaging across the rows keeps the batch loss independent of the batch size.