{'fraction_non_alphanumeric': 0.03203905036609718, 'fraction_numerical': 0.011360106500998448, 'mean_word_length': 4.796296296296297, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Wasserstein Variational Inference

This paper introduces Wasserstein variational inference, a new form of approximate Bayesian inference based on optimal transport theory. Wasserstein variational inference uses a new family of divergences that includes both f-divergences and the Wasserstein distance as special cases. The gradients of the Wasserstein variational loss are obtained by backpropagating through the Sinkhorn iterations. This technique results in a very stable likelihood-free training method that can be used with implicit distributions and probabilistic programs. Using the Wasserstein variational inference framework, we introduce several new forms of autoencoders and test their robustness and performance against existing variational autoencoding techniques.


Introduction
Variational Bayesian inference is gaining a central role in machine learning. Modern stochastic variational techniques can be easily implemented using differentiable programming frameworks [1][2][3]. As a consequence, complex Bayesian inference is becoming almost as user friendly as deep learning [4,5]. This is in sharp contrast with old-school variational methods that required modelspecific mathematical derivations and imposed strong constraints on the possible family of models and variational distributions. Given the rapidness of this transition it is not surprising that modern variational inference research is still influenced by some legacy effects from the days when analytical tractability was the main concern. One of the most salient examples of this is the central role of the (reverse) KL divergence [6,7]. While several other divergence measures have been suggested [8][9][10][11], the reverse KL divergence still dominates both research and applications. Recently, optimal transport divergences such as the Wasserstein distance [12,13] have gained substantial popularity in the generative modeling literature as they can be shown to be well-behaved in several situations where the KL divergence is either infinite or undefined [14][15][16][17]. For example, the distribution of natural images is thought to span a sub-manifold of the original pixel space [14]. In these situations Wasserstein distances are considered to be particularly appropriate because they can be used for fitting degenerate distributions that cannot be expressed in terms of densities [14].
In this paper we introduce the use of optimal transport methods in variational Bayesian inference. To this end, we define the new c-Wasserstein family of divergences, which includes both Wasserstein metrics and all f-divergences (which have both forward and reverse KL) as special cases. Using this family of divergences we introduce the new framework of Wasserstein variational inference, which exploits the celebrated Sinkhorn iterations [18,19] and automatic differentiation. Wasserstein variational inference provides a stable gradient-based black-box method for solving Bayesian inference problems even when the likelihood is intractable and the variational distribution is implicit [20,21]. Importantly, as opposed to most other implicit variational inference methods [20][21][22][23], our approach does not rely on potentially unstable adversarial training [24].

Background on joint-contrastive variational inference
We start by briefly reviewing the framework of joint-contrastive variational inference [22,20]. For notational convenience we will express distributions in terms of their densities. Note however that those densities could be degenerate. For example, the density of a discrete distribution can be expressed in terms of delta functions. The posterior distribution of the latent variable z given the observed data x is p(z|x) = p(z, x)/p(x). While the joint probability p(z, x) is usually tractable, the evaluation of p(x) often involves an intractable integral or summation. The central idea of variational Bayesian inference is to minimize a divergence functional between the intractable posterior p(z|x) and a tractable parametrized family of variational distributions. This form of variational inference is sometimes referred to as posterior-contrastive. Conversely, in joint-contrastive inference the divergence to minimize is defined between two structured joint distributions. For example, using the reverse KL we have the following cost functional: where q(x, z) = q(z|x)k(x) is the product between the variational posterior and the sampling distribution of the data. Usually k(x) is approximated as the re-sampling distribution of a finite training set, as in the case of variational autoencoders (VAE) [25]. The advantage of this jointcontrastive formulation is that it does not require the evaluation of the intractable distribution p(z|x). Joint-contrastive variational inference can be seen as a generalization of amortized inference [20].

Background on optimal transport
Intuitively speaking, optimal transport divergences quantify the distance between two probability distributions as the cost of transporting probability mass from one to the other. Let Γ[p, q] be the set of all bivariate probability measures on the product space X × X whose marginals are p and q respectively. An optimal transport divergence is defined by the following optimization: where c(x 1 , x 2 ) is the cost of transporting probability mass from x 1 to x 2 . When the cost is a metric function the resulting divergence is a proper distance and it is usually referred to as the Wasserstein distance. We will denote the Wasserstein distance as W (p, q).
The computation of the optimization problem in Eq. 2 suffers from a super-cubic complexity. Recent work showed that this complexity can be greatly reduced by adopting entropic regularization [19]. We begin by defining a new set of joint distributions: These distributions are characterized by having the mutual information between the two variables bounded by the regularization parameter −1 . Using this family of distributions we can define the entropy regularized optimal transport divergence: This regularization turns the optimal transport into a strictly convex problem. When p and q are discrete distributions the regularized optimal transport cost can be efficiently obtained using the Sinkhorn iterations [18,19]. The -regularized optimal transport divergence is then given by: where the function S t [p, q, c] gives the output of the t-th Sinkhorn iteration. The pseudocode of the Sinkhorn iterations is given in Algorithm 1. Note that all the operations in this algorithm are differentiable.

Wasserstein variational inference
We can now introduce the new framework of Wasserstein variational inference for general-purpose approximate Bayesian inference. We begin by introducing a new family of divergences that includes both optimal transport divergences and f-divergences as special cases. Subsequently, we develop a black-box and likelihood-free variational algorithm based on automatic differentiation through the Sinkhorn iterations.

c-Wasserstein divergences
Traditional divergence measures such as the KL divergence depend explicitly on the distributions p and q. Conversely, optimal transport divergences depend on p and q only through the constraints of an optimization problem. We will now introduce the family of c-Wasserstein divergences that generalize both forms of dependencies. A c-Wasserstein divergence has the following form: where the real-valued functional C p,q (x 1 , x 2 ) depends both on the two scalars x 1 and x 2 and on the two distributions p and q. Note that we are writing this dependency in terms of the densities only for notational convenience and that this dependency should be interpreted in terms of distributions. The cost functional C p,q (x 1 , x 2 ) is assumed to respect the following requirements: where supp(p) denotes the support of the distribution p. From these requirements we can derive the following theorem: Theorem 1. The functional W C (p, q) is a (pseudo-)divergence, meaning that W C (p, q) ≥ 0 for all p and q and W C (p, p) = 0 for all p.
Proof. From property 1 and property 2 it follows that, when p is equal to q, C p,p (x 1 , x 2 ) is a nonnegative function of x and y that vanishes when x = y. In this case, the optimization in Eq. 5 is optimized by the diagonal transport γ( In fact: This is a global minimum since property 3 implies that W C (p, q) is always non-negative.
All optimal transport divergences are part of the c-Wasserstein family, where C p,q (x, y) reduces to a non-negative valued function c(x 1 , x 2 ) independent from p and q.
Proving property 3 for an arbitrary cost functional can be a challenging task. The following theorem provides a criterion that is often easier to verify: Proof. The result follows directly from Jensen's inequality.

Stochastic Wasserstein variational inference
We can now introduce the general framework of Wasserstein variational inference. The loss functional is a c-Wasserstein divergence between p(x, z) and q(x, z): From Theorem 1 it follows that this variational loss is always minimized when p is equal to q. Note that we are allowing members of the c-Wasserstein divergence family to be pseudo-divergences, meaning that L C [p, q] could be 0 even if p = q. It is sometimes convenient to work with pseudodivergences when some features of the data are not deemed to be relevant.
We can now derive a black-box Monte Carlo estimate of the gradient of Eq. 7 that can be used together with gradient-based stochastic optimization methods [26]. A Monte Carlo estimator of Eq 7 can be obtained by computing the discrete c-Wasserstein divergence between two empirical distributions: where (x 2 ) are sampled from p(x, z) and q(x, z) respectively. In the case of the Wasserstein distance, we can show that this estimator is asymptotically unbiased: Theorem 3. Let W (p n , q n ) be the Wasserstein distance between two empirical distributions p n and q n . For n tending to infinity, there is a positive number s such that Proof. Using the triangle inequality and the linearity of the expectation we obtain: In [27] it was proven that for any distribution u: Unfortunately the Monte Carlo estimator is biased for finite values of n. In order to eliminate the bias when p is equal to q, we use the following modified loss: It is easy to see that the expectation of this new loss is zero when p is equal to q. Furthermore: As we discussed in Section 1.2, the entropy-regularized version of the optimal transport cost in Eq. 8 can be approximated by truncating the Sinkhorn iterations. Importantly, the Sinkhorn iterations are differentiable and consequently we can compute the gradient of the loss using automatic differentiation [16]. The approximated gradient of the -regularized loss can be written as where the function S t [p n , q n , C p,q ] is the output of t steps of the Sinkhorn algorithm with regularization and cost function C p,q . Note that the cost is a functional of p and q and consequently the gradient contains the term ∇C p,q . Also note that this approximation converges to the real gradient of Eq. 7 for n → ∞ and → 0 (however the Sinkhorn algorithm becomes unstable when → 0).

Examples of c-Wasserstein divergences
We will now introduce two classes of c-Wasserstein divergences that are suitable for deep Bayesian variational inference. Moreover, we will show that the KL divergence and all f-divergences are part of the c-Wasserstein family.

A metric divergence for latent spaces
In order to apply optimal transport divergences to a Bayesian variational problem we need to assign a metric, or more generally a transport cost, to the latent space of the Bayesian model. The geometry of the latent space should depend on the geometry of the observable space since differences in the latent space are only meaningful as far as they correspond to differences in the observables. The simplest way to assign a geometric transport cost to the latent space is to pull back a metric function from the observable space: where d x (x 1 , x 2 ) is a metric function in the observable space and g p (z) is a deterministic function that maps z to the expected value of p(x|z). In our notation the subscript p in g p denotes the fact that the distribution p(x|z) and the function g p depend on a common set of parameters which are optimized during variational inference. The resulting pullback cost function is a proper metric if g p is a diffeomorphism (i.e. a differentiable map with differentiable inverse) [28].

Autoencoder divergences
Another interesting special case of c-Wasserstein divergence can be obtained by considering the distribution of the residuals of an autoencoder. Consider the case where the expected value of q(z|x) is given by the deterministic function h q (x). We can define the latent autoencoder cost functional as the transport cost between the latent residuals of the two models: where d is a distance function. It is easy to check that this cost functional defines a proper c-Wasserstein divergence since it is non-negative valued and it is equal to zero when p is equal to q and x 1 , z 1 are equal to x 2 , z 2 . Similarly, we can define the observable autoencoder cost functional as follows: where again g p (z) gives the expected value of the generator. In the case of a deterministic generator, this expression reduces to C p OA (x 1 , z 1 ; x 2 , z 2 ) = d(0, x 2 − g p (z 2 )) . Note that the transport optimization is trivial in this special case since the cost does not depend on x 1 and z 1 . In this case, the resulting divergence is just the average reconstruction error: As expected, this is a proper (pseudo-)divergence since it is non-negative valued and x − g p (z) is always equal to zero when x and z are sampled from p(x, z).

f-divergences
We can now show that all f-divergences are part of the c-Wasserstein family. Consider the following cost functional: where f is a convex function such that f (0) = 1. From Theorem 2 it follows that this cost functional defines a valid c-Wasserstein divergence. We can now show that the c-Wasserstein divergence defined by this functional is the f -divergence defined by f . In fact since q(x 2 ) is the marginal of all γ(x 1 , x 2 ) in Γ[p, q].

Wasserstein variational autoencoders
We will now use the concepts developed in the previous sections in order to define a new form of autoencoder. VAEs are generative deep amortized Bayesian models where the parameters of both the probabilistic model and the variational model are learned by minimizing a joint-contrastive divergence [25,29,30]. Let D p and D q be parametrized probability distributions and g p (z) and h q (x) be the outputs of deep networks determining the parameters of these distributions. The probabilistic model (decoder) of a VAE has the following form: The variational model (encoder) is given by: We can define a large family of objective functions of VAEs by combining the cost functionals defined in the previous section. The general form is given by the following total autoencoder cost functional: where w is a vector of non-negative valued weights, d x (x 1 , x 2 ) is a metric on the observable space and f is a convex function.

Connections with related methods
In the previous sections we showed that variational inference based on f-divergences is a special case of Wasserstein variational inference. We will discuss several theoretical links with some recent variational methods.

Operator variational inference
Wasserstein variational inference can be shown to be a special case of a generalized version of operator variational inference [9]. The (amortized) operator variational objective is defined as follows: where F is a set of test functions and ζ(·) is a positive valued function. The dual representation of the optimization problem in the c-Wasserstein loss (Eq. 5) is given by the following expression: where Converting the expectation over p to an expectation over q using importance sampling, we obtain the following expression: which has the same form as the operator variational loss in Eq. 17 with t(x) = x and O p,q = p/q − 1.
Note that the fact that ζ(·) is not positive valued is irrelevant since the optimum of Eq. 18 is always non-negative. This is a generalized form of operator variational loss where the functional family can now depend on p and q. In the case of optimal transport divergences, where C p,q (x 1 , z 1 ; x 2 , z 2 ) = c(x 1 , z 1 ; x 2 , z 2 ), the resulting loss is a special case of the regular operator variational loss.

Wasserstein autoencoders
The recently introduced Wasserstein autoencoder uses a regularized optimal transport divergence between p(x) and k(x) in order to train a generative model [31]. The regularized loss has the following form: where c x does not depend on p and q and D(p(z) q(z)) is an arbitrary divergence. This loss was not derived from a variational Bayesian inference problem. Instead, the Wasserstein autoencoder loss is derived as a relaxation of an optimal transport loss between p(x) and k(x): When D(p(z) q(z)) is a c-Wasserstein divergence, we can show that the L W A is a Wasserstein variational inference loss and consequently that Wasserstein autoencoders are approximate Bayesian methods. In fact: [c x (x 2 , g p (z 2 )) + λC p,q z (z 1 , z 2 )] dγ .
In the original paper the regularization term D(p(z) q(z)) is either the Jensen-Shannon divergence (optimized using adversarial training) or the maximum mean discrepancy (optimized using a reproducing kernel Hilbert space estimator). Our reformulation suggests another way of training the latent space using a metric optimal transport divergence and the Sinkhorn iterations.

Experimental evaluation
We will now demonstrate experimentally the effectiveness and robustness of Wasserstein variational inference. We focused our analysis on variational autoecoding problems. We decided to use simple deep architectures and to avoid any form of structural and hyper-parameter optimization for three main reasons. First and foremost, our main aim is to show that Wasserstein variational inference works off-the-shelf without user tuning. Second, it allows us to run a large number of analyses and consequently to systematically investigate the performance of several variants of the Wasserstein autoencoder on several datasets. Finally, it minimizes the risk of inducing a bias that disfavors the baselines.
In our first experiment, we assessed the performance and the robustness of our Wasserstein variation autoencoder against a conventional VAE and ALI, a more recent (adversarial) likelihood-free alternative [22]. We used the same neural architecture for all models. The generative models were parametrized by three-layered fully connected networks (100-300-500-1568) with Relu nonlinearities in the hidden layers. Similarly, the variational models were parametrized by three-layered ReLu networks (784-500-300-100). The cost functional of our Wasserstein variational autoencoder (see Eq. 16) had the weights w 1 , w 2 , w 3 and w 4 different from zero. Conversely, in this experiment w 5 was set to zero, meaning that we did not use a f-divergence component. We refer to this model as 1111. We trained 1111 using t = 20 Sinkhorn iterations and = 0.1. We assessed the robustness of the methods by running 30 re-runs of the experiment for each method. In each of these re-runs, the parameters of the networks were re-initialized and the weights of the losses (4 weights for 1111, 2 weights for VAE and 2 weights for ALI) were randomly sampled from the interval [0.1, 1]. We evaluated three performance metrics: 1) mean squared deviation in the latent space, 2) pixelwise mean squared reconstruction error in the image space and 3) sample quality estimated as the smallest Euclidean distance with an image in the validation set. The results are reported in Table 1. Our 1111 model outperforms both VAE and ALI in all metrics. Furthermore, the performance of 1111 with respect to all metrics is very stable to perturbations in the weights, as demonstrated by the small standard deviations. Note that the maximum error of 1111 is lower than the minimum errors of the other methods in four different comparisons. Figure 1 shows the reconstruction of several real images and some generated images for all methods in a randomly chosen run. In this run, the reconstructions from ALI collapsed into only 7s and 9s. In our setup, this phenomenon was observed in all runs with the reconstructions collapsing on different digits. This explains the high observable reconstruction error of ALI in Table 1.
In our second experiment we tested several other forms of Wasserstein variational autoencoders on three different datasets. We denote different versions of our autoencoder with a binary string  denoting which weight was set to either zero or one. For example, we denote the purely metric version without autoencoder divergences as 1100. We also included two hybrid models obtained by combining our loss (1111) with the VAE and the ALI losses. These methods are special cases of Wasserstein variational autoencoders with non-zero w 5 weight and where the f function is chosen to give either the reverse KL divergence or the Jansen-Shannon divergence respectively. Note that this fifth component of the loss was not obtained from the Sinkhorn iterations. As can be seen in Table 2, most versions of the Wasserstein variational autoencoder perform better than both VAE and ALI on all datasets. The 0011 has good reconstruction errors but significantly lower sample quality as it does not explicitly train the marginal distribution of x. Interestingly, the purely metric 1100 version has a small reconstruction error even if the cost functional is solely defined in terms of the marginals over x and z. Also interestingly, the hybrid methods h-VAE and h-ALI have high performances. This result is promising as it suggests that the Sinkhorn loss can be used for stabilizing adversarial methods.

Conclusions
In this paper we showed that Wasserstein variational inference offers an effective and robust method for black-box (amortized) variational Bayesian inference. Importantly, Wasserstein variational inference is a likelihood-free method and can be used together with implicit variational distributions and differentiable variational programs [21,20]. These features make Wasserstein variational inference particularly suitable for probabilistic programming, where the aim is to combine declarative general purpose programming and automatic probabilistic inference.