Sam Babichenko

Reading notes / Cost-sensitive classification and calibration

Logit adjustment

Menon et al., 2021

Context. When a few classes dominate the training data, plain cross-entropy favors them, and the usual target becomes the balanced error, the average of the per-class error rates. Recent deep-net fixes either divide each class's logit by its weight norm after training or change the loss with per-class margins or weights; they work on benchmarks, but the margin and normalization fixes were not derived from the target's Bayes rule, which this paper writes down. The balanced error is the ordinary error under a uniform class prior, so its Bayes rule is arg⁡max⁡y[log⁡P(y∣x)−log⁡P(y)]\arg\max_y[\log P(y\mid x)-\log P(y)]: subtract the log prior from the logits, after training a standard model or inside the softmax during training. A correction that is multiplicative, or that looks at only one of the two labels being compared, is not consistent in general. This is Saerens-style prior correction toward a uniform prior, and in the binary case Elkan's threshold set at the base rate.

Setting. Labels y∈[L]y\in[L] with priors πy\pi_y, a scorer f:X→RLf:\mathcal X\to\mathbb R^L, ηy(x)=P(y∣x)\eta_y(x)=P(y\mid x), and BER(f)=1L∑yPx∣y(y∉arg⁡max⁡y′fy′(x))\mathrm{BER}(f)=\frac1L\sum_yP_{x\mid y}\big(y\notin\arg\max_{y'}f_{y'}(x)\big). Post-hoc logit adjustment predicts arg⁡max⁡y fy(x)−τlog⁡πy\arg\max_y\,f_y(x)-\tau\log\pi_y for a standard model. The logit-adjusted loss trains with ℓ(y,f(x))=−log⁡efy(x)+τlog⁡πy∑y′efy′(x)+τlog⁡πy′=log⁡[1+∑y′≠y(πy′πy)τefy′(x)−fy(x)]\ell(y,f(x))=-\log\frac{e^{f_y(x)+\tau\log\pi_y}}{\sum_{y'}e^{f_{y'}(x)+\tau\log\pi_{y'}}}=\log\Big[1+\sum_{y'\ne y}\Big(\frac{\pi_{y'}}{\pi_y}\Big)^\tau e^{f_{y'}(x)-f_y(x)}\Big] and predicts arg⁡max⁡yfy(x)\arg\max_yf_y(x). It sits inside the pairwise margin family ℓ=αylog⁡[1+∑y′≠yeΔyy′efy′−fy]\ell=\alpha_y\log[1+\sum_{y'\ne y}e^{\Delta_{yy'}}e^{f_{y'}-f_y}], which also contains the balanced loss (αy=1/πy\alpha_y=1/\pi_y), the adaptive margin loss of Cao et al. (LDAM, Δyy′=πy−1/4\Delta_{yy'}=\pi_y^{-1/4}) and the equalization loss of Tan et al. (Δyy′=log⁡F(πy′)\Delta_{yy'}=\log F(\pi_{y'})).

Theorem (consistency). For any δ∈R>0L\delta\in\mathbb R^L_{>0}, the pairwise margin loss with αy=δy/πy\alpha_y=\delta_y/\pi_y and Δyy′=log⁡(δy′/δy)\Delta_{yy'}=\log(\delta_{y'}/\delta_y) is Fisher consistent for the balanced error; δ=π\delta=\pi is the logit-adjusted loss with τ=1\tau=1, and δ=1\delta=1 the balanced loss. Lemma (binary, if and only if). With temperature γ\gamma, ℓ(+1,f)=ω+1γlog⁡(1+eγδ+1e−γf)\ell(+1,f)=\frac{\omega_{+1}}\gamma\log(1+e^{\gamma\delta_{+1}}e^{-\gamma f}) and ℓ(−1,f)=ω−1γlog⁡(1+eγδ−1eγf)\ell(-1,f)=\frac{\omega_{-1}}\gamma\log(1+e^{\gamma\delta_{-1}}e^{\gamma f}) are consistent for the balanced error exactly when ω+1 σ(γδ+1)ω−1 σ(γδ−1)=1−ππ,σ(z)=11+e−z.\frac{\omega_{+1}\,\sigma(\gamma\delta_{+1})}{\omega_{-1}\,\sigma(\gamma\delta_{-1})}=\frac{1-\pi}\pi,\qquad\sigma(z)=\frac1{1+e^{-z}}. Neither LDAM nor the equalization loss meets the condition, and post-hoc weight normalization is not consistent either.

Step 1

