This NeurIPS 2019 paper from Google Research addresses a fundamental "missing link" in deep learning: how to backpropagate through sorting and ranking operations.
The Challenge of Sorting and Ranking in Deep Learning
In the world of deep learning, gradients are king. However, sorting and ranking are notoriously "gradient-unfriendly."
- Sorting ($S$): The vector of sorted values is piecewise linear. Its gradient is constant within regions but jumps at boundaries, making it difficult for optimizers to navigate.
- Ranking ($R$): The vector of ranks (e.g., "this is the 3rd largest element") is even worse—it is integer-valued and piecewise constant. Its derivative is zero almost everywhere, providing no useful signal for backpropagation.
Because of this, researchers usually avoid using ranks in training losses, opting instead for proxies like cross-entropy. This paper aims to bridge that gap, allowing models to be trained directly on the metrics we actually care about, such as "being in the top 5" or "minimizing the median error."
Methodology: Optimal Transport to the Rescue
The core innovation is re-imagining sorting through the lens of Optimal Transport (OT).
- Sorting as Assignment: Sorting $n$ numbers can be viewed as an optimal assignment problem. You are matching $n$ input values to $n$ "target" values that are already in increasing order (e.g., $1, 2, \dots, n$).
- Kantorovich Relaxation: The authors generalize this by allowing the target measure to have a different size ($m$) and different weights. This allows for "split" ranking, where an input's rank is a weighted average of target positions.
- Entropic Regularization (The Sinkhorn Step): To make the operation differentiable, they add an entropic penalty to the OT problem. This turns the "hard" assignment (a permutation matrix) into a "soft," dense transport plan. This plan is computed using the Sinkhorn algorithm, which consists of iterative row and column normalizations.
- Differentiability: Since the Sinkhorn algorithm involves only basic matrix operations, the entire process is differentiable. Gradients can flow through the iterations (algorithmic differentiation) or via the implicit function theorem.
- Numerical Stability: The authors implement the algorithm in the log-domain and use "squashing" functions (like arctan) to ensure the values stay within a stable range during training.
Key Insights and Applications
The authors evaluated their differentiable operators across several tasks, including image classification (CIFAR-10/100), neural sorting (MNIST), and robust regression (UCI datasets).
- The $n \neq m$ Flexibility: One of the most clever insights is that you don't need $n$ target values to compute a rank. By mapping $n$ inputs to just $m=3$ target values with specific weights, you can create a differentiable "soft quantile" operator (e.g., a differentiable median).
- Smoothing the Heaviside: The authors show that their Sinkhorn rank operator can be combined with a smoothed Heaviside function to create a continuous version of the 0/1 loss, bypassing the combinatorial complexity usually associated with top-k optimization.
- Robustness to Outliers: In quantile regression, the Sinkhorn-based "soft quantile" significantly reduced training error compared to a standard subgradient baseline, suggesting a smoother and more navigable optimization landscape.
- Superior Sorting: On the MNIST sorting task, the proposed method outperformed "NeuralSort" (a previous state-of-the-art differentiable sorter) in both exact and partial precision across various sequence lengths.
Limitations and the $\epsilon$ Trade-off
While powerful, the approach has limitations. The Sinkhorn algorithm is efficient ($O(nm)$), but it is still more computationally expensive than a standard $O(n \log n)$ sort.
Furthermore, the regularization strength $\epsilon$ acts as a critical temperature parameter. As shown in the visualizations above, if $\epsilon$ is too small, the gradients vanish (approaching the "hard" rank); if it's too large, the sorting information is lost as everything blurs toward the average. Finding the "sweet spot" is crucial for stable training.
Future work may explore non-Euclidean geometries (like KL-divergence or hyperbolic space) to better define the "distance" between ranks and values for specific data distributions.
Summary updated