A margin-based replacement for cross-entropy loss

Michael W. Spratling, Heiko H. Schütt · OpenReview

Abstract: Cross-entropy (CE) loss is the de-facto standard for training deep neural networks to perform classification. However, CE-trained deep neural networks struggle with robustness and generalisation issues. To alleviate these issues, we propose high error margin (HEM) loss, a variant of multi-class margin loss that overcomes the training issues of other margin-based losses. We evaluate HEM extensively on a range of architectures and datasets. We find that HEM loss is more effective than cross-entropy loss across a wide range of tasks: unknown class rejection, adversarial robustness, learning with imbalanced data, continual learning, and semantic segmentation (a pixel-level classification task). Despite all training hyper-parameters being chosen for CE loss, HEM is inferior to CE only in terms of clean accuracy and this difference is insignificant. We also compare HEM to specialised losses that have previously been proposed to improve performance on specific tasks. LogitNorm, a loss achieving state-of-the-art performance on unknown class rejection, produces similar performance to HEM for this task, but is much poorer for continual learning and semantic segmentation. Logit-adjusted loss, designed for imbalanced data, has superior results to HEM for that task, but performs more poorly on unknown class rejection and semantic segmentation. DICE, a popular loss for semantic segmentation, is inferior to HEM loss on all tasks, including semantic segmentation. Thus, HEM often out-performs specialised losses, and in contrast to them, is a general-purpose replacement for CE loss.

Introduction

Deep neural networks are almost universally trained using Cross-Entropy (CE) loss. However, CE suffers from what can be termed a "confidence addiction." Because the loss only reaches zero when the correct class logit is infinitely larger than the others, the network never stops attempting to increase its confidence. This drive leads to three major issues: overconfidence in out-of-distribution (OOD) scenarios, catastrophic forgetting in continual learning, and a general lack of robustness against adversarial attacks and corruptions.

While specialized losses exist for specific problems—such as LogitNorm for OOD detection or DICE for segmentation—they often lack generalizability. This research introduces High Error Margin (HEM) loss, a general-purpose replacement for CE that improves robustness and learning stability without sacrificing competitive performance.

The HEM Loss Framework

HEM loss builds upon the foundation of Multi-class Margin (MM) loss (or hinge loss). In standard MM loss, the error for a sample is zero if the correct class logit exceeds the next best logit by at least a margin $\mu$. While theoretically sound, MM loss often fails in deep learning because the gradient signal becomes "diluted" as many samples reach zero error, causing training to stall.

HEM addresses this via two primary mechanisms:

  1. Selective Averaging: For each sample, HEM only considers errors that are above the mean error for that sample.
  2. Gradient Concentration: This ensures the optimizer focuses on the most "wrong" components of a prediction. As the network improves, the threshold naturally drops, maintaining a strong learning signal throughout training.

For imbalanced datasets, the authors propose HEM+, which scales the margin for each class based on its frequency (using an inverse square root of the sample count), effectively handling long-tailed distributions.

Experimental Evaluation

The researchers conducted an exhaustive evaluation across 18+ architectures (from LeNet to Vision Transformers) and numerous benchmarks, including standard classification (ImageNet-1k, CIFAR), robustness (CIFAR-C), continual learning (SplitMNIST), and semantic segmentation (Cityscapes).

Key Results

HEM loss demonstrates a remarkable ability to act as a "generalist" loss. As shown in the summary of results, HEM and its variant HEM+ consistently outperform CE and other specialized losses across nearly all metrics except for a negligible (~1%) trade-off in clean accuracy.

Performance of HEM and other losses relative to Cross-Entropy across various tasks.
  • OOD Detection & Robustness: HEM significantly outperforms CE at rejecting unknown classes and resisting adversarial attacks. By allowing a "dead zone" where the loss is zero once a sample is "correct enough," HEM prevents the over-optimization that makes networks brittle.
  • Continual Learning: HEM drastically reduces catastrophic forgetting. Because it stops updating weights for well-learned samples, it preserves prior knowledge far better than the confidence-driven CE loss.
  • Semantic Segmentation: HEM+ outperformed the popular DICE loss on all tested benchmarks, proving that margin-based approaches are highly effective for pixel-level classification.

Visualizing the Confidence Advantage

The core advantage of HEM in OOD detection is its ability to separate the confidence distributions of known versus unknown data. While CE loss pushes all samples toward maximum confidence (MSP = 1.0), HEM creates a more distinct separation, allowing for more reliable rejection of unknown classes.

Comparison of Maximum Softmax Probability (MSP) distributions for CE, LN, MM, and HEM losses.

Conclusion and Future Work

HEM loss presents a viable, general-purpose alternative to Cross-Entropy. It matches or exceeds specialized state-of-the-art losses on their own tasks while maintaining broad applicability. Future work may focus on closing the minor clean accuracy gap by optimizing hyperparameters specifically for HEM and exploring computational efficiencies by skipping backward passes for samples that have already reached the zero-loss margin.

Summary updated

Abstract

Cross-entropy (CE) loss is the de-facto standard for training deep neural networks to perform classification. However, CE-trained deep neural networks struggle with robustness and generalisation issues. To alleviate these issues, we propose high error margin (HEM) loss, a variant of multi-class margin loss that overcomes the training issues of other margin-based losses. We evaluate HEM extensively on a range of architectures and datasets. We find that HEM loss is more effective than cross-entropy loss across a wide range of tasks: unknown class rejection, adversarial robustness, learning with imbalanced data, continual learning, and semantic segmentation (a pixel-level classification task). Despite all training hyper-parameters being chosen for CE loss, HEM is inferior to CE only in terms of clean accuracy and this difference is insignificant. We also compare HEM to specialised losses that have previously been proposed to improve performance on specific tasks. LogitNorm, a loss achieving state-of-the-art performance on unknown class rejection, produces similar performance to HEM for this task, but is much poorer for continual learning and semantic segmentation. Logit-adjusted loss, designed for imbalanced data, has superior results to HEM for that task, but performs more poorly on unknown class rejection and semantic segmentation. DICE, a popular loss for semantic segmentation, is inferior to HEM loss on all tasks, including semantic segmentation. Thus, HEM often out-performs specialised losses, and in contrast to them, is a general-purpose replacement for CE loss.
BibTeX
@misc{spratling2025margin,
  author = {Michael W. Spratling and Heiko H. Schütt},
  title = {{A margin-based replacement for cross-entropy loss}},
  year = {2025},
  eprint = {2501.12191},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2501.12191},
}
Figure