Notes

Class-Balanced Loss

When one class is rare—fraud transactions, a disease case, a customer who churns—a model can look “accurate” while quietly failing at the only cases you truly care about. Class-balanced loss is a way to make the training objective pay more attention to those rare examples.

What it is and how it works

A loss function turns prediction mistakes into a number the model tries to minimize. In imbalanced classification, the majority class contributes many more loss terms simply because it appears more often, so its gradients dominate learning. Class-balanced loss reweights each example’s loss so that minority-class mistakes count more. A common approach uses weights based on the effective number of samples per class (from Cui et al., 2019): instead of treating 10,000 near-duplicate majority examples as 10,000 equally “new” pieces of information, it discounts redundancy and boosts underrepresented classes. This is typically applied on top of standard losses like cross-entropy (and can be combined with focal loss to emphasize hard examples).

Where you see it in practice

  • Fraud detection: missing a fraud case is costly; class-balanced loss increases the penalty for false negatives on the rare fraud class.
  • Medical screening: the positive class is rare; balancing helps the model learn features that separate true positives from “normal” cases.
  • Churn prediction: churners are fewer; reweighting prevents the model from defaulting to “no churn.”

Why it matters (and what can go wrong)

It directly changes what the optimizer prioritizes, often improving recall and PR-AUC for the minority class without resampling. If weights are too aggressive, you can overfit the minority class or inflate false positives, so evaluation should focus on imbalance-aware metrics. In libraries, you’ll encounter this idea as class weights (e.g., scikit-learn’s class_weight or PyTorch’s weighted cross-entropy), with class-balanced variants implemented in many deep learning codebases.

Class-Balanced Loss is a supervised-learning objective that reweights per-example or per-class loss terms so each class contributes comparably to the total training signal, despite skewed class frequencies. Weights are commonly set inversely to class counts or to an “effective number” of samples. It matters because standard losses can be dominated by majority classes, yielding high overall accuracy but poor minority-class recall and miscalibrated decision boundaries.

Imagine a teacher grading a class where 95 students did the homework and 5 didn’t. If the teacher only cares about getting the “majority” right, they might barely pay attention to those 5—but that would be unfair if those cases matter.

Class-Balanced Loss is a way to make an AI model “care” more evenly about different groups in the training data when one group is much rarer than another. In tasks like fraud detection or rare disease screening, the rare cases are often the most important. This loss setting gives extra weight to mistakes on the rare class, so the model doesn’t learn to ignore it just to look accurate.