Differentiable Top-k Classification Learning

Felix Petersen, Hilde Kuehne, Christian Borgelt, Oliver Deussen

Introduction

In traditional classification tasks, models are typically trained to maximize top-1 accuracy using softmax cross-entropy. However, in many real-world applications—especially those involving thousands of classes like ImageNet-21K—the "correct" label can be ambiguous. In these cases, it is often more valuable for a model to include the correct answer within its top 5 or 10 predictions.

While top-$k$ accuracy is a standard evaluation metric, it is notoriously difficult to use directly as a training objective because the "rank" operation is discrete and non-differentiable (its gradient is zero almost everywhere). Previous attempts to create "smooth" top-$k$ losses often struggled to outperform standard top-1 cross-entropy in clean, non-noisy settings. This paper introduces a novel family of differentiable top-$k$ cross-entropy losses that treat $k$ as a random variable drawn from a probability distribution rather than a fixed integer. By leveraging advances in differentiable sorting and ranking, this flexible objective not only improves top-$k$ metrics but also consistently boosts top-1 accuracy and model robustness.

Methodology

The core innovation of this work is a differentiable top-$k$ loss where $k$ is relaxed from a fixed integer to a probability distribution, $P_K$.

Overview of the proposed architecture: A CNN predicts scores, which are then ranked by a differentiable algorithm to produce a probability distribution for the cross-entropy loss.
  1. Probabilistic $k$: Instead of choosing a static $k=5$, users can specify a distribution (e.g., 50% weight on top-1 and 50% on top-5). This allows the model to learn from multiple "ranks" simultaneously.
  2. Differentiable Ranking: To make the loss differentiable, the authors utilize "relaxed" permutation matrices, evaluating methods like NeuralSort, SoftSort, and SinkhornSort. These produce a matrix where rows represent ranks and columns represent classes.
  3. Splitter Selection Networks (SSN): The authors propose a new, more efficient class of selection networks. Traditional sorting networks have a depth of $O(\log^2 n)$. SSNs reduce this to $O(\log n)$, significantly lowering computational overhead and reducing approximation errors introduced by the differentiable relaxation.
  4. Subset Ranking: To handle large-scale datasets with over 10,000 classes, the expensive differentiable ranking is only performed on a subset of the top-$m$ scores (e.g., $m=50$), ensuring the ground truth is always included during training.

Key Insights and Results

The authors evaluated their method across major benchmarks, including CIFAR-100, ImageNet-1K, and ImageNet-21K-P.

The "Top-1 Boost" and the Sweet Spot Surprisingly, training with a mixture of top-1 and top-$k$ objectives often yields better top-1 accuracy than training on top-1 alone, suggesting this relaxation makes the model more robust. However, as illustrated below, moving to a pure top-5 loss ($\alpha=1.0$) causes top-1 accuracy to plummet because the model loses the incentive to place the correct class in the absolute first slot. The optimal approach is a balanced distribution.

Effects of varying the ratio between top-1 and top-5 (left) and the size of the ranked subset m (right).

State-of-the-Art Performance By fine-tuning a Noisy Student EfficientNet-L2, the authors achieved 88.37% top-1 and 98.68% top-5 accuracy on ImageNet-1K, setting a new record for publicly available models at the time of publication. The method also demonstrated consistent gains across the ResNeXt-101 WSL series and proved its scalability on ImageNet-21K, achieving improvements of over 1% with only last-layer fine-tuning.

Limitations and Future Work

While highly optimized, differentiable ranking remains computationally heavier than standard softmax. The authors mitigated this with the subset ranking trick, but it remains a factor for very large subset sizes ($m$). Additionally, the "temperature" parameter of the differentiable operators and the choice of the distribution $P_K$ require careful hyperparameter tuning. Finally, while successful on CIFAR-100 from scratch, the massive computational cost meant the authors primarily focused on fine-tuning for larger ImageNet models. Exploring these losses throughout the entire training regime of massive models remains an open direction for future research.

Summary updated

BibTeX
@inproceedings{petersen2022differentiable,
  author = {Felix Petersen and Hilde Kuehne and Christian Borgelt and Oliver Deussen},
  title = {{Differentiable Top-k Classification Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2022},
  url = {https://openreview.net/forum?id=ATUK7eKdJLY},
}
Figure