Paul’s learnings

Neural networks › Loss, softmax and calibration

Loss, softmax and calibration

Loss functions formalize what optimization rewards; softmax maps unbounded logits into categorical distributions; calibration aligns confidence with empirical accuracy.

Cross-entropy and the linear softmax gradient

For a discrete distribution over K classes with ground-truth one-hot target y and model logits z∈ℝK, softmax converts logits into normalized probabilities:

pi=ezi/τ∑j=1Kezj/τ

To avoid numerical overflow in floating-point hardware, implementations subtract zmax=maxjzj from all logits before exponentiating. Because e(zi−zmax)/∑e(zj−zmax)=ezi/∑ezj, the mathematical distribution is identical while the maximum exponent is 0.0, eliminating Inf/NaN overflow.

The derivative of cross-entropy with respect to logits

Cross-entropy loss measures the negative log-likelihood of the true class:

LCE=−∑k=1Kyklogpk

Differentiating with respect to logit zi (with τ=1):

∂LCE∂zi=−∑k=1Kykpk∂pk∂zi=−yipipi(1−pi)−∑k≠iykpk(−pkpi) ∂LCE∂zi=−yi(1−pi)+∑k≠iykpi=pi(∑k=1Kyk)−yi

Because y is a probability distribution (∑yk=1):

∂LCE∂zi=pi−yi

The derivative is simply prediction minus target. The complicated exponential terms in the denominator of softmax and the division inside the logarithm cancel each other completely. That linear simplicity is why cross-entropy is the standard loss for all classification and language modeling.

Enable JavaScript to use this local playground. The explanation and sources below work without it.

Brier score, proper scoring rules, and calibration

The multiclass Brier score measures mean squared error over probability vectors:

BS=1N∑n=1N∑k=1K(pnk−ynk)2

Both cross-entropy and Brier score are strictly proper scoring rules: they are uniquely minimized in expectation if and only if the model's reported distribution p matches the true ground-truth posterior distribution p*. A proper scoring rule penalizes overconfident bluffing as well as unconfident hesitation.

Expected Calibration Error (ECE)

A classifier is calibrated if its predicted probability reflects real empirical accuracy: when a model assigns 80% probability to a class across 1 000 predictions, exactly 800 of those predictions should be correct. Expected Calibration Error (ECE) bins predictions by confidence:

ECE=∑m=1M|Bm|N|acc(Bm)−conf(Bm)|

Modern deep networks are notoriously overconfident (Guo et al., 2017). Temperature scaling calibrates a trained model post-hoc by optimizing a single positive scalar τ on a held-out validation set to minimize NLL. Because τ rescales all logits uniformly, it preserves the argmax class ranking exactly while smoothing overconfident probability spikes.

Decision Models & Single-Pass Pointer Readout Heads

When software systems use an LLM to route a ticket, select a tool, or score risk, autoregressive token generation is both wasteful and brittle: the model must emit dozens of syntax tokens ({"choice": "..."}), token probabilities fragment across surface synonyms, and raw softmax probabilities are notoriously overconfident on out-of-distribution inputs.

As detailed in our Decision Models report (Jev, Kev, and Laya), a Decision Pointer Head packs the state and candidate options into a structured sequence with reserved option delimiters (‹0› … ‹K−1› or pre-mask slots), runs a single forward pass, and computes the inner product between the projected state representation hstate and each option delimiter's hidden vector h‹k›. Crucially, it decouples Choice (which option wins under temperature-calibrated softmax) from the Act / Review Gate (which uses margin and entropy features of the raw distribution to abstain when evidence is ambiguous).

zk=(Wqhstate)T(Wkh‹k›)d·T,p=softmax(z) Confidence=0.65p(1)+0.35(p(1)−p(2)),Gate∈{act,review}

Single-Pass Option Pointer & Calibration Controls

Calibrated Option Distribution & Gate Decision

Sources and further reading