Surprise Adds Over Rows
Compute the negative log-probability for each row and verify that multiplying independent row probabilities turns into adding their surprises.
The problem
When a classifier scores a batch of labelled rows, each row has one probability assigned to its actual outcome. Suppose a four-row table gives the true-class probabilities 0.8, 0.5, 0.25, and 0.1. If the rows represent independent observations, the probability of seeing all four outcomes together is the product of those four numbers. Multiplying many small probabilities quickly shrinks toward zero, and gradients of a long product couple every factor to the product of all the others.
Cross-entropy replaces the product of probabilities with a sum of surprise values. For a single row whose true class receives probability p, the surprise is , using the natural logarithm:
Because lies between zero and one, is negative or zero, and the minus sign makes surprise non-negative. This book works row by row across a batch first, taking each row's true-class probability as given; the companion book Cross-Entropy, Exactly works term by term inside a single row's class distribution second.
Work through the numbers
Start with the joint probability of the four rows at the default setting blend equal to 0.0. Multiply in order: 0.8 times 0.5 gives 0.4; 0.4 times 0.25 gives 0.1; and 0.1 times 0.1 equals 0.01. The negative natural logarithm of that joint probability is , or approximately 4.60517.
Now compute separately for each row. Row 0 has probability 0.8 and surprise , or about 0.22314. Row 1 has probability 0.5 and surprise , or about 0.69315. Row 2 has probability 0.25, which is 0.5 times 0.5, so its surprise is 0.69315 plus 0.69315, or about 1.38629. Row 3 has probability 0.1 and surprise , or about 2.30259. Adding 0.22314, 0.69315, 1.38629, and 2.30259 yields 4.60517, matching to full floating-point precision.
The live model
The blue bars on the left show the four true-class probabilities, and the red bars on the right show each row's surprise . The blend slider mixes each base probability with 0.5 according to p = input * (1.0 - blend) + blend * 0.5.
At blend equal to 0.0 the probabilities stay at [0.8, 0.5, 0.25, 0.1] and the total surprise is 4.605. Moving blend to 0.2 raises the lowest probability from 0.1 to 0.18 and lowers the total surprise to 3.913. At the upper endpoint blend equal to 0.5, the probabilities become [0.65, 0.5, 0.375, 0.3] and the total surprise falls to 3.309.

let blend = scene.parameter("blend", 0.0, 0.0, 0.5, 0.1);
let p = input * (1.0 - blend) + blend * 0.5;
let surprise = p.log().neg();
let total = surprise.sum().reshape(1);
let answer = torch.cat(torch.list().add(surprise).add(total), 0);
out.set("probabilities", p).set("surprise", surprise).set("total", total);
{
"probabilities": [
0.8,
0.5,
0.25,
0.1
],
"surprise": [
0.2231435513142097,
0.6931471805599453,
1.3862943611198906,
2.3025850929940455
],
"total": [
4.605170185988091
]
}Watch the parameter change
Your turn
At blend = 0.0 the starter reports row contributions [-0.22314, -0.69315, -1.38629, -2.30259] and a total of -4.60517, whereas by hand the four surprises are positive and sum to 4.60517. Fix surprise so each row measures positive surprise , while keeping the appended five-element answer vector of four row surprises followed by their sum.

let blend = scene.parameter("blend", 0.0, 0.0, 0.5, 0.1);
let p = input * (1.0 - blend) + blend * 0.5;
let surprise = p.log();
let total = surprise.sum().reshape(1);
let answer = torch.cat(torch.list().add(surprise).add(total), 0);
out.set("probabilities", p).set("surprise", surprise).set("total", total);
{
"probabilities": [
0.8,
0.5,
0.25,
0.1
],
"surprise": [
-0.2231435513142097,
-0.6931471805599453,
-1.3862943611198906,
-2.3025850929940455
],
"total": [
-4.605170185988091
]
}Each row surprise must be non-negative before summing across the batch. Check tests new probability vectors and slider blends.
Negating the logarithm turns the starter's negative row contributions into the four positive surprises [0.22314355, 0.69314718, 1.38629436, 2.30258509], so the default run outputs those and the total 4.60517019. Check also evaluates two unseen probability tables at blend values 0.2 and 0.4, rejecting linear penalties and models that ignore blend.
Read the result carefully
Adding surprises over rows relies on the logarithmic identity . That identity turns a product of probabilities into an additive loss whose gradient is a sum of per-row gradients. It does not prove that the real observations were statistically independent; if several rows come from the same repeated user or sensor burst, summing their surprises overweights that correlated cluster.
Recap
- The surprise of a row with true-class probability
pis , which is zero at and grows without bound as approaches zero. - Multiplying independent row probabilities is equivalent to adding their per-row surprises to get
4.60517. - Pulling extreme probabilities toward
0.5with theblendslider reduces the large penalties on low-probability rows more than it increases the small penalties on high-probability rows.