Introduction
In computer science, sorting is a ubiquitous operation. However, in deep learning, the argsort operator—which returns the indices of a vector in sorted order—presents a significant hurdle. Because its output is a discrete permutation, its gradients are zero almost everywhere. This "dead end" prevents researchers from building end-to-end trainable models for tasks like learning-to-rank, top-k element selection, or differentiable k-Nearest Neighbors (kNN).
While "continuous relaxations" (soft versions) of sorting have been proposed before, they are often computationally expensive or mathematically opaque. To address this, researchers introduce SoftSort, a simple, differentiable replacement for the discrete argsort operator. SoftSort enables neural networks to learn tasks involving sorting and ranking via standard backpropagation, proving to be mathematically simpler and significantly faster than previous state-of-the-art methods like NeuralSort.
The SoftSort Method
The core idea of SoftSort is elegant: instead of trying to differentiate the sorting process itself, the authors define a "soft" permutation matrix based on distances.
- The Intuition: The $r$-th row of the SoftSort matrix represents a probability distribution over which input element is the $r$-th largest.
-
The Formula: SoftSort computes the distance between every input element $s_j$ and the $r$-th largest element (found using a standard, non-differentiable
sort). It then applies asoftmaxto the negative of these distances: $$SoftSort(s) = \text{softmax}\left(\frac{-d(\text{sort}(s)\mathbf{1}^T, \mathbf{1}s^T)}{\tau}\right)$$ where $\tau$ is a "temperature" parameter. As $\tau$ approaches zero, the operator converges to a hard permutation matrix. -
Implementation: Because it relies entirely on standard operations (sort, distance, softmax), it can be implemented in just three lines of code in frameworks like TensorFlow or PyTorch.
Key Insights
- The "Sort" Subroutine is Okay: A major insight is that you can use a non-differentiable
sortoperator inside your model. As long as the final output (the soft permutation matrix) is a differentiable function of the input scores, backpropagation works perfectly. - Unimodal Row Stochastic (URS) Matrices: Unlike some methods that produce "doubly stochastic" matrices (where both rows and columns sum to 1), SoftSort produces URS matrices. The authors argue this is actually better for sorting because it guarantees that each row has a unique "winner" (a clear argmax), making it trivial to extract a hard permutation at test time.
- Mathematical Simplicity: Previous methods like NeuralSort relied on complex identities involving the sums of top-k elements. SoftSort replaces this with a simple distance-to-target-rank approach, making proofs of convergence and unimodality much more straightforward.
Experimental Results
The authors evaluated SoftSort across several benchmarks to prove its versatility, including Large-MNIST sorting, Quantile Regression, and Image Classification (kNN) on datasets like MNIST, Fashion-MNIST, and CIFAR-10.
- Speed: SoftSort is significantly faster than NeuralSort. In TensorFlow, it is up to 6x faster for large input dimensions ($n=4000$). Even after the authors optimized the NeuralSort implementation, SoftSort remained 40-80% faster.
- Accuracy: On the Large-MNIST sorting task, SoftSort matched or slightly exceeded the performance of NeuralSort and optimal transport-based methods.
- Differentiable kNN: SoftSort achieved a 92.03% accuracy on CIFAR-10 using a kNN-based architecture, outperforming standard kNN baselines and showing that the operator can effectively learn useful data representations.
Limitations and Future Work
- Memory Complexity: Like most current soft-sorting methods, SoftSort constructs an $n \times n$ matrix. This leads to $O(n^2)$ memory usage, which becomes a bottleneck for very long sequences (e.g., sorting thousands of items). Exploring sparse versions of the operator is a key area for future work.
- Metric Selection: While the authors primarily used the $L_1$ (absolute difference) distance, they noted that the choice of distance metric $d$ matters. "Learning the metric" itself could be a promising research direction.
- Ties: The theoretical proofs assume no two input scores are identical. While ties are rare in continuous neural network outputs, the behavior of the operator in high-tie scenarios requires further exploration.
Summary updated