Log Is the Named Boundary
Compare the logarithmic penalty -log(p) against the linear error 1 - p as probabilities approach the zero boundary.
The problem
Why does cross-entropy wrap the predicted probability inside instead of measuring the linear shortfall ? Near , the two formulas agree almost perfectly: their values and slopes at are both and .
As moves toward , however, their behavior splits completely. The linear penalty is bounded above by : dropping from 0.05 to 0.0001 barely moves from 0.95 to 0.9999. By contrast, is a vertical asymptote of , erecting an infinite boundary wall that punishes near-zero probabilities on true outcomes with rapidly escalating loss.
Work through the numbers
Compare and across four true-class probabilities [0.90, 0.50, 0.20, 0.05] at the default setting floor equal to 0.0:
- At : linear , logarithmic , excess
0.00536. - At : linear , logarithmic , excess
0.19315. - At : linear , logarithmic , excess
0.80944. - At : linear , logarithmic , excess
2.04573.
Averaging those four excess values gives a mean curvature gap of 0.76342 nats. Over two-thirds of that gap comes from the single row at .
The live model
For each row, the top blue bar shows the linear error and the bottom red bar shows the logarithmic penalty . The floor slider pulls probabilities away from the zero boundary using p = input * (1.0 - fl) + fl.
At floor equal to 0.0, row 3 sits at with and the mean excess over is 0.763. Raising floor to 0.05 lifts row 3's probability to 0.0975, cutting its log penalty to 2.328 and dropping the mean excess to 0.567 (0.56667). At floor equal to 0.10, row 3 reaches 0.145 () and the mean excess falls to 0.445 (0.44528).
![Probabilities 0.90, 0.50, 0.20, 0.05 compare log penalty [0.105, 0.693, 1.609, 2.996] against linear penalty [0.10, 0.50, 0.80, 0.95] with mean gap 0.763.](../assets/ml/log-is-the-named-boundary--demo.png)
let fl = scene.parameter("floor", 0.0, 0.0, 0.10, 0.05);
let p = input * (1.0 - fl) + fl;
let ll = p.log().neg();
let lin = 1.0 - p;
let gap = (ll - lin).mean().reshape(1);
let answer = torch.cat(torch.list().add(ll).add(gap), 0);
out.set("p", p).set("log_loss", ll).set("lin_loss", lin).set("gap", gap);
{
"p": [
0.9,
0.5,
0.2,
0.05
],
"log_loss": [
0.10536051565782628,
0.6931471805599453,
1.6094379124341003,
2.995732273553991
],
"lin_loss": [
0.09999999999999998,
0.5,
0.8,
0.95
],
"gap": [
0.7634194705514658
]
}Your turn
At floor = 0.0 the starter reports ll = [0.10, 0.50, 0.80, 0.95] and gap = 0.0 because ll duplicates the linear shortfall; by hand the logarithmic penalties are [0.10536, 0.69315, 1.60944, 2.99573] and the mean excess over is 0.76342. Fix ll so it evaluates the logarithmic penalty for each row.

let fl = scene.parameter("floor", 0.0, 0.0, 0.10, 0.05);
let p = input * (1.0 - fl) + fl;
let ll = 1.0 - p;
let lin = 1.0 - p;
let gap = (ll - lin).mean().reshape(1);
let answer = torch.cat(torch.list().add(ll).add(gap), 0);
out.set("p", p).set("log_loss", ll).set("lin_loss", lin).set("gap", gap);
{
"p": [
0.9,
0.5,
0.2,
0.05
],
"log_loss": [
0.09999999999999998,
0.5,
0.8,
0.95
],
"lin_loss": [
0.09999999999999998,
0.5,
0.8,
0.95
],
"gap": [
0
]
}Compute ll as the logarithmic penalty for each row so gap measures the mean curvature excess over the linear shortfall 1 - p. Check tests unseen probability vectors and floor settings.
With the logarithmic penalty restored, the default run returns [0.10536052, 0.69314718, 1.60943791, 2.99573227, 0.76341947]. The check reruns the comparison on [0.85, 0.60, 0.25, 0.10] at floor = 0.05 and on [0.95, 0.40, 0.15, 0.02] at floor = 0.10.
Read the result carefully
The power-series expansion for explains why both curves start together near ( near zero) and why always lies above for every :
In numerical code, the boundary at also explains why libraries either compute cross-entropy directly from logits (log_softmax) or clamp raw probabilities away from zero. Evaluating directly produces and breaks downstream arithmetic.
Recap
- Near , is well approximated by the linear shortfall .
- As approaches , saturates at whereas grows without bound toward .
- Lifting small probabilities away from the boundary with a positive
floorsharply reduces the curvature gap between and .