Sinkhorn Distances: Lightspeed Computation of Optimal Transportation Distances
Marco Cuturi
Introduction
Optimal transportation distances (Villani, 2009, §6) – also known as Earth Mover’s following the seminal work of Rubner et al. (1997) and their application to computer vision – hold a special place among other distances in the probability simplex. Compared to other classic distances or divergences, such as Hellinger, , Kullback-Leibler or Total Variation, they are the only ones to be parameterized. This parameter – the ground metric – plays an important role to handle high-dimensional histograms: the ground metric provides a natural way to handle redundant features that are bound to appear in high-dimensional histograms (think synonyms for bags-of-words), in the same way that Mahalanobis distances can correct for statistical correlations between vector coordinates.
The central role played by histograms and bags-of-features in most data analysis tasks and the good performance of optimal transportation distances in practice has generated ample interest, both from a theoretical point of view (Levina and Bickel, 2001; Indyk and Thaper, 2003; Naor and Schechtman, 2007; Andoni et al., 2009) and a pracical aspect, mostly to compare images (Grauman and Darrell, 2004; Ling and Okada, 2007; Gudmundsson et al., 2007; Shirdhonkar and Jacobs, 2008). Optimal transportation distances have, however, a very clear drawback. No matter what the algorithm employed – network simplex or interior point methods – their cost scales at least in when computing the distance between a pair of histograms of dimension , in the general case where no restrictions are placed upon the ground metric parameter (Pele and Werman, 2009, §2.1). This speed can be improved by ensuring that the ground metric observes certain constraints and/or by accepting some approximation errors. However, when these restrictions do not apply, computing a single distance between a pair of histograms of dimension in the few hundreds can take more than a few seconds. This issue severely hinders the applicability of optimal transportation distances in large-scale data analysis and goes as far as putting into question their relevance within the field of machine learning.
Our aim in this paper is to show that the optimal transportation problem can be regularized by an entropic term, following the maximum-entropy principle. We argue that this regularization is intuitive given the geometry of the optimal transportation problem and has, in fact, been long known and favored in transportation theory (Erlander and Stewart, 1990). From an optimization point of view, this regularization has multiple virtues, among which that of turning this LP into a strictly convex problem that can be solved extremely quickly with the Sinkhorn-Knopp matrix scaling algorithm (Sinkhorn and Knopp, 1967; Knight, 2008). This algorithm exhibits linear convergence and can be trivially parallelized – it can be vectorized. It is therefore amenable to large scale executions on parallel platforms such as GPGPUs. From a practical perspective, we show that, on the benchmark task of classifying MNIST digits, Sinkhorn distances perform better than the EMD and can be computed several orders of magnitude faster over a large sample of dimensions without making any assumption on the ground metric. We believe this paper contains all the ingredients that are required for optimal transportation distances to be at last applied on high-dimensional datasets and attract again the attention of the machine learning community.
This paper is organized as follows: we provide reminders on optimal transportation theory in Section 2, introduce Sinkhorn distances in Section 3 and provide algorithmic details in Section 4. We follow with an empirical study in Section 5 before concluding.
Reminders on Optimal Transportation
where is the dimensional vector of ones. contains all nonnegative matrices with row and column sums and respectively. has a probabilistic interpretation: for and two multinomial random variables taking values in , each with distribution and respectively, the set contains all possible joint probabilities of . Indeed, any matrix can be identified with a joint probability for such that . Such joint probabilities are also known as contingency tables. We define the entropy and the Kullback-Leibler divergences of these tables and their marginals as
2. Optimal Transportation
Given a cost matrix , the cost of mapping to using a transportation matrix (or joint probability) can be quantified as . The following problem:
is called an optimal transportation problem between and given cost . An optimal table for this problem can be obtained with the network simplex (Ahuja et al., 1993, §9) as well as other approaches (Orlin, 1993). The optimum of this problem, , is a distance (Villani, 2009, §6.1) whenever the matrix is itself a metric matrix, namely whenever belongs to the cone of distance matrices (Avis, 1980; Brickell et al., 2008):
For a general matrix , the worst case complexity of computing that optimum with any of the algorithms known so far scales in and turns out to be super-cubic in practice as well (Pele and Werman, 2009, §2.1). Much faster speeds can be obtained however when placing all sorts of restrictions on and accepting approximated solutions, albeit at a cost in performance (Grauman and Darrell, 2004) and a loss in applicability.
Sinkhorn Distances
We consider in this section a family of optimal transportation distances whose feasible set is the not the whole of , but a parameterized restricted set of joint probability matrices.
We recall a basic information theoretic inequality (Cover and Thomas, 1991, §2) which applies to all joint probabilities:
This bound is tight, since the table – known as the independence table (Good, 1963) – has an entropy of . By the concavity of entropy, we can introduce the convex set as
These definitions are indeed equivalent, since one can easily check that
a quantity which is also the mutual information of two random variables should they follow the joint probability (Cover and Thomas, 1991, §2). Hence, all tables whose Kullback-Leibler divergence to the table is constrained to lie below a certain threshold can be interpreted as the set of tables in which have sufficient entropy with respect to and , or joint probabilities which display a small enough mutual information.
As a classic result of linear optimization, the optimum of classical optimal transportation distances is achieved on vertices of , that is matrices with only up to non-zero elements (Brualdi, 2006, §8.1.3). Such plans can be interpreted as quasi-deterministic joint probabilities, since if , then very few values will have a non-zero probability. By mitigating the transportation cost objective with an entropic constraint, which is equivalent to following the max-entropy principle (Jaynes, 1957; Dudík and Schapire, 2006) and thus for a given level of the cost look for the most smooth joint probability, we argue that we can provide a more robust notion of distance between histograms. Indeed, for a given pair , finding plausible transportation plans with low cost (where plausibility is measured by entropy) is more informative than finding extreme plans that are extremely unlikely to appear in nature.
We note that the idea of regularizing the transportation problem was also considered recently by Ferradans et al. (2013). In their work, Ferradans et al. also argue that an optimal matching may not be sufficiently regular in vision applications (color transfer), and that these undesirable properties can be handled through an adequate relaxation and penalization (through graph-based norms) of the transportation problem. While Ferradans et al. (2013) penalize the transportation problem to obtain a more regular transportation plan, we believe that an entropic regularization yields here a better distance. An illustration of this idea is provided in Figure 1. For reasons that will become clear in Section 4, we call such distances Sinkhorn distances.
2. Metric Properties
When is large enough, the Sinkhorn distance coincides with the classic optimal transportation distance. When , the Sinkhorn distance has a closed form and becomes a negative definite kernel if one assumes that is itself a negative definite distance, that is a Euclidean distance matrix.
For large enough, the Sinkhorn distance is the transportation distance .
Since for any is lower bounded by , we have that for large enough and thus both quantities coincide.
The proof is provided in the appendix. Beyond these two extreme cases, the main theorem of this section states that Sinkhorn distances are symmetric and satisfy triangle inequalities for all possible values of . Since for small enough for any such that , Sinkhorn distances cannot satisfy the coincidence axiomsatisfied if holds for all . However, multiplying by suffices to recover the coincidence property if needed.
For all and , is symmetric and satisfies all triangle inequalities. The function satisfies all three distance axioms.
The gluing lemma (Villani, 2003, Lemma 7.6) plays a crucial role to prove that optimal transportation distances are indeed distances. The version we use below is slightly different since it incorporates the entropic constraint.
Let and be three elements of . Let and be two joint probabilities in the transportation polytopes of and with sufficient entropy. Let be the matrix whose ’s coefficient is . Then .
The proof is provided in the appendix. We can prove the triangle inequality for by using the same proof strategy than that used for classical transportation distances.
Proof of Theorem 1. The symmetry of is a direct result of ’s symmetry. Let be three elements in . Let and be the optimal solutions obtained when computing and respectively. Using the matrix of provided in Lemma 1, we proceed with the following chain of inequalities:
Computing Sinkhorn Distances with the Sinkhorn-Knopp Algorithm
Recall that the Sinkhorn distance (Definition 1) is defined through a hard constraint on the entropy of relative to and . In what follows, we consider the same program with a Lagrange multiplier for the entropy constraint,
By duality theory we have that for every pair , to each corresponds an such that . We call the dual-Sinkhorn divergence and show that it can be computed at a much cheaper cost than the classical optimal transportation problem for reasonable values of .
When , the solution is unique by strict convexity of minus the entropy. In fact, is necessarily of the form , where and are two non-negative vectors uniquely defined up to a multiplicative factor.
This well known fact in transportation theory (Erlander and Stewart, 1990) can be indeed checked by forming the Lagrangian of the objective of Equation (2) using for each of the two equality constraints in . For these two cost vectors ,
We obtain then, for any couple , that if , then
and thus recover the form provided above. is thus, by Sinkhorn and Knopp’s theorem (1967), the only matrix with row-sum and column-sum of the form
Given and marginals and , it is thus sufficient to run enough iterations of Sinkhorn and Knopp’s algorithm to converge to a solution of that problem. We provide a one line implementation in Algorithm 1. The case where some coordinates of or are null can be easily handled by selecting those elements of that are strictly positive to obtain the desired table, as shown in the first line of Algorithm 1. Note that Algorithm 1 is vectorized: it can be used as such to compute the distance between and a family of histograms by replacing with . These linear algebra operations can be very quickly executed by using a GPGPU.
With a naive approach, can be obtained by computing iteratively until the entropy of the solution has reached an adequate value . Since the entropy of decreases monotonically when increases, this search can be carried out by simple bisection, starting with a small which is iteratively increased. In what follows, we only consider the dual-Sinkhorn divergence since it is cheaper to compute and displays good performances in itself. We believe that more clever approaches can be applied to calculate exactly , and we leave this for future work. In the rest of this paper we will now refer to as the Sinkhorn distance, despite the fact that it is not provably a distance.
Experimental Results
We test the performance of Sinkhorn distances on the MNIST digitshttp://yann.lecun.com/exdb/mnist/ dataset, on which the ground metric has a natural interpretation in terms of pixel distances. Each digit is provided as a vector of intensities on a pixel grid. We convert each image into a histogram by normalizing each pixel intensity by the total sum of all intensities . We consider a subset of points in the training set of the database, where ranges within datapoints.
For each subset of size , we provide mean and standard deviation of classification error using a 4 fold (3 test, 1 train) cross validation scheme repeated 6 times, resulting in 24 different experiments. We study the performance of different distances with the following parameter selection scheme: for each distance , we consider the kernel , where is chosen by cross validation individually for each training fold within the set , where is the quantile of a subset of distances observed in the training fold. We regularize non-positive definite kernel matrices resulting from this computation by adding a sufficiently large diagonal term. SVM’s were run with libsvm (one-vs-one) for multiclass classification, the regularization constant being selected by 2 folds/2 repeats cross-validation on the training fold in the set
1.2. Distances
The Hellinger, , Total Variation and squared Euclidean (Gaussian kernel) distances are used as such. We set the ground metric to be the Euclidean distance between the points in the grid, resulting in a distance matrix. We also tried to use Mahalanobis distances on this example with a positive definite matrix equal to exp(-tM.^2), t>0, as well as its inverse, with varying values of but none of the results proved competitive. For the Independence kernel, since any Euclidean distance matrix is valid, we consider where and choose by cross-validation on the training set. Smaller values of seem to be preferable. We select the entropic penalty of Sinkhorn distances so that the matrix is relatively diagonally dominant and the resulting transportation not too far from the classic optimal transportation. We select for each training fold by internal cross-validation within where is the median distance between pixels on the grid. We set the number of fixed-point iterations to an arbitrary number of 20 iterations. In most (though not all) folds, the value comes up as the best setting. The Sinkhorn distance beats by a safe margin all other distances, including the EMD.
2. Does the Sinkhorn Distance Converge to the EMD?
We study in this section the convergence of Sinkhorn distances towards classical optimal transportation distances as gets bigger. Because of the additional penalty that appears in (2) program, is necessarily larger than , and we expect this gap to decrease as increases. Figure 3 illustrates this by plotting the boxplot of distributions of over pairs of distinct points taken in the MNIST database. As can be observed, even with large values of , Sinkhorn distances hover above the values of EMD distances by about . For practical values of such as selected above we do not expect the Sinkhorn distance to be numerically close to the EMD, nor believe it to be a desirable property.
3. Several Orders of Magnitude Faster
We measure in this section the computational speed of classic optimal transportation distances vs. that of Sinkhorn distances using Rubner et al.’s (1997)http://robotics.stanford.edu/ rubner/emd/default.htm and Pele and Werman’s (2009)http://www.cs.huji.ac.il/ ofirpele/FastEMD/code/, we use emd_hat_gd_metric in these experiments publicly available implementations. We generate points uniformly in the -simplex (Smith and Tromble, 2004) and generate random distance matrices by selecting points distributed with a spherical Gaussian in dimension to obtain enough variability in the distance matrix. is then divided by the median of its values, M=M/median(M(:)). Sinkhorn distances are implemented in matlab code (see Algorithm 1) while emd_mex, emd_hat_gd_metric are mex/C files. The emd distances and Sinkhorn CPU are run on a matlab session with a single working core (2.66 Ghz Xeon). Sinkhorn GPU is run on an NVidia Quadro K5000 card. Following the experimental findings of Section 5.1, we consider two parameters for , and . results in a relatively dense matrix , with results comparable to that of the Independence kernel, while results in a matrix with mostly negligible values and therefore a matrix with low entropy that is closer to the optimal transportation solution. Rubner et al.’s implementation cannot be run for histograms larger than . For large dimensions and on the same CPU, Sinkhorn distances are more than 100.000 faster than EMD solvers given a threshold of . Using a GPU results in a speed-up of a supplementary order of magnitude.
4. Empirical Complexity
Conclusion
We have shown that regularizing the optimal transportation problem with an intuitive entropic penalty opens the door for new research directions and potential applications at the intersection of optimal transportation theory and machine learning. This regularization guarantees speed-ups that are effective whatever the structure of the ground metric . Based on preliminary evidence, it seems that Sinkhorn distances do not perform worse than the EMD, and may in fact perform better in applications. Sinkhorn distances are parameterized by a regularization weight which should be tuned having both computational and performance objectives in mind, but we have not observed a need to establish a trade-off between both. Indeed, reasonably small values of seem to perform better than large ones.
Appendix: Proofs
where and . We used the fact that to go from the first to the second equality. is thus a n.d. kernel because it is the sum of two n.d. kernels: the first term is the sum of the same function evaluated separately on and , and thus a negative definite kernel (Berg et al., 1984, §3.2.10); the latter term is negative definite as minus a positive definite kernel (Berg et al., 1984, Definition §3.1.1).
Remark. The proof above suggests a faster way to compute the Independence kernel. Given a matrix , one can indeed pre-compute the vector of norms as well as a Cholesky factor of above to preprocess a dataset of histograms by premultiplying each observations by and only store as well as precomputing its diagonal term . Note that the independence kernel is positive definite on histograms with the same 1-norm, but is no longer positive definite for arbitrary vectors.
Let be the a probability distribution on whose coefficients are defined as
for all indices such that . For indices such that , all values are set to .
Let . is a transportation matrix between and . Indeed,
We now prove that . Let be three random variables jointly distributed as . Since by definition of in Equation (4)
the triplet is a Markov chain (Cover and Thomas, 1991, Equation 2.118) and thus, by virtue of the data processing inequality (Cover and Thomas, 1991, Theorem 2.8.1), the following inequality between mutual informations applies: