Fast Differentiable Sorting and Ranking
Mathieu Blondel, Olivier Teboul, Quentin Berthet, Josip Djolonga
Introduction
Modern deep learning architectures are built by composing parameterized functional blocks (including loops and conditionals) and are trained end-to-end using gradient backpropagation. This has motivated the term differentiable programming, recently popularized, among others, by LeCun (2018). Despite great empirical successes, many operations commonly used in computer programming remain poorly differentiable or downright pathological, limiting the set of architectures for which a gradient can be computed.
We focus in this paper on two such operations: sorting and ranking. Sorting returns the given input vector with its values re-arranged in monotonic order. It plays a key role to handle outliers in robust statistics, as in least-quantile (Rousseeuw, 1984) or trimmed (Rousseeuw & Leroy, 2005) regression. As a piecewise linear function, however, the sorted vector contains many kinks where it is non-differentiable. In addition, when used in composition with other functions, sorting often induces non-convexity, thus rendering model parameter optimization difficult.
In this paper, we propose the first differentiable sorting and ranking operators with time and memory complexity. Our proposals enjoy exact computation and differentiation (i.e., they do not involve differentiating through the iterates of an approximate algorithm). We achieve this feat by casting differentiable sorting and ranking as projections onto the permutahedron, the convex hull of all permutations, and using a reduction to isotonic optimization. While the permutahedron had been used for learning before (Yasutake et al., 2011; Ailon et al., 2016; Blondel, 2019), it had not been used to define fast differentiable operators. The rest of the paper is organized as follows.
We review the necessary background (§2) and show how to cast sorting and ranking as linear programs over the permutahedron, the convex hull of all permutations (§3).
We introduce regularization in these linear programs, which turns them into projections onto the permutahedron and allows us to define differentiable sorting and ranking operators. We analyze the properties of these operators, such as their asymptotic behavior (§4).
Using a reduction to isotonic optimization, we achieve computation and differentiation of our operators, a key technical contribution of this paper (§5).
We show that our approach is an order of magnitude faster than existing approaches and showcase two novel applications: differentiable Spearman’s rank coefficient and soft least trimmed squares (§6).
Preliminaries
We define the argsort of as the indices sorting , i.e.,
where . If some of the coordinates of are equal, we break ties arbitrarily. We define the sort of as the values of in descending order, i.e.,
We define the rank of as the function evaluating at coordinate to the position of in the descending sort (smaller rank means that has higher value). It is formally equal to the argsort’s inverse permutation, i.e.,
For instance, if , then , and . All three operations can be computed in time. Note that throughout this paper, we use descending order for convenience. The ascending order counterparts are easily obtained by , and , respectively.
Sorting and ranking as linear programs
We show in this section how to cast sorting and ranking operations as linear programs over the permutahedron. To that end, we first formulate the argsort and ranking operations as optimization problems over the set of permutations . {lemma}Discrete optimization formulations
A well-known object in combinatorics (Bowman, 1972; Ziegler, 2012), the permutahedron of is a convex polytope, whose vertices correspond to permutations of . It is illustrated in Figure 1. In particular, when , . With this defined, we can now derive linear programming formulations of sort and ranks. {proposition}Linear programming formulations
A proof is provided in §B.2. The key idea is to perform a change of variable to “absorb” the permutation in (4) and (5) into a permutahedron. From the fundamental theorem of linear programming (Dantzig et al., 1955, Theorem 6), an optimal solution of a linear program is almost surely achieved at a vertex of the convex polytope, a permutation in the case of the permutahedron. Interestingly, appears in the constraints and appears in the objective for sorting, while this is the opposite for ranking.
For , the fact that appears in the linear program constraints makes piecewise linear and thus differentiable almost everywhere. When is unique at , is differentiable at and its Jacobian is the permutation matrix associated with . When is not unique, we can choose any matrix in Clarke’s generalized Jacobian, i.e., any convex combination of the permutation matrices associated with .
Lack of useful Jacobian of ranking.
On the other hand, for , since appears in the objective, a small perturbation to may cause the solution of the linear program to jump to another permutation of . This makes a discontinuous, piecewise constant function. This means that has null or undefined partial derivatives, preventing its use within a neural network trained with backpropagation.
Differentiable sorting and ranking
As we have already motivated, our primary goal is the design of efficiently computable approximations to the sorting and ranking operators, that would smoothen the numerous kinks of the former, and provide useful derivatives for the latter. We achieve this by introducing strongly convex regularization in our linear programming formulations. This turns them into efficiently computable projection operators, which are differentiable and amenable to formal analysis.
i.e., the Euclidean projection of onto . We also consider entropic regularization , popularized in the optimal transport literature (Cuturi, 2013; Peyré & Cuturi, 2017). Subtly, we define
More generally, we can use any strongly convex regularization under mild conditions. For concreteness, we focus our exposition in the main text on . We state all our propositions for these two cases and postpone a more general treatment to the appendix.
Soft operators.
We now build upon these projections to define soft sorting and ranking operators. To control the regularization strength, we introduce a parameter which we multiply by (equivalently, divide by).
For sorting, we choose and therefore define the -regularized soft sort as
For ranking, we choose and therefore define the -regularized soft rank as
We illustrate the behavior of both of these soft operations as we vary in Figures 2 and 3. As for the hard versions, the ascending-order soft sorting and ranking are obtained by negating the input as and , respectively.
Properties.
We can further characterize these approximations. Namely, as we now formalize, they are differentiable a.e., and not only converge to the their “hard” counterparts, but also satisfy some of their properties for all . {proposition}Properties of and
Differentiability. For all , and are differentiable (a.e.) w.r.t. .
where , .
The last property describes the behavior as and . Together with the proof of Section 4, we include in §B.3 a slightly stronger result. Namely, we derive an explicit value of below which our operators are exactly equal to their hard counterpart, and a value of above which our operators can be computed in closed form.
Convexification effect.
Proposition 4 shows that and for all converge to convex functions of as . This suggests that larger make the objective function increasingly easy to optimize (at the cost of departing from “hard” sorting or ranking). This behavior is also visible in Figure 3, where converges towards the mean , depicted by a straight line.
On tuning ε𝜀\varepsilon (or not).
The parameter controls the trade-off between approximation of the original operator and “smoothness”. When the model producing the scores or “logits” to be sorted/ranked is a homogeneous function, from (12) and (13), can be absorbed into the model. In our label ranking experiment, we find that indeed tuning is not necessary to achieve excellent accuracy. On the other hand, for top- classification, we find that applying a logistic map to squash to and tuning is important, confirming the empirical finding of Cuturi et al. (2019).
Relation to linear assignment formulation.
Similarly, we can rewrite (7) as . To obtain a differentiable operator, Cuturi et al. (2019) (see also (Adams & Zemel, 2011)) propose to replace the permutation matrix by a doubly stochastic matrix , which is computed approximately in using Sinkhorn (1967). In comparison, our approach is based on regularizing with directly, the key to achieve time and space complexity, as we now show.
Fast computation and differentiation
As shown in the previous section, computing our soft sorting and ranking operators boils down to projecting onto a permutahedron. Our key contribution in this section is the derivation of an forward pass and an backward pass (multiplication with the Jacobian) for these projections. Beyond soft sorting and ranking, this is an important sensitivity analysis question in its own right.
We now show how to reduce the projections to isotonic optimization, i.e., with simple chain constraints, which is the key to fast computation and differentiation. We will w.l.o.g. assume that is sorted in descending order (if not the case, we sort it first). {proposition}Reduction to isotonic optimization
The function is classically known as isotonic regression. The fact that it can be used to solve the Euclidean projection onto has been noted several times (Negrinho & Martins, 2014; Zeng & Figueiredo, 2015). The reduction of Bregman projections, which we use here, to isotonic optimization was shown by Lim & Wright (2016). Unlike that study, we use the KL projection of onto , and not of onto , which simplifies many expressions. We include in §B.4 a simple unified proof of Section 5 based on Fenchel duality and tools from submodular optimization. We also discuss an interpretation of adding regularization to the primal linear program as relaxing the equality constraints of the dual linear program in §B.5.
Computation.
Hence, PAV returns an exact solution of both and in time (Best et al., 2000). This means that we do not need to choose a number of iterations or a level of precision, unlike with Sinkhorn. Since computing and requires obtaining beforehand, the total computational complexity is .
Differentiating isotonic optimization.
The block-wise structure of the solution also makes its derivatives easy to analyze, despite the fact that we are differentiating the solution of an optimization problem. Since the coordinates of the solution in block are all equal to , which in turn depends only on a subset of the parameters, the Jacobian has a simple block-wise form, which we now formalize. {lemma}Jacobian of isotonic optimization
Let be the ordered partition of induced by from Proposition 5. Then,
There are interesting differences between the two forms of regularization. For quadratic regularization, the Jacobian only depends on the partition (not on ) and the blocks have constant value. For entropic regularization, the Jacobian does depend on and the blocks are constant column by column. Both formulations are averaging the incoming gradients, one uniformly and the other weighted.
Differentiating the projections.
We now combine Section 5 with Section 5 to characterize the Jacobians of the projections onto the permutahedron and show how to multiply arbitrary vectors with them in linear time. {proposition}Jacobian of the projections
Let be defined in Proposition 5. Then,
where is the matrix obtained by permuting the rows and columns of according to , and where
Again, the Jacobian w.r.t. is entirely symmetric. Unlike the Jacobian of isotonic optimization, the Jacobian of the projection is not block diagonal, as we need to permute its rows and columns. We can nonetheless multiply with it in linear time by using the simple identity , which allows us to reuse the multiplication with the Jacobian of isotonic optimization.
With the Jacobian of w.r.t. and at hand, differentiating and boils down to a mere application of the chain rule to (12) and (13). To summarize, we can multiply with the Jacobians of our soft operators in time and space.
Experiments
We present in this section our empirical findings. NumPy, JAX, PyTorch and Tensorflow versions of our sorting and ranking operators are available at https://github.com/google-research/fast-soft-sort/.
OT (Cuturi et al., 2019): optimal transport formulation.
All-pairs (Qin et al., 2010): noting that is equivalent to , one can obtain soft ranks in by replacing the indicator function with a sigmoid.
Proposed: our soft ranks and . Although not used in this experiment, for top- ranking, the complexity can be reduced to by computing using the algorithm of Lim & Wright (2016).
We use the CIFAR-10 and CIFAR-100 datasets, with and classes, respectively. Following Cuturi et al. (2019), we use a vanilla CNN (4 Conv2D with 2 max-pooling layers, ReLU activation, 2 fully connected layers with batch norm on each), the ADAM optimizer (Kingma & Ba, 2014) with a constant step size of , and set . Similarly to Cuturi et al. (2019), we found that squashing the scores to with a logistic map was beneficial.
Results.
Our empirical results, averaged over runs, are shown in Figure 4 (left, center). On both CIFAR-10 and CIFAR-100, our soft rank formulations achieve comparable accuracy to the OT formulation, though significantly faster, as we elaborate below. Similarly to Cuturi et al. (2019), we found that the soft top- loss slightly outperforms the classical cross-entropy (logistic) loss for these two datasets. However, we did not find that the All-pairs formulation could outperform the cross-entropy loss.
The training times for 600 epochs on CIFAR-100 were 29 hours (OT), 21 hours (), 23 hours () and 16 hours (All-pairs). Training times on CIFAR-10 were similar. While our soft operators are several hours faster than OT, they are slower than All-pairs, despite its complexity. This is due the fact that, with , All-pairs is very efficient on GPUs, while our PAV implementation runs on CPU.
2 Runtime comparison: effect of input dimension
To measure the impact of the dimensionality on the runtime of each method, we designed the following experiment.
Results.
Run times for one batch computation with backpropagation disabled are shown in Figure 4 (Right). While their runtime is reasonable in small dimension, OT and All-pairs scale quadratically with respect to the dimensionality (note the log scale on the -axis). Although slower than a softmax, our formulations scale well, with the dimensionality having negligible impact on the runtime. OT and All-pairs go out-of-memory starting from and , respectively. With backpropagation enabled, they go out-of-memory at and , due to the need for recording the computational graph. This shows that the lack of memory available on GPUs is problematic for these methods. In contrast, our approaches only require memory and comes with the theoretical Jacobian (they do not rely on differentiating through iterates). They therefore suffer from no such issues.
3 Label ranking via soft Spearman’s rank correlation coefficient
We now consider the label ranking setting where supervision is given as full rankings (e.g., ) rather than as label relevance scores. The goal is therefore to learn to predict permutations, i.e., a function . A classical metric between ranks is Spearman’s rank correlation coefficient, defined as the Pearson correlation coefficient between the ranks. Maximizing this coefficient is equivalent to minimizing the squared loss between ranks. A naive idea would be therefore to use as loss , where . This is unfortunately a discontinuous function of . We therefore propose to rather use , hence the name differentiable Spearman’s rank correlation coefficient. At test time, we replace with , which is justified by the order-preservation property (Proposition 4).
We consider the datasets from (Hüllermeier et al., 2008; Cheng et al., 2009), which has both semi-synthetic data obtained from classification problems, and real biological measurements. Following (Korba et al., 2018), we average over two 10-fold validation runs, in each of which we train on 90% and evaluate on 10% of the data. Within each repetition, we run an internal 5-fold cross-validation to grid-search for the best parameters. We consider linear models of the form , and for ablation study we drop the soft ranking layer .
Results.
Due to the large number of datasets, we choose to present a summary of the results in Figure 5. We postpone detailed results to the appendix (Table 1). Out of 21 datasets, introducing a soft rank layer with works better on 15 datasets, similarly on 4 and worse on 2 datasets. We can thus conclude that even for such simple model, introducing our layer is beneficial, and even achieving state of the art results on some of the datasets (full details in the appendix).
4 Robust regression via soft least trimmed squares
The classical ridge regression can be cast as
To empirically validate our proposal, we compare cross-validated results for increasing percentage of outliers of the following methods:
Least trimmed squares, with truncation parameter ,
Soft least trimmed squares (26), with truncation parameter and regularization parameter ,
Ridge regression (25), with regularization parameter ,
Huber loss (Huber, 1964) with regularization parameter and threshold parameter , as implemented in scikit-learn (Pedregosa et al., 2011).
We consider datasets from the LIBSVM archive (Fan & Lin, 2011). We hold out 20% of the data as test set and use the rest as training set. We artifically create outliers, by adding noise to a certain percentage of the training labels, using , where . We do not add noise to the test set. For all methods, we use L-BFGS (Liu & Nocedal, 1989), with a maximum of iterations. For hyper-parameter optimization, we use -fold cross-validation. We choose from , from log-spaced values between and , and from linearly spaced values between and . We repeat this procedure times with a different train-test split, and report the averaged scores (a.k.a. coefficient of determination).
Results.
The averaged scores (higher is better) are shown in Figure 6.3. On all datasets, the accuracy of ridge regression deteriorated significantly with increasing number of outliers. Least trimmed squares (hard or soft) performed slightly worse than the Huber loss on housing, comparably on bodyfat and much better on cadata. We found that hard least trimmed squares (i.e., ) worked well on all datasets, showing that regularization is less important for sorting operators (which are piecewise linear) than for ranking operators (which are piecewise constant). Nevertheless, regularization appeared useful in some cases. For instance, on cadata, the cross-validation procedure picked when the percentage of outliers is less than , and when the percentage of outliers is larger than . This is confirmed visually on Figure 6.3, where the soft sort with works slightly better than the hard sort with few outliers, then performs comparably with more outliers. The interpolation effect enabled by therefore allows some adaptivity to the (unknown) percentage of outliers.
Conclusion
Building upon projections onto permutahedra, we constructed differentiable sorting and ranking operators. We derived exact computation and differentiation of these operators, a key technical contribution of this paper. We demonstrated that our operators can be used as a drop-in replacement for existing ones, with an order-of-magnitude speed-up. We also showcased two applications enabled by our soft operators: label ranking with differentiable Spearman’s rank correlation coefficient and robust regression via soft least trimmed squares.
Acknowledgements
We are grateful to Marco Cuturi and Jean-Philippe Vert for useful discussions, and to Carlos Riquelme for comments on a draft of this paper.
References
Appendix A Additional empirical results
We include in this section the detailed label ranking results on the same datasets as considered by Hüllermeier et al. (2008) as well as Cheng et al. (2009).
Spearman’s rank correlation coefficient for each method, averaged over runs, is shown in the table below.
Appendix B Proofs
and in particular for . The second claim follows from
B.2 Proof of Proposition 6 (Linear programming formulations)
where in the second equality we used and the fundamental theorem of linear programming (Dantzig et al., 1955, Theorem 6). For the second claim, we have similarly
Setting and using proves the claim.
B.3 Proof of Proposition 4 (Properties of soft sorting and ranking operators)
Let be a closed convex set and let . If is strongly convex over , then is Lipschitz continuous. By Rademacher’s theorem, is differentiable almost everywhere. Furthermore, since with , is differentiable a.e. as long as is twice differentiable, which is the case when .
Order preservation.
Proposition 1 of Blondel et al. (2019) shows that and are sorted the same way. Furthermore, since with and since is monotone, is sorted the same way as , as well. Let and . From the respective definitions, this means that is sorted the same way as (i.e., it is sorted in descending order) and is sorted the same way as , which concludes the proof.
Asymptotic behavior.
We will now characterize the behavior for sufficiently small and large regularization strength . Note that rather than multiplying the regularizer by , we instead divide by , which is equivalent. {lemma}Analytical solutions of isotonic optimization in the limit regimes
If , then
If , then
where .
We start with the case. Recall that is sorted in descending order. Therefore, since we chose sufficiently small, the vector is sorted in descending order as well. This means that is feasible, i.e., it belongs to the constraint sets in Section 5. Further, note that so that is the optimal solution if we drop the constraints, which completes the argument.
Next, we tackle the case. Note that the claimed solutions are exactly and , so the claim will immediately follow if we show that is an optimal partition. The PAV algorithm (cf. §B.6) merges at each iteration any two neighboring blocks that violate , starting from the partitions consisting of singleton sets. Let be the iteration number. We claim that the two blocks, and , will always be violating the constraint, so that they can be merged. Note that in the quadratic case, they can be merged only if
which is indeed satisfied when . In the KL case, they can be merged only if
This will be true if the term on the left-hand side is smaller than the term on the right-hand side, i.e., when , which again is implied by the assumption. ∎
We can now directly characterize the behavior of the projection operator in the two regimes and . This in turn implies the results for both the soft ranking and sorting operations using (12) and (13). {proposition}Analytical solutions of the projections in the limit regimes
If , then
If , then
Therefore, in these two regimes, we do not even need PAV to compute the optimal projection.
B.4 Proof of Proposition 5 (Reduction to isotonic optimization)
Before proving Proposition 5, we need the following three lemmas.
Note that and . This means that we can express and as a convex combination of the endpoints of the line segment , namely
Solving for and gives . From the convexity of , we therefore have
Dual formulation of a regularized linear program
Let , where is strongly convex and is convex. We have
which is the infimal convolution of with . Moreover, . The results follows from choosing and noting that . ∎
For instance, with , we have , and with , we have .
The next lemma shows how to go further by choosing as the base polytope associated with a cardinality-based submodular function , of which the permutahedron is a special case. The polytope is defined as (see, e.g., Bach (2013))
Reducing dual formulation to isotonic regression
The support function is known as the Lovász extension of . For conciseness, we use the standard notation . Applying Lemma B.4, we obtain
Moreover, .
Let us fix to the permutation that sorts . Following the same idea as from (Djolonga & Krause, 2017), since the Lovász extension is linear on the set of all vectors that are sorted by , we can write
This is an instance of isotonic optimization, as we can rewrite the problem as
with .
Let . It remains to show that , i.e., that and the optimal dual variables are both in descending order. Suppose for some . Let be a copy of with and swapped. Since is convex, by Lemma B.4,
which contradicts the assumption that and the corresponding are optimal. A similar result is proven by Suehiro et al. (2012, Lemma 1) but for the optimal primal variable . ∎
We now prove Proposition 5. The permutahedron is a special case of with and . In that case, .
For , we thus have . Finally, note that if is Legendre-type, which is the case of both and , then . Therefore, , which concludes the proof.
B.5 Relaxed dual linear program interpretation
We show in this section that the dual problem in Lemma B.4 can be interpreted as the original dual linear program (LP) with relaxed equality constraints. Consider the primal LP
As shown by Bach (2013, Proposition 3.2), the dual LP is
Moreover, let be a permutation sorting in descending order. Then, an optimal is given by (Bach, 2013, Proposition 3.2)
Now let us restrict to the support of and do the change of variable
The non-negativity constraints in become and the equality constraints in become . Adding quadratic regularization in the primal problem (55) is equivalent to relaxing the dual equality constraints in (56) by smooth constraints (this can be seen by adding quadratic regularization to the primal variables of Bach (2013, Eq. (3.6))). For the dual objective (56), we have
where in the second line we used (Bach, 2013, Eq. (3.2)). Altogether, we obtain , which is exactly the expression we derived in Lemma B.4. The entropic case is similar.
B.6 Pool adjacent violators (PAV) algorithm
Let be convex functions. As shown in (Best et al., 2000; Lim & Wright, 2016),
can be solved using a generalization of the PAV algorithm (note that unlike these works, we use decreasing constraints for convenience). All we need is a routine for solving, given some set of indices, the “pooling” sub-problem
Thus, we can use PAV to solve (53), as long as is separable. We now give the closed-form solution for two special cases. To simplify, we denote and .
We have . We therefore minimize
Entropic regularization.
We have . We therefore minimize
where .
Although not explored in this work, other regularizations are potentially possible, see, e.g., (Blondel et al., 2019).
B.7 Proof of Proposition 5 (Jacobian of isotonic optimization)
Let be the partition of induced by . From the PAV algorithm, for all , there is a unique block such that and . Therefore, for all , we obtain
Therefore, the Jacobian matrix is block diagonal, i.e.,
The multiplication with the Jacobian uses the fact that each block is constant column-wise.
The expression above is for points where is differentiable. For points where is not differentiable, we can take an arbitrary matrix in the set of Clarke’s generalized Jacobians, the convex hull of Jacobians of the form . The points of non-differentiability occur when a block of the optimal solution can be split up into two blocks with equal values. In that case, the two directional derivatives do not agree, but are derived for quadratic regularization by Djolonga & Krause (2017).