Interpolating between Optimal Transport and MMD using Sinkhorn Divergences
Jean Feydy, Thibault Séjourné, François-Xavier Vialard, Shun-ichi Amari, Alain Trouvé, Gabriel Peyré
Introduction
The two main classes of losses which avoid these shortcomings are Optimal Transport distances and Maximum Mean Discrepancies: they are continuous with respect to the convergence in law and metrize its topology. That is, . The main purpose of this paper is to study the theoretical properties of a new class of geometric divergences which interpolates between these two families and thus offers an extra degree of freedom through a parameter that can be cross-validated in typical learning scenarios.
1 Previous works
Out of this collection of methods, entropic regularization has recently emerged as a computationally efficient way of approximating OT costs. For , we define
MMD norms.
If is universal (Micchelli et al.,, 2006) (i.e. if the linear space spanned by functions is dense in ) we know that metrizes the convergence in law. Such Euclidean norms, introduced for shape matching in (Glaunes et al.,, 2004), are often referred to as “Maximum Mean Discrepancies” (MMD) (Gretton et al.,, 2007). They have been extensively used for generative model (GANs) fitting in machine learning (Li et al.,, 2015; Dziugaite et al.,, 2015). MMD norms are cheaper to compute than OT and have a smaller sample complexity – i.e. approximation error when sampling a distribution.
2 Interpolating between OT and MMD using Sinkhorn divergences
Sinkhorn divergences. On the one hand, OT losses have appealing geometric properties; on the other hand, cheap MMD norms scales up to large batches with a low sample complexity. Why not interpolate between them to get the best of both worlds?
Following (Genevay et al.,, 2018) (see also (Ramdas et al.,, 2017; Salimans et al.,, 2018; Sanjabi et al.,, 2018)) we consider a new cost built from that we call a Sinkhorn divergence:
Such a formula satisfies and interpolates between OT and MMD (Ramdas et al.,, 2017):
In the literature, the formula (3) has been introduced more or less empirically to fix the entropic bias present in the cost: with a structure that mimicks that of a squared kernel norm (2), it was assumed or conjectured that would define a positive definite loss function, suitable for applications in ML. This paper is all about proving that this is indeed what happens.
3 Contributions
The purpose of this paper is to show that the Sinkhorn divergences are convex, smooth, positive definite loss functions that metrize the convergence in law. Our main result is the theorem below, that ensures that one can indeed use as a reliable loss function for ML applications – whichever value of we pick.
Let be a compact metric space with a Lipschitz cost function that induces, for , a positive universal kernel . Then, defines a symmetric positive definite, smooth loss function that is convex in each of its input variables. It also metrizes the convergence in law: for all probability Radon measures and ,
This theorem legitimizes the use of the unbiased Sinkhorn divergences instead of in model-fitting applications. Indeed, computing is roughly as expensive as (the computation of the corrective factors being cheap, as detailed in Section 3) and the “debiasing” formula (3) allows us to guarantee that the unique minimizer of is the target distribution (see Figure 1). Section 3 details how to implement these divergences efficiently: our algorithms scale up to millions of samples thanks to freely available GPU routines. To conclude, we showcase in Section 4 the typical behavior of compared with and standard MMD losses.
Proof of Theorem 1
We now give the proof of Theorem 1. Our argument relies on a new Bregman divergence derived from a weak∗ continuous entropy that we call the Sinkhorn entropy (see Section 2.2). We believe this (convex) entropy function to be of independent interest. Note that all this section is written under the assumptions of Theorem 1; the proof of some intermediate results can be found in the appendix.
First, let us recall some standard results of regularized OT theory (Peyré and Cuturi,, 2017). Thanks to the Fenchel-Rockafellar theorem, we can rewrite Cuturi’s loss (1) as
where is the tensor sum . The primal-dual relationship linking an optimal transport plan solving (1) to an optimal dual pair that solves (8) is
Crucially, the first order optimality conditions for the dual variables are equivalent to the primal’s marginal constraints on (9). They read
where the “Sinkhorn mapping” is defined through
with a SoftMin operator of strength defined through
The following proposition recalls some important properties of and the associated dual potentials. Its proof can be found in Section B.1.
The following proposition, whose proof is detailed in Section B.2, shows that the dual potentials are the gradients of .
is weak* continuous and differentiable. Its gradient reads
where satisfies and on the whole domain and T is the Sinkhorn mapping (11).
Let us stress that even though the solutions of the dual problem (8) are defined -a.e., the gradient (15) is defined on the whole domain . Fortunately, an optimal dual pair defined -a.e. satisfies the optimality condition (10) and can be extended in a canonical way: to compute the “gradient” pair associated to a pair of measures , using and is enough.
2 Sinkhorn and Haussdorf divergences
Crucially, we now assume that is a positive universal kernel on the space of signed Radon measures.
Under the assumptions above, we define the Sinkhorn negentropy of a probability Radon measure through
The following proposition is the cornerstone of our approach to prove the positivity of , providing an alternative expression of . Its proof relies on a change of variables in (8) that is detailed in the Section B.3 of the appendix.
The following proposition, whose proof can be found in the Section B.4 of the appendix, leverages the alternative expression (18) to ensure the convexity of .
Under the same hypotheses as Proposition 3, is a strictly convex functional on .
We now define an auxiliary “Hausdorff” divergence that can be interpreted as an loss with decoupled dual potentials.
Thanks to Proposition 2, the Sinkhorn negentropy is differentiable in the sense of (14). For any probability measures and regularization strength , we can thus define
It is the symmetric Bregman divergence induced by the strictly convex functional (Bregman,, 1967) and is therefore a positive definite quantity.
3 Proof of the Theorem
We are now ready to conclude. First, remark that the dual expression (8) of as a maximization of linear forms ensures that is convex with respect to and with respect to (but not jointly convex if ). is thus convex with respect to both inputs and as a sum of the functions and – see Proposition 4.
Using (15) to get , and summing the above inequalities, we show that , which implies (5).
To prove (6), note that , which implies that since is a strictly convex functional.
Finally, we show that metrizes the convergence in law (7) in the Section B.5 of the appendix.
Computational scheme
We have shown that Sinkhorn divergences (3) are positive definite, convex loss functions on the space of probability measures. Let us now detail their implementation on modern hardware.
1 The Sinkhorn algorithm(s)
Proposition 1 is key to the modern theory of regularized Optimal Transport: it allows us to compute the cost – and thus the Sinkhorn divergence , thanks to (3) – using dual variables that have the same memory footprint as the input measures: solving (8) in our discrete setting, we only need to store the sampled values of the dual potentials and on the measures’ supports.
denotes a (stabilized) log-sum-exp reduction.
If is an optimal pair of dual vectors that satisfies Equations (20-21), we deduce from (13) that
But how can we solve this coupled system of equations given , , and as input data?
The Sinkhorn algorithm.
One simple answer: by enforcing (20) and (21) alternatively, updating the vectors and until convergence (Cuturi,, 2013). Starting from null potentials , this numerical scheme is nothing but a block-coordinate ascent on the dual problem (8). One step after another, we are enforcing null derivatives on the dual cost with respect to the ’s and the ’s.
Convergence.
The “Sinkhorn loop” converges quickly towards its unique optimal value: it enjoys a linear convergence rate (Peyré and Cuturi,, 2017) that can be improved with some heuristics (Thibault et al.,, 2017). When computed through the dual expression (23), and its gradients (26-27) are robust to small perturbations of the values of and : monitoring convergence through the norm of the updates on and breaking the loop as we reach a set tolerance level is thus a sensible stopping criterion. In practice, if is large enough – say, on the unit square with an Earth Mover’s cost – waiting for 10 or 20 iterations is more than enough.
All in all, the baseline Sinkhorn loop provides an efficient way of solving the discrete problem for generic input measures. But in the specific case of the (symmetric) corrective terms and introduced in (3), we can do better.
The key here is to remark that if , the dual problem (8) becomes a concave maximization problem that is symmetric with respect to its two variables and . Hence, there exists a (unique) optimal dual pair on the diagonal which is characterized in the discrete setting by the symmetric optimality condition:
Fortunately, given and , the optimal vector that solves this equation can be computed by iterating a well-conditioned fixed-point update:
This symmetric variant of the Sinkhorn algorithm can be shown to converge much faster than the standard loop applied to a pair on the diagonal, and three iterations are usually enough to compute accurately the optimal dual vector.
2 Computing the Sinkhorn divergence and its gradients
In this day and age, we could be tempted to rely on the automatic differentiation engines provided by modern libraries, which let us differentiate the result of twenty or so Sinkhorn iterations as a mere composition of elementary operations (Genevay et al.,, 2018). But beware: this loop has a lot more structure than a generic feed forward network. Taking advantage of it is key to a x2-x3 gain in performances, as we now describe.
Crucially, we must remember that the Sinkhorn loop is a fixed point iterative solver: at convergence, its solution satisfies an equation given by the implicit function theorem. Thanks to (15), using the very definition of gradients in the space of probability measures (14) and the intermediate variables in the computation of , we get that
Graph surgery with PyTorch.
Assuming convergence in the Sinkhorn loops, it is thus possible to compute the gradients of without having to backprop through the twenty or so iterations of the Sinkhorn algorithm: we only have to differentiate the expression above with respect to . But does it mean that we should differentiate C or the log-sum-exp operation by hand? Fortunately, no!
Modern libraries such as PyTorch (Paszke et al.,, 2017) are flexible enough to let us “hack” the naive autograd algorithm, and act as though the optimal dual vectors , , and did not depend on the input variables of . As documented in our reference code,
an appropriate use of the .detach() method in PyTorch is enough to get the best of both worlds: an automatic differentiation engine that computes our gradients using the formula at convergence instead of the baseline backpropagation algorithm. All in all, as evidenced by the benchmarks provided Figure 3, this trick allows us to divide by a factor 2-3 the time needed to compute a Sinkhorn divergence and its gradient with respect to the ’s.
3 Scaling up to large datasets
The Sinkhorn iterations rely on a single non-trivial operation: the log-sum-exp reduction (22). In the ML literature, this SoftMax operator is often understood as a row- or column-wise reduction that acts on matrices. But as we strive to implement the update rules (20-21) and (25) on the GPU, we can go further.
First, if the number of samples N and M in both measures is small enough, we can optimize the GPU usage by computing Sinkhorn divergences by batches of size B. In practice, this can be achieved by encoding the cost function C as a 3D tensor of size made up of stacked matrices , while and become and tensors, respectively. Thanks to the broadcasting syntax supported by modern libraries, we can then seamlessly compute, in parallel, loss values for in .
The KeOps library.
Unfortunately though, tensor-centric methods such as the one presented above cannot scale to measures sampled with large numbers N and M of Dirac atoms: as these numbers exceed 10,000, huge matrices stop fitting into GPU memories. To alleviate this problem, we leveraged the KeOps library (Charlier et al.,, 2018) that provides online map-reduce routines on the GPU with full PyTorch integration. Performing online log-sum-exp reductions with a running maximum, the KeOps primitives allow us to compute Sinkhorn divergences with a linear memory footprint. As evidenced by the benchmarks of Figures 3-3, computing the gradient of a Sinkhorn loss with 100,000 samples per measure is then a matter of seconds.
Numerical illustration
In the previous sections, we have provided theoretical guarantees on top of a comprehensive implementation guide for the family of Sinkhorn divergences . Let us now describe the geometry induced by these new loss functions on the space of probability measures.
To compare MMD losses with Cuturi’s original cost and the de-biased Sinkhorn divergence , a simple yet relevant experiment is to let a model distribution flow with time along the “Wasserstein-2” gradient flow of a loss functional that drives it towards a target distribution (Santambrogio,, 2015). This corresponds to the “non-parametric” version of the data fitting problem evoked in Section 1, where the parameter is nothing but the vector of positions that encodes the support of a measure . Understood as a “model free” idealization of fitting problems in machine learning, this experiment allows us to grasp the typical behavior of the loss function as we discover the deformations of the support that it favors.
with a Euler scheme and display the evolution of up to time .
Interpretation.
In both figures, the fourth line highlights the entropic bias that is present in the loss: is driven towards a minimizer that is a “shrunk” version of . As showed in Theorem 1, the de-biased loss does not suffer from this issue: just like MMD norms, it can be used as a reliable, positive-definite divergence.
Going further, the dynamics induced by the Sinkhorn divergence interpolates between that of an MMD () and Optimal Transport (), as shown in (4). Here, and we can indeed remark that the second and third lines bridge the gap between the flow of the energy distance (in the first line) and that of the Earth Mover’s cost which moves particles according to an optimal transport plan.
Please note that in both experiments, the gradient of the energy distance with respect to the ’s vanishes at the extreme points of ’s support. Crucially, for small enough values of , recovers the translation-aware geometry of OT and we observe a clean convergence of to as no sample lags behind.
Conclusion
Recently introduced in the ML literature, the Sinkhorn divergences were designed to interpolate between MMD and OT. We have now shown that they also come with a bunch of desirable properties: positivity, convexity, metrization of the convergence in law and scalability to large datasets.
To the best of our knowledge, it is the first time that a loss derived from the theory of entropic Optimal Transport is shown to stand on such a firm ground. As the foundations of this theory are progressively being settled, we now hope that researchers will be free to focus on one of the major open problems in the field: the interaction of geometric loss functions with concrete machine learning models.
Appendix A Standard results
Before detailing our proofs, we first recall some well-known results regarding the Kullback-Leibler divergence and the SoftMin operator defined in (12).
It can be rewritten as an -divergence associated to
Dual formulation.
Lower bound on the sup. If is not absolutely continuous with respect to , there exists a Borel set such that and . Consequently, for ,
Going further, the density of continuous functions in the space of bounded measurable functions allows us to restrict the optimization domain:
Let be a simple Borel function on , and let us choose some error margin . Since and are Radon measures, for any in the finite set of indices , there exists a compact set and an open set such that and
Moreover, for any , there exists a continuous function such that . The continuous function is then such that
We can then show that the Kullback-Leibler divergence is weakly lower semi-continuous:
If and are weakly converging sequences in , we get
According to (31), the KL divergence is defined as a pointwise supremum of weakly continuous applications
A.2 SoftMin Operator
Under the assumptions of the definition (12), we get that
If and are two continuous functions in such that ,
Let be a sequence of probability measures converging weakly towards , and be a sequence of continuous functions that converges uniformly towards . Then, for , the SoftMin of the values of on converges towards the SoftMin of the values of on , i.e.
Appendix B Proofs
The existence of an optimal pair of potentials that reaches the maximal value of the dual objective is proved using the contractance of the Sinkhorn map T, defined in (11), for the Hilbert projective metric (Franklin and Lorenz,, 1989).
While optimal potentials are only defined -a.e., as highlighted in Proposition 1, they are extended to the whole domain by imposing, similarly to the classical theory of OT (Santambrogio,, 2015, Remark 1.13), that they satisfy
with defined in (11). We thus assume in the following that this condition holds. The following propositions studies the uniqueness and the smoothness (with respect to the spacial position and with respect to the input measures) of these functions defined on the whole space.
For , let us define , and
the value of the dual objective between the two optimal pairs. As is a concave function bounded above by , it is constant with respect to . Hence, for all in $$,
This is only possible if, -a.e. in ,
As we extend the potentials through (34), the SoftMin operator commutes with the addition of (33) and lets our result hold on the whole feature space. ∎
According to (34), is a SoftMin combination of -Lipschitz functions of the variable ; using the algebraic properties of the SoftMin operator detailed in (32-33), one can thus show that is a -Lipschitz function on the feature space. The same argument holds for . ∎
Let and be weakly converging sequences of measures in . Given some arbitrary anchor point , let us denote by the (unique) sequence of optimal potentials for such that .
Then, and converge uniformly towards the unique pair of optimal potentials for such that . Up to the value at the anchor point , we thus have that
Being equicontinuous and uniformly bounded on the compact set , the sequence satisfies the hypotheses of the Ascoli-Arzela theorem: there exists a subsequence that converges uniformly towards a pair of continuous functions. tend to infinity, we see that and, using the continuity of the SoftMin operator (Proposition 10) on the optimality equations (10), we show that is an optimal pair for .
Now, according to Proposition 11, such a limit pair of optimal potentials is unique. is thus a compact sequence with a single possible adherence value: it has to converge, uniformly, towards . ∎
B.2 Proof of Proposition 2
The proof is mainly inspired from (Santambrogio,, 2015, Proposition 7.17). Let us consider , , , and times in a neighborhood of , as in the statement above. We define , and the variation ratio given by
Using the very definition of and the continuity property of Proposition 13, we now provide lower and upper bounds on as goes to .
As written in (13), can be computed through a straightforward, continuous expression that does not depend on the value of the optimal dual potentials at the anchor point :
Combining this equation with Proposition 13 (that guarantees the uniform convergence of potentials for weakly converging sequences of probability measures) allows us to conclude.
Lower bound.
First, let us remark that is a suboptimal pair of dual potentials for . Hence,
since and satisfy the optimality equations (10).
Upper bound.
Conversely, let us denote by the optimal pair of potentials for satisfying for some arbitrary anchor point . As are suboptimal potentials for , we get that
Conclusion.
Thanks to Proposition 13, we thus know that and converge uniformly towards and . Combining the lower and upper bound, we get
since and both have an overall mass that sums up to zero.
B.3 Proof of Proposition 3
The definition of is that
Thanks to the symmetry of this concave problem with respect to the variables and , we know that there exists a pair of optimal potentials on the diagonal, and
where denotes the smoothing (convolution) operator defined through
for and .
Optimizing on measures.
keeping in mind that is a probability measure, we then get that
where we optimize on positive measures such that and .
Expansion of the problem.
As is positive for all and in , we can remove the constraint from the optimization problem:
Existence of the optimal measure μ𝜇\mu.
In the expression above, the existence of an optimal is given as a consequence of the well-known fact from OT theory that optimal dual potentials and exist, so that the dual OT problem (8) is a max and not a mere supremum. Nevertheless, since this property of is key to the metrization of the convergence in law by Sinkhorn divergences, let us endow it with a direct, alternate proof:
For any , assuming that is compact, there exists a unique such that
Moreover, .
Notice that for ,
Since C is bounded on the compact set and is a probability measure, we can already say that
Upper bound on the mass of μ𝜇\mu.
Since is compact and , there exists such that for all and in . We thus get
As we build a minimizing sequence for , we can thus assume that is uniformly bounded by some constant .
Weak continuity.
Crucially, the Banach-Alaoglu theorem asserts that
is weakly compact; we can thus extract a weakly converging subsequence from the minimizing sequence . Using Proposition 8 and the fact that is continuous on , we show that is a weakly lower semi-continuous function: realizes the minimum of and we get our existence result.
Uniqueness.
We assumed that our kernel is positive universal. The squared norm is thus a strictly convex functional and using Proposition 6, we can show that is strictly convex. This ensures that is uniquely defined. ∎
B.4 Proof of Proposition 4
Let us take a pair of measures in , and ; according to Proposition 14, there exists a pair of measures , in such that
which is enough to conclude. To show the strict inequality, let us remark that
B.5 Proof of the Metrization of the Convergence in Law
The regularized OT cost is weakly continuous, and the uniform convergence for dual potentials ensures that and are both continuous too. Paired with (6), this property guarantees the convergence towards of the Hausdorff and Sinkhorn divergences, as soon as .
Conversely, let us assume that (resp. ). Any weak limit of a subsequence is equal to : since our divergence is weakly continuous, we have (resp. ), and positive definiteness holds through (6).
In the meantime, since is compact, the set of probability Radon measures is sequentially compact for the weak- topology. is thus a compact sequence with a unique adherence value: it converges, towards .