Speed

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 pp inside −log⁡(p)-\log(p) instead of measuring the linear shortfall 1−p1 - p? Near p=1p = 1, the two formulas agree almost perfectly: their values and slopes at p=1p = 1 are both 00 and −1-1.

As pp moves toward 00, however, their behavior splits completely. The linear penalty 1−p1 - p is bounded above by 11: dropping pp from 0.05 to 0.0001 barely moves 1−p1 - p from 0.95 to 0.9999. By contrast, p=0p = 0 is a vertical asymptote of −log⁡(p)-\log(p), erecting an infinite boundary wall that punishes near-zero probabilities on true outcomes with rapidly escalating loss.

Work through the numbers

Compare −log⁡(p)-\log(p) and 1−p1 - p across four true-class probabilities [0.90, 0.50, 0.20, 0.05] at the default setting floor equal to 0.0:

  • At p=0.90p = 0.90: linear 1−p=0.100001 - p = 0.10000, logarithmic −log⁡(0.90)=0.10536-\log(0.90) = 0.10536, excess 0.00536.
  • At p=0.50p = 0.50: linear 1−p=0.500001 - p = 0.50000, logarithmic −log⁡(0.50)=0.69315-\log(0.50) = 0.69315, excess 0.19315.
  • At p=0.20p = 0.20: linear 1−p=0.800001 - p = 0.80000, logarithmic −log⁡(0.20)=1.60944-\log(0.20) = 1.60944, excess 0.80944.
  • At p=0.05p = 0.05: linear 1−p=0.950001 - p = 0.95000, logarithmic −log⁡(0.05)=2.99573-\log(0.05) = 2.99573, excess 2.04573.

Averaging those four excess values (0.00536+0.19315+0.80944+2.04573)/4(0.00536 + 0.19315 + 0.80944 + 2.04573) / 4 gives a mean curvature gap of 0.76342 nats. Over two-thirds of that gap comes from the single row at p=0.05p = 0.05.

The live model

For each row, the top blue bar shows the linear error 1−p1 - p and the bottom red bar shows the logarithmic penalty −log⁡(p)-\log(p). 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 p=0.05p = 0.05 with −log⁡(p)=2.996-\log(p) = 2.996 and the mean excess over 1−p1 - p 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 (−log⁡(p)=1.931-\log(p) = 1.931) 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.
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 1−p1 - p is 0.76342. Fix ll so it evaluates the logarithmic penalty −log⁡(p)-\log(p) for each row.

Probabilities 0.90, 0.50, 0.20, 0.05; the starter uses linear penalty 1 - p for ll and gets zero gap.
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 x=1−px = 1 - p explains why both curves start together near p=1p = 1 (xx near zero) and why −log⁡(p)-\log(p) always lies above 1−p1 - p for every p∈(0,1)p \in (0, 1):

−log⁡(1−x)=x+x22+x33+⋯>x-\log(1 - x) = x + \frac{x^2}{2} + \frac{x^3}{3} + \cdots > x

In numerical code, the boundary at p=0p = 0 also explains why libraries either compute cross-entropy directly from logits (log_softmax) or clamp raw probabilities away from zero. Evaluating log⁡(0)\log(0) directly produces −∞-\infty and breaks downstream arithmetic.

Recap

  • Near p=1p = 1, −log⁡(p)-\log(p) is well approximated by the linear shortfall 1−p1 - p.
  • As pp approaches 00, 1−p1 - p saturates at 1.01.0 whereas −log⁡(p)-\log(p) grows without bound toward +∞+\infty.
  • Lifting small probabilities away from the p=0p = 0 boundary with a positive floor sharply reduces the curvature gap between −log⁡(p)-\log(p) and 1−p1 - p.