The Bayes rule for the balanced error ignores the prior. Averaging the per-class errors with equal weights is the ordinary error under Pbal(x,y)=1LP(x∣y)P_{\mathrm{bal}}(x,y)=\frac1LP(x\mid y), whose Bayes classifier is arg⁡max⁡yP(x∣y)=arg⁡max⁡yηy(x)πy=arg⁡max⁡y[sy∗(x)−log⁡πy]\arg\max_yP(x\mid y)=\arg\max_y\frac{\eta_y(x)}{\pi_y}=\arg\max_y\big[s^*_y(x)-\log\pi_y\big] when ηy∝esy∗\eta_y\propto e^{s^*_y}. This decision does not move when the priors change with P(x∣y)P(x\mid y) fixed, which makes the balanced error the natural target under label shift. Two procedures follow: shift the logits of an ordinary model afterwards, or train a model whose logits already estimate log⁡Pbal(y∣x)\log P_{\mathrm{bal}}(y\mid x). In the binary case the rule predicts positive when η(x)>π\eta(x)>\pi, Elkan's threshold p∗p^* with a false negative costing (1−π)/π(1-\pi)/\pi times a false positive (my remark; the paper leaves general costs to future work), and shifting the log-odds by −logit⁡p∗-\operatorname{logit}p^* handles any cost ratio.

Step 2

Post-hoc adjustment is additive, which is why it beats weight normalization. With τ=1\tau=1, subtracting log⁡πy\log\pi_y is the Bayes rule applied to the model's own probability estimates, and since arg⁡max⁡y[fy/τ−log⁡πy]=arg⁡max⁡y[fy−τlog⁡πy]\arg\max_y[f_y/\tau-\log\pi_y]=\arg\max_y[f_y-\tau\log\pi_y], any other τ\tau is the same rule after temperature scaling, the paper's reason for tuning τ\tau on a miscalibrated net. Weight normalization predicts arg⁡max⁡yfy(x)/νyτ\arg\max_yf_y(x)/\nu_y^\tau, a multiplicative rule: when a rare class has a negative logit while some other class is positive, shrinking νy\nu_y only pushes it further down, so the rare class can never win. The common choice νy=∥wy∥2\nu_y=\|w_y\|_2 also assumes the classifier norms track the priors, which on long-tailed CIFAR holds under SGD with momentum and fails under Adam at comparable balanced error, so the method rests on an accident of the optimizer's implicit bias.

Step 3

The logit-adjusted loss learns the balanced posterior, by two changes of variable. A margin Δyy′=log⁡(δy′/δy)\Delta_{yy'}=\log(\delta_{y'}/\delta_y) is the same as adding log⁡δy\log\delta_y to logit yy inside the softmax, so the population minimizer satisfies fy+log⁡δy=log⁡ηy+C(x)f_y+\log\delta_y=\log\eta_y+C(x). A class weight αy\alpha_y turns the risk into unweighted cross-entropy on a distribution with posterior ηˉy∝ηyαy\bar\eta_y\propto\eta_y\alpha_y. Together, fy∗(x)=log⁡ηy(x) αyδy+C(x)=log⁡ηy(x)πy+C(x)when αy=δyπy,f^*_y(x)=\log\frac{\eta_y(x)\,\alpha_y}{\delta_y}+C(x)=\log\frac{\eta_y(x)}{\pi_y}+C(x)\quad\text{when }\alpha_y=\frac{\delta_y}{\pi_y}, whose argmax is the Bayes rule of Step 1. Only the ratio αy/δy\alpha_y/\delta_y matters, so a change in a weight can be offset one for one by a change in the margin, and a margin that depends on the positive label alone (LDAM) or on the negative alone (equalization) cannot produce the factor 1/πy1/\pi_y. The loss is post-hoc adjustment of the scorer f+τlog⁡πf+\tau\log\pi, so the two routes coincide for convex models and differ for nets only through the local minimum reached, and applying both at once double-counts the prior.

Step 4

In the binary case, consistency is one equation at f=0f=0. The binary pairwise loss is proper composite, so its Bayes score is f∗=Ψ(η)f^*=\Psi(\eta) for an invertible link Ψ\Psi, and consistency means Ψ−1(0)=π\Psi^{-1}(0)=\pi. The ratio of the two loss derivatives fixes the link (Reid and Williamson), and at f=0f=0 it gives the lemma's condition. Pure weighting ω±1∝1/π±1\omega_{\pm1}\propto1/\pi_{\pm1} satisfies it at every γ\gamma. A pure antisymmetric margin needs δ+1=−δ−1=γ−1log⁡1−ππ\delta_{+1}=-\delta_{-1}=\gamma^{-1}\log\frac{1-\pi}\pi, so it must shrink with the temperature, and in the hinge limit γ→∞\gamma\to\infty fixed positive margins drop out and only the weights decide, the cost-sensitive SVM result. Weights and margins each sized for the full correction correct twice: in a check of mine at π=0.05\pi=0.05 and γ=1\gamma=1, the Bayes score at η=π\eta=\pi is 00 for either single correction, +2.94+2.94 for both and −2.94-2.94 for plain logistic loss.

Step 5

In experiments only the consistent methods find the Bayes separator, and on long-tailed benchmarks the gains are a few points. On two Gaussians with 5% positives and an affine classifier, the logit-adjusted loss lands close to the balanced-error Bayes separator, while plain ERM, LDAM and the equalization loss end up away from it; post-hoc adjustment reaches the Bayes error near τ=1\tau=1, and weight normalization at any τ\tau makes things worse. On CIFAR-10-LT, CIFAR-100-LT, ImageNet-LT and iNaturalist, balanced error is 27.16, 61.64, 53.11 and 38.66 for ERM, 22.60, 58.24, 49.66 and 33.98 for post-hoc adjustment at τ=1\tau=1 and 22.33, 56.11, 48.89 and 33.64 for the logit-adjusted loss, with the gain in the rare classes. The method is not uniformly best (weight normalization at τ=1\tau=1 slightly beats both untuned variants on one CIFAR-100 profile, though post-hoc adjustment with τ>1\tau>1 overtakes it), and adding the inconsistent LDAM margin on top gives the best iNaturalist number, so consistency does not settle the ranking at finite samples.

Consequences and limits. Imbalance is a decision-rule problem. Reweighting is consistent in the population but does little once a flexible model separates the training data (Byrd and Lipton). For a binary fraud score, post-hoc adjustment is the cost threshold written in log-odds, so the threshold should come from the actual costs p∗p^*, with the base rate as one special case. A model trained with the adjusted loss outputs the balanced log-odds logit⁡η−logit⁡π\operatorname{logit}\eta-\operatorname{logit}\pi, the log likelihood ratio, so a downstream rule that reads the score as a probability must add a prior's logit back: the training prior gives training-distribution probabilities and the population prior gives population probabilities directly, even when negatives were subsampled for training (my remark). Post-hoc adjustment also presumes calibrated estimates of η\eta, so Platt, isotonic or temperature scaling comes first. Fisher consistency is a population statement about the minimizer: Kini et al. (2021) show, for overparameterized linear classifiers trained past zero training error, that additive logit offsets help early in training but lose their effect at convergence, while multiplicative logit scalings shape the max-margin solution, the regime Soudry et al. describe. The paper takes the priors as known training frequencies and targets the balanced error only, and its gains are a few points on vision data with one optimizer.

Terms

Balanced error
1L∑yPx∣y(error)\frac1L\sum_yP_{x\mid y}(\text{error}): ordinary error under a uniform class prior.
Balanced posterior
Pbal(y∣x)∝P(y∣x)/πy∝P(x∣y)P_{\mathrm{bal}}(y\mid x)\propto P(y\mid x)/\pi_y\propto P(x\mid y); its argmax is the Bayes rule for the balanced error.
Post-hoc logit adjustment
Predict arg⁡max⁡yfy(x)−τlog⁡πy\arg\max_yf_y(x)-\tau\log\pi_y; τ=1\tau=1 is the plug-in Bayes rule, other τ\tau act as a temperature.
Logit-adjusted loss
Softmax cross-entropy on fy+τlog⁡πyf_y+\tau\log\pi_y; at the optimum fy=log⁡(ηy/πy)+C(x)f_y=\log(\eta_y/\pi_y)+C(x).
Pairwise margin loss
αylog⁡[1+∑y′≠yeΔyy′efy′−fy]\alpha_y\log[1+\sum_{y'\ne y}e^{\Delta_{yy'}}e^{f_{y'}-f_y}]: weights α\alpha and margins Δ\Delta together.
Consistency condition
αy=δy/πy\alpha_y=\delta_y/\pi_y, Δyy′=log⁡(δy′/δy)\Delta_{yy'}=\log(\delta_{y'}/\delta_y) for any δ>0\delta>0; only αy/δy\alpha_y/\delta_y matters.
Weight normalization
Predict arg⁡max⁡yfy/νyτ\arg\max_yf_y/\nu_y^\tau; multiplicative, so not consistent, and νy=∥wy∥\nu_y=\|w_y\| depends on the optimizer.
Binary rule
Predict positive iff η(x)>π\eta(x)>\pi: Elkan's p∗p^* with cFN/cFP=(1−π)/πc_{FN}/c_{FP}=(1-\pi)/\pi.
Binary lemma
Consistent iff ω+1σ(γδ+1)/(ω−1σ(γδ−1))=(1−π)/π\omega_{+1}\sigma(\gamma\delta_{+1})/(\omega_{-1}\sigma(\gamma\delta_{-1}))=(1-\pi)/\pi, standard sigmoid.
Recovering probabilities
An adjusted-loss model's score is the log likelihood ratio; adding logit⁡π\operatorname{logit}\pi of a prior gives the log-odds of P(y=1∣x)P(y=1\mid x) under that prior.

Quiz

  1. Why is it a mistake to apply post-hoc logit adjustment to a model trained with the logit-adjusted loss?
  2. Why does class reweighting by 1/πy1/\pi_y help little when a deep net fits the training data perfectly?
  3. Why does the optimizer matter for weight normalization with νy=∥wy∥2\nu_y=\|w_y\|_2?
  4. Why can post-hoc logit adjustment benefit from τ≠1\tau\ne1 on a deep net?

Sources