V ECTOR Q UANTIZED W ASSERSTEIN A UTO -E NCODER
Anonymous authors
Paper under double-blind review
A BSTRACT
Learning deep discrete latent presentations offers a promise of better symbolic and summarized abstractions that are more useful to subsequent downstream tasks.
Recent work on Vector Quantized Variational Auto-Encoder (VQ-VAE) has made substantial progress in this direction.
However, this quantizes latent representations using the online k-means algorithm which suffers from poor initialization and non-stationary clusters.
To strengthen the clustering quality for the latent representations, we propose Vector Quantized Wasserstein Auto-Encoder (VQ-WAE) intuitively developed based on the clustering viewpoint of Wasserstein (WS) distance.
Specifically, we endow a discrete distribution over the codewords and learn a deterministic decoder that transports the codeword distribution to the data distribution via minimizing a WS distance between them.
We develop further theories to connect it with the clustering viewpoint of WS distance, allowing us to have a better and more controllable clustering solution.
Finally, we empirically evaluate our method on several well-known benchmarks, where it achieves better qualitative and quantitative performances than the baselines in terms of the codebook utilization and image reconstruction/generation.
I NTRODUCTION
Learning compact yet expressive representations from large-scale and high-dimensional unlabeled data is an important and long-standing task in machine learning (Kingma & Welling, 2013; Chen et al., 2020; Chen & He, 2021; Zoph et al., 2020).
Among many different kinds of methods, Variational Auto-Encoder (VAE) (Kingma & Welling, 2013) and its variants (Tolstikhin et al., 2017;
Alemi et al., 2016; Higgins et al., 2016; Voloshynovskiy et al., 2019) have shown great success in unsupervised representation learning.
Although these continuous representation learning methods have been applied successfully to various problems ranging from images (Pathak et al., 2016;
Goodfellow et al., 2014; Kingma et al., 2016), video and audio (Reed et al., 2017;
Oord et al., 2016;
Kalchbrenner et al., 2017), in some contexts, input data are more naturally modeled and encoded as discrete symbols rather than continuous ones.
For example, discrete representations are a natural fit for complex reasoning, planning and predictive learning (Van Den Oord et al., 2017).
This motivates the need of learning discrete representations, preserving the insightful characteristics of input data.
Vector Quantization Variational Auto-Encoder (VQ-VAE) (Van Den Oord et al., 2017) is a pioneer generative model, which successfully combines the VAE framework with discrete latent representations.
In particular, the vector quantized models learn a compact discrete representation using a deterministic encoder-decoder architecture in the first stage, and subsequently applied this highly compressed representation for various downstream tasks, examples including image generation (Esser et al., 2021), cross-modal translation (Kim et al., 2022), and image recognition (Yu et al., 2021).
While VQ-VAE has been widely applied to representation learning in many areas (Henter et al., 2018; Baevski et al., 2020; Razavi et al., 2019; Kumar et al., 2019; Dieleman et al., 2018; Yan et al., 2021), it is known to suffer from codebook collapse , which has a low codebook usage, i.e. most of embedded latent vectors are quantized to just few discrete codewords, while the other codewords are rarely used, or dead , due to the poor initialization of the codebook, reducing the information capacity of the bottleneck (Roy et al., 2018; Takida et al., 2022; Yu et al., 2021).
To mitigate this issue, additional training heuristics were proposed, such as the exponential moving average (EMA) update (Van Den Oord et al., 2017; Razavi et al., 2019), soft expectation maximization (EM) update (Roy et al., 2018), codebook reset (Dhariwal et al., 2020; Williams et al., 2020).
Notably, soft expectation maximization (EM) update (Roy et al., 2018) connects the EMA update with an EM algorithm and softens the EM algorithm with a stochastic posterior.
Codebook reset randomly reinitializes unused/low-used codewords to one of the encoder outputs (Dhariwal et al., 2020) or those near codewords of high usage Williams et al.
Takida et al.
(2022) suspects that deterministic quantization is the cause of codebook collapse and extends the standard VAE with stochastic quantization and trainable posterior categorical distribution, showing that the annealing of the stochasticity of the quantization process significantly improves the codebook utilization.
Additionally, WS distance has been applied successfully to generative models and continuous representation learning (Arjovsky et al., 2017; Gulrajani et al., 2017;
Tolstikhin et al., 2017)
owing to its nice properties and rich theory.
It is natural to ask: ”Can we take advantages of intuitive properties of the WS distance and its mature theory for learning highly compact yet expressive discrete representations?”
Toward this question, in this paper, we develop solid theories by connecting the theory bodies and viewpoints of the WS distance, generative models, and deep discrete representation learning.
In particular, a) we first endow a discrete distribution over the codebook and propose learning a ”deterministic decoder transporting the codeword to data distributions” via minimizing the WS distance between them; b) To devise a trainable algorithm, we develop Theorem 3.1 to equivalently turn the above WS minimization to push-forwarding the data to codeword distributions via minimizing a WS distance between ”the latent representation and codeword distributions”; c)
More interestingly, our Corollary 3.1 proves that when minimizing the WS distance between the latent representation and codeword distributions, the codewords tend to flexibly move to the clustering centroids of the latent representations with a control on the proportion of latent representations associated to a centroid.
We argue and empirically demonstrate that using the clustering viewpoint of a WS distance to learn the codewords, we can obtain more controllable and better centroids than using a simple k-means as in VQ-VAE (cf.
Sections 3.1 and 5.2).
Our method, called Vector Quantized Wasserstein Auto-Encoder (VQ-WAE), applies the WS distance to learn a more controllable codebook, hence leading to an improvement in the codebook utilization.
We conduct comprehensive experiments to demonstrate our key contributions by comparing with VQ-VAE (Van Den Oord et al., 2017) and SQ-VAE (Takida et al., 2022) ( i.e. , the recent work that can improve the codebook utilization).
The experimental results show that our VQ-WAE can achieve better codebook utilization with higher codebook perplexity, hence leading to lower (compared with VQ-VAE) or comparable (compared with SQ-VAE) reconstruction error, with significantly lower reconstructed Fr ´ echlet Inception Distance (FID) score (Heusel et al., 2017).
Generally, a better quantizer in the stage-1 can naturally contribute to stage-2 downstream tasks (Yu et al., 2021; Zheng et al., 2022).
To further demonstrate this, we conduct comprehensive experiments on four benchmark datasets for both unconditional and class-conditional generation tasks.
The experimental results indicate that from the codebooks of our VQ-WAE, we can generate better images with lower FID scores.
V ECTOR Q UANTIZED V ARIATIONAL A UTO -E NCODER
Given a training set D = { x 1 , ..., x N } ⊂ R V , VQ-VAE (Van Den Oord et al., 2017) aims at learning a codebook which is formed by set of codewords C
= [ c k ] Kk =1 ∈ R K × D on the latent space Z ∈ R D , an encoder f e to map the data examples to the codewords, and a decoder f d (i.e., q ( x | z ) ) to reconstruct accurately the data examples from the codewords.
Given a data example x , the encoder f e (i.e., p ( z | x )) associates x to the codeword ¯ f e ( x ) =
c defined as
[Equation]
where d z is a metric on the latent space.
The objective function of VQ-VAE is as follows:
[Equation]
where P x = 1 N (cid:80) Nn =1 δ x n is the empirical data distribution, sg specifies stop gradient, d x is a cost metric on the data space, and β is set between 0 . 1 and 2 .
(Van Den Oord et al., 2017) and d z ( f e ( x ) , C ) =
(cid:80) c ∈ C d z ( f e ( x ) , c ) .
The purpose of VQ-VAE training is to form the latent representations in clusters and adjust the codewords to be the centroids of these clusters.
C ONTROLLABLE C ODEBOOKS WITH W ASSERSTEIN Q UANTIZATION
We present the theoretical development of our VQ-WAE framework which connects the viewpoints of the WS distance, generative models, and deep discrete representation learning in Section 3.1.
Specifically, we propose to learn a ”deterministic decoder transporting the codeword to data distributions” via minimizing the WS distance between them (Figure 1a (Top)).
We then turn the above
WS minimization to push-forwarding the data to codeword distribution via minimizing a WS distance between ”the latent representation and codeword distributions” (Figure 1a (Bottom)).
We prove that when minimizing the WS distance between the latent representation and codeword distributions, the codewords tend to flexibly move to the clustering centroids of the latent representations with a control on the proportion of latent representations associated with a centroid (Figure 1b).
Based on the theoretical development, we devise a practical algorithm for VQ-WAE in Section 3.2.
T HEORETICAL D EVELOPMENT
Given a training set D = { x 1 , ..., x N } ⊂ R V , we wish to learn a codebook C
= { c k } Kk =1 ⊂ R K × D on a latent space Z and an encoder to map each data example to a given codebook, preserving insightful characteristics carried in the data.
We first endow a discrete distribution over the codewords as P c,π = (cid:80)
Kk =
π k δ c k with the Dirac delta function δ and the weights π ∈ ∆ K − 1 = { π ′ ≥ 0 :
∥ π ′ ∥ 1 = 1 } .
We aim to learn a decoder function f d : Z → X (i.e., mapping from the latent space Z ⊂ R D to the data space X ), the codebook C , and the weights π , to minimize:
[Equation]
where P x = 1 N (cid:80) Nn =1 δ x n is the empirical data distribution and d x is a cost metric on the data space.
We interpret the optimization problem (OP) in Eq.
(1) as follows.
Given a discrete distribution P c,π on the codewords, we use the decoder f d to map the codebook C to the data space and consider
W d x ( f d # P c , P x ) as the codebook-data distortion w.r.t.
f d .
We subsequently learn f d to minimize the codebook-data distortion given P c,π and finally adjust the codebook C and π to minimize the optimal codebook-data distortion.
To offer more intuition for the OP in Eq.
(1), we introduce the following lemma.
Lemma 3.1.
Let C ∗ =
{ c ∗ k } k , π ∗ , and f ∗ d be the optimal solution of the OP in Eq.
Assume
K < N , then C ∗ =
{ c ∗ k } k , π ∗ , and f ∗ d are also the optimal solution of the following OP:
[Equation]
) where Σ π is the set of assignment functions σ : { 1 , ..., N } → { 1 , ..., K } such that the cardinalities (cid:12)(cid:12) σ − 1 ( k ) (cid:12)(cid:12) , k = 1 , ..., K are proportional to π k , k = 1 , ..., K .
Lemma 3.1 states that for the optimal solution C ∗ = { c ∗ k } , π ∗ , and f ∗ d of the OP in Eq.
(1), { f ∗ d ( c ∗ k ) }
Kk =1 become the optimal clustering centroids of the optimal clustering solution which minimizes the distortion.
Inspired by Wasserstein Auto-Encoder (Tolstikhin et al., 2017), we establish the following theorem to engage the OP in (1) with the latent space.
Theorem 3.1.
We can equivalently turn the optimization problem in (1) to
[Equation]
where ¯ f e is a deterministic discrete encoder mapping data example x directly to the codebook.
First, we learn both the codebook C and the weights π .
Second, ours seeks a deterministic discrete encoder ¯ f e mapping data example x directly to a codeword, concurring with vector quantization and serving our further derivations, whereas Theorem 1 in Tolstikhin et al.
(2017) involves a probabilistic/stochastic encoder mapping to a continuous latent distribution (i.e., a larger space to search).
More importantly, our proof is totally different from that in Tolstikhin et al.
(2017) (all proof details are given in Appendix A).
Additionally, ¯ f e is a deterministic discrete encoder mapping a data example x directly to a codeword.
To make it trainable, we replace ¯ f e by a continuous encoder f
e : X → Z and arrive the OP:
[Equation]
(4) where Q C ( f e ( x )) =
argmin c ∈ C d z ( f e ( x ) , c ) is a quantization operator which returns the closest codeword to f e ( x ) and the parameter λ > 0 .
Particularly, we can rigorously prove that the two optimization problems of interest in (3) and (4) are equivalent under some mild conditions in Theorem 3.2.
This rationally explains why we could solve the OP in (4) for our final tractable solution.
Theorem 3.2.
If we seek f d and f e in a family with infinite capacity (e.g., the family of all measurable functions), the three OPs of interest in (1, 3, and 4) are equivalent.
Moreover, the OP in (4) conveys important meaningful interpretations.
Specifically, by minimizing
W d z ( f e # P x , P c,π ) w.r.t.
C, π , we aim to learn the codewords that are clustering centroids of f e # P x
according to the clustering viewpoint of OT as shown in Corollary 3.1, and similar to VQ-VAE, we quantize f e ( x ) to the closest codeword using Q C ( f e ( x ))
=
argmin c ∈ C d z ( f e ( x ) , c ) and try to reconstruct x from this codebook.
Corollary 3.1.
Consider minimizing the second term: min f e ,C W d z ( f e # P x , P c,π ) in (4) given π and assume K < N , its optimal solution f ∗ e and C ∗ are also the optimal solution of the OP:
[Equation]
) where Σ π is the set of assignment functions σ : { 1 , ..., N } → { 1 , ..., K } such that the cardinalities (cid:12)(cid:12) σ − 1 ( k ) (cid:12)(cid:12) , k = 1 , ..., K are proportional to π k , k = 1 , ..., K .
Corollary 3.1 indicates the aim of minimizing the second term W d z ( f e # P x , P c,π ) in (4).
By which, we adjust the encoder f e and the codebook C such that the codewords of C become the clustering are quantized to codeword.
centroids of the latent representations { f e ( x n ) } n to minimize the codebook-latent distortion (see
Figure 1 (Right)).
Additionally, at the optimal solution, the optimal assignment function σ ∗ , which indicates how latent representations (or data examples) associated with the clustering centroids (i.e., the codewords) has a valuable property, i.e., the cardinalities (cid:12)(cid:12) ( σ ∗ ) − 1 ( k ) (cid:12)(cid:12) , k
= 1 , ..., K are proportional to π k , k = 1 , ..., K .
Remark:
Recall the codebook collapse issue, i.e. most of embedded latent vectors are quantized to just few discrete codewords while the other codewords are rarely used.
Corollary 3.1 give us important properties: (1) we can control the number of latent representations assigned to each codeword by adjust π , guaranteeing all codewords are utilized, (2) codewords become the clustering centroids of the associated latent representations to minimize the codebook-latent distortion , to develop our
VQ-WAE framework.
3. P ROPOSED F RAMEWORK
One of crucial aims of learning meaningful and well-distributed codewords is to make use of each individual codeword efficiently by solving the OP in (4).
Specifically, we wish the latent representations are more uniformly associated with the codewords.
Based on Corollary 3.1, pointing out that the numbers of latent representations associated with the k th codeword is proportional to π k , we hence fix π as a uniform distribution (i.e., P c,π = (cid:80)
Kk =1
K δ c k ) to make all the codewords utilized equally by the model, hence boosting the perplexity or the codebook usage.
We now present the practical method based on the OP in (4) with P c,π = (cid:80)
Kk =1 1 K δ c k .
At each iteration, we sample a mini-batch x 1 , ..., x B and then solve the OP in (4) by updating f d , f e and C based on this mini-batch as follows.
Let us denote P b = 1 B (cid:80)
Bj = i
δ x i as the empirical distribution of embedded vectors.
over the current batch.
Basically, we learn the optimal transportation plan P ∗ by solving:
[Equation]
where 1 B = (cid:2) 1 B (cid:3) B is the vector of atom masses of P b , 1 C = (cid:2) 1 C (cid:3) C is the vector of atom masses of
P c,π , Γ (1 B , 1 C ) is the set of feasible transportation plans, and D c,x = [ d z ( x i , c k )] i,k ∈ R B × K is the cost matrix.
The pseudcode of our VQ-WAE is summarized in Algorithm 1.
We use the copy gradient trick (Van Den Oord et al., 2017) to deal with the back-propagation from decoder to encoder for reconstruction term while Wasserstein regularization term W d z ( f e # P b , P c,π ) can be optimized directly without further manipulation. Additionally, W d z ( f e # P b , P c,π ) term is only utilized in the training phase.
objective in (4): [Equation] 8: end for 9: Return: The optimal f e , f d and C .  R ELATED W ORK
Variational Auto-Encoder (VAE) was first introduced by Kingma & Welling (Kingma & Welling, 2013) for learning continuous representations. However, learning discrete latent representations has proved much more challenging because it is nearly impossible to accurately evaluate the gradients which are required to train models. To make the gradients tractable, one possible solution is to apply the Gumbel Softmax reparameterization trick (Jang et al., 2016) to VAE, which allows us to estimate stochastic gradients for updating the models. Although this technique has a low variance, it brings up a high-bias gradient estimator.
Another possible solution is to employ the REINFORCE algorithm (Williams, 1992), which is unbiased but has a high variance.
Additionally, the two techniques can be complementarily combined (Tucker et al., 2017).
To enable learning the discrete latent codes, VQ-VAE (Van Den Oord et al., 2017) uses deterministic encoder/decoder and encourages the codebooks to become the clustering centroids of latent representations.
Additionally, the copy gradient trick is employed in back-propagating gradients from the decoder to the encoder (Bengio, 2013).
Some further works were proposed to extend VQ-VAE, notably (Roy et al., 2018; Wu & Flierl, 2020).
Particularly, Roy et al.
(2018) uses the Expectation
Maximization (EM) algorithm in the bottleneck stage to train the VQ-VAE for improving the quality of the generated images.
However, to maintain the stability of this approach, we need to collect a large number of samples on the latent space.
Wu & Flierl (2020) imposes noises on the latent codes and uses a Bayesian estimator to optimize the quantizer-based representation.
The introduced bottleneck Bayesian estimator outputs the posterior mean of the centroids to the decoder and performs soft quantization of the noisy latent codes which have latent representations preserving the similarity relations of the data space.
Recently, Takida et al.
(2022) extends the standard VAE with stochastic quantization and trainable posterior categorical distribution, showing that the annealing of the stochasticity of the quantization process significantly improves the codebook utilization.
Wasserstein (WS) distance has been widely used in generative models (Arjovsky et al., 2017; Gulrajani et al., 2017; Tolstikhin et al., 2017). Arjovsky et al. Arjovsky et al. (2017) uses a dual form of
WS distance to develop Wasserstein generative adversarial network (WGAN).
Later, Gulrajani et al.
(2017) employs the gradient penalty trick to improve the stability of WGAN.
In terms of theory development, mostly related to our work is Wasserstein Auto-Encoder (Tolstikhin et al., 2017) which aims to learn continuous latent representation preserving the characteristics of input data.
E XPERIMENTS
In this section, we conduct extensive experiments to show the effectiveness of our proposed method compared to other advances.
Datasets : we empirically evaluate the proposed VQ-WAE in comparison with VQ-VAE (Van
Den Oord et al., 2017) that is the baseline method and recently proposed SQ-VAE (Takida et al., 2022) which is the state-of-the-art work of improving the codebook usage, on four different benchmark datasets: CIFAR10 (Van Den Oord et al., 2017), MNIST (Deng, 2012), SVHN (Netzer et al., 2011), CelebA (Liu et al., 2015) and the high-resolution images dataset FFHQ Karras et al.
Implementation :
For a fair comparison, we utilize the same architectures and hyper-parameters for all methods.
Additionally, in the primary setting, we use the codeword (discrete latent) dimensionality of 64 and codebook size | C | = 512 for all datasets except FFHQ with codeword dimensionality of 256 and | C | = 1024 , while the hyper-parameters { β, τ, λ } are specified as presented in the original papers, i.e. , β = 0 .
25 for VQ-VAE and VQ-GAN (Esser et al., 2021), τ = 1 e − 5 for SQ-VAE and λ = 1 for our VQ-WAE.
The details of the experimental settings are presented in Appendix C.
5. R ESULTS ON B ENCHMARK D ATASETS
In order to quantitatively assess the quality of the reconstructed images, we report the results on most common evaluation metrics, including the pixel-level peak signal-to-noise ratio (PSNR), patchlevel structure similarity index (SSIM), feature-level LPIPS (Zhang et al., 2018), and dataset-level
Fr ´ echlet Inception Distance (FID) (Heusel et al., 2017).
We report the test-set reconstruction results on four datasets in Table 1.
With regard to the codebook utilization, we employ perplexity score which is defined as e − (cid:80)
Kk =1 p ck log p ck where p c k =
N ck (cid:80) Ki
[-=
-]=1 N ci (i.e., N c i is the number of latent representations associated with the codeword c i ) is the probability of the i th codeword being used.
Note that by formula, perplexity max = | C | as P ( c ) becomes to the uniform distribution, which means that all the codewords are utilized equally by the model.
[Table] We compare VQ-WAE with VQ-VAE, SQ-VAE and VQ-GAN for image reconstruction in Table 1.
All instantiations of our model significantly outperform the baseline VQ-VAE under the same compression ratio, with the same network architecture.
While the latest state-of-the-art SQ-VAE holds slightly better scores for traditional pixel- and patch-level metrics, our method achieves much better rFID scores which evaluate the image quality at the dataset level.
Note that our VQ-WAE significantly improves the perplexity of the learned codebook.
This suggests that the proposed method significantly improves the codebook usage, resulting in better reconstruction quality.
Finally, to complete the assessment, the qualitative results are visualized in Figure 4 (Appendix B).
5. D ETAILED A NALYSIS
We run a number of ablations to analyze the properties of VQ-VAE, SQ-VAE and VQ-WAE, in order to assess if our VQ-WAE can simultaneously achieve (i) efficient codebook usage, (ii) reasonable latent representation.
5.2. C ODEBOOK U SAGE
[Table] We observe the codebook utilization of three methods with different codebook sizes { 64 , 128 , 256 , 512 } on MNIST and CIFAR10 datasets.
Particularly, we present the reconstruction performance for different settings in Table 2 and the histogram of latent representations over the codebook in Figure 2.
As discussed in Section 3.1 and Section 3.2, the number of used centroids reflects the capability of the latent representations.
In other words, it represents the certain amount of information is pre- [Figure] served in the latent space.
By explicitly defining the numbers of latent representations associated with the codebooks to be uniform (i.e., fixing π in (4) as a uniform distribution) in the Wasserstein regularization term, VQ-WAE is able to maximize the information in the codebooks, hence improving the reconstruction capacity.
It can be seen from Figure 2 that the latent distribution of VQ-WAE over the codebook is nearly uniform and the codebook’s perplexity almost reaches the optimal value (i.e., the value of perplexities reach to corresponding codebook sizes) in different settings.
It is also observed that as the size of the codebook increases, the perplexity of codebook of VQ-WAE also increases, leading to the better reconstruction performance (Table 2), in line with the analysis in (Wu & Flierl, 2018) .
SQ-VAE also has good codebook utilization as its perplexity is proportional to the size of the codebook.
However, its codebook utilization becomes less efficient when the codebook size becomes large, especially in low texture dataset (i.e., MNIST).
On the contrary, the codebook usage of VQ-VAE is less efficient, i.e., there are many zero entries in its codebook usage histogram, indicating that some codewords have never been used (Figure 2).
Furthermore, Table 2 also shows the instability of VQ-VAE’s reconstruction performance with different codebook sizes.
5.2. V ISUALIZATION OF L ATENT R EPRESENTATION
To better understand the codebook’s representation power, we employ t-SNE (van der Maaten &
Hinton, 2008) to visualize the latent representations that have been learned by VQ-VAE, SQ-VAE and VQ-WAE on the MNIST dataset with two codebook sizes of 64 and 512 .
Figure 3 shows the latent distributions of different classes in the latent space, in which the samples are colored accordingly to their class labels.
Figure 3c shows that representations from different classes of VQWAE are well clustered (i.e., each class focuses on only one cluster) and clearly separated to other classes.
In contrast, the representations of some classes in VQ-VAE and SQ-VAE are distributed to several clusters and or mixed to each other (Figure 3a,b).
Moreover, the class-clusters of SQ-VAE are uncondensed and tend to overlap with each other.
These results suggest that the representations learned by VQ-WAE can better preserve the similarity relations of the data space better than the other models.
5.2. I MAGE G ENERATION
As discussed in the previous section, VQ-WAE is able to optimally utilize its codebook, leading to meaningful and diverse codewords that naturally improve the image generation.
To confirm this ability, we perform the image generation on the benchmark datasets.
Since the decoder reconstructs images directly from the discrete embeddings, we only need to model a prior distribution over the discrete latent space (i.e., codebook) to generate images.
We employ a conventional autoregressive model, the CNN-based PixelCNN (Van den Oord et al., 2016), to estimate a prior distribution over the discrete latent space of VQ-VAE, SQ-VAE and VQWAE on CIFAR10, MNIST, SVHN and CelebA. The details of generation settings are presented
[Table]
in Section 3.2 of the supplementary material.
The quantitative results in Table 3 indicate that the codebook of VQ-WAE leads to a better generation ability than VQ-VAE and SQ-VAE
[Table]
C ONCLUSION
In this paper, inspired by the nice properties and mature theory of the WS distance allowing it to be applied successfully to generative models and continous representation learning, we propose Vector Quantized Wasserstein Auto-Encoder (VQ-WAE), which endows a discrete distribution over the codewords and learns a deterministic decoder that transports the codeword distribution to the data distribution via minimizing a WS distance between them.
We then developed theoretical analysis to show the equivalence of this WS minimization to another OP regarding push-forwarding the data distribution to the codeword distribution, which can be realized by minimizing a WS distance between the latent representation and codeword distributions.
We conduct comprehensive experiments to show that our VQ-WAE utilizes the codebooks more efficiently than the baselines, hence leading to better reconstructed and generated image quality.
R EPRODUCIBILITY S TATEMENT
We provide the implementation of our framework in the supplementary material.
A PPENDIX
This appendix is organized as follows:
• In Section A, we present all proofs for theory developed in the main paper.
• In Section B, we present additional experimental results on the high-quality image dataset FFHQ.
• In Section C, we present experimental settings and implementation specification of VQWAE.
A T HEORETICAL D EVELOPMENT
Given a training set D = { x 1 , ..., x N } ⊂ R V , we wish to learn a set of codebooks C
= { c k } Kk =1 ∈
R K × D on a latent space Z and an encoder to map each data example to a given codebook, preserving insightful characteristics carried in data.
We first endow a discrete distribution over the codebooks as P c,π = (cid:80)
Kk =
π k δ c k with the Dirac delta function δ and the weights π ∈ ∆ K .
We aim to learn a decoder function f d : Z → X (i.e., mapping from the latent space Z ⊂ R D to the data space X ), the codebooks C , and the weights π , to minimize:
[Equation]
where P x = 1 N (cid:80) Nn =1 δ x n is the empirical data distribution and d x is a cost metric on the data space.
Lemma A.1.
(Lemma 3.1 in the main paper) Let C ∗ =
{ c ∗ k } k , π ∗ , and f ∗ d be the optimal solution of the OP in Eq.
Assume K < N , then C ∗ = { c ∗ k } k , π ∗ , and f ∗ d are also the optimal solution of the following OP:
[Equation]
where Σ π is the set of assignment functions σ : { 1 , ..., N } → { 1 , ..., K } such that the cardinalities (cid:12)(cid:12) σ − 1 ( k ) (cid:12)(cid:12) , k = 1 , ..., K are proportional to π k , k = 1 , ..., K .
Proof of Lemma A.
It is clear that
[Equation]
Therefore, we reach the following OP:
[Equation]
By using the Monge definition, we have
[Equation]
Since T # P x = f d # P c,π , T ( x n ) =
f d ( c k ) for some k .
Additionally, (cid:12)(cid:12) T − 1 ( f d ( c k ))
(cid:12)(cid:12) , k = 1 , ..., K are proportional to π k , k = 1 , ..., K .
Denote σ : { 1 , ..., N } → { 1 , ..., K } such that T ( x n ) =
f d ( c σ ( n ) ) , ∀ i = 1 , ..., N , we have σ ∈ Σ π .
It follows that
[Equation]
Finally, the the optimal solution of the OP in Eq.
(7) is equivalent to
[Equation]
which directly implies the conclusion.
Theorem A.1.
(Theorem 3.1 in the main paper)
We can equivalently turn the optimization problem in (7) to
[Equation]
where ¯ f e is a deterministic discrete encoder mapping data example x directly to the codebooks.
Proof of Theorem A.
We first prove that the OP of interest in (7) is equivalent to
[Equation]
where ¯ f e is a stochastic discrete encoder mapping data example x directly to the codebooks.
To this end, we prove that
[Equation]
where ¯ f e is a stochastic discrete encoder mapping data example x directly to the codebooks.
Let ¯ f e be a stochastic discrete encoder
such that ¯ f e # P x
= P c,π (i.e., x ∼ P x and c ∼ ¯ f e ( x ) implies c ∼ P c,π ).
We consider γ d,c as the joint distribution of ( x, c ) with x ∼ P x and c ∼ ¯ f e ( x ) .
We also consider γ fc,d as the joint distribution including ( x, x ′ ) ∼ γ fc,d where x ∼ P x ,c ∼ ¯ f e ( x ) , and x ′ =
f d ( c ) .
This follows that γ fc,d ∈ Γ ( f d # P c,π , P x ) which admits f d # P c,π and P x as its marginal distributions.
We also have:
[Equation]
Note that we have the equality in (1) due to ( id, f d ) # γ d,c
= γ fc,d .
Therefore, we reach
[Equation]
Let γ fc,d ∈ Γ ( f d # P c,π , P x ) .
Let γ fc,c ∈ Γ ( f d # P c,π , P c,π ) be a deterministic coupling such that c ∼ P c,π and x = f d ( c ) imply ( c, x ) ∼ γ c,fc .
Using the gluing lemma (see Lemma 5.
in Santambrogio (2015)), there exists a joint distribution α ∈ Γ ( P c,π , f d # P c,π , P x ) which admits γ fc,d and γ fc,c as the corresponding joint distributions.
By denoting γ d,c ∈ Γ ( P x , P c,π ) as the marginal distribution of α over P x , P c,π , we then have
E ( x,x ′ ) ∼ γ fc
[Equation]
x,
[Equation]
where ¯ f e ( x ) = γ d,c ( · | x ) .
This follows that
[Equation]
This completes the proof for the equality in Eq.
(12), which means that the OP of interest in (7) is equivalent to
[Equation]
We now further prove the above OP is equivalent to
[Equation]
where ¯ f e is a deterministic discrete encoder mapping data example x directly to the codebooks.
It is obvious that the OP in (14) is special case of that in (13) when we limit to search for deterministic discrete encoders.
Given the optimal solution C ∗ 1 , π ∗ 1 , f ∗ 1 d , and ¯ f ∗ 1 e of the OP in (13), we show how to construct the optimal solution for the OP in (14).
Let us construct C ∗ 2 =
C ∗ 1 , f ∗ 2 d = f ∗ 1 d .
Given x ∼ P x , let us denote ¯ f ∗ 2 e ( x ) =
argmin c
d x (cid:0) f ∗ 2 d ( c ) , x (cid:1) .
Thus, ¯ f ∗ 2 e is a deterministic discrete encoder mapping data example x directly to the codebooks.
We define π ∗ 2 k =
Pr (cid:0) ¯
f ∗ 2 e ( x )
=
c k : x ∼ P x (cid:1) , k = 1 , ..., K , meaning that ¯ f ∗ 2 e # P x = P c ∗ 2 ,π ∗ 2 .
From the construction of ¯ f ∗ 2 e , we have
[Equation]
Furthermore, because C ∗ 2 , π ∗ 2 , f ∗ 2 d , and ¯ f ∗ 2 e are also a feasible solution of the OP in (14), we have
[Equation]
This means that
[Equation]
and C ∗ 2 , π ∗ 2 , f ∗ 2 d , and ¯ f ∗ 2 e are also the optimal solution of the OP in (14).
Additionally, ¯ f e is a deterministic discrete encoder mapping data example x directly to the codebooks.
To make it trainable, we replace ¯ f e by a continuous encoder f
e : X → Z and arrive the following OP:
[Equation]
where Q C ( f e ( x )) =
argmin c ∈ C d z ( f e ( x ) , c ) is a quantization operator which returns the closest codebook to f e ( x ) and the parameter λ > 0 .
We now propose and prove the following lemma that is necessary for the proof of Theorem A.2.
Lemma A.2.
Consider C, π, f d , and f e as a feasible solution of the OP in (15).
Let us denote ¯ f e ( x ) = argmin c d z ( f e ( x )) , c ) =
Q C ( x ) , then ¯ f e ( x ) is a Borel measurable function.
Proof of Lemma A.2 .
We denote the set
A k on the latent space as
[Equation]
A k is known as a Voronoi cell w.r.t.
the metric d z .
If we consider a continuous metric d z , A k is a measurable set.
Given a Borel measurable function B , we prove that ¯ f − 1 e ( B ) is a Borel measurable set on the data space.
Let B ∩ { c 1 , .., c K } =
{ c i 1 , ..., c i m } , we prove that ¯ f − 1 e ( B ) = ∪ mj
=1 f − 1 e (cid:0) A i j (cid:1) .
Indeed, take x ∈ ¯ f − 1 e ( B ) , then ¯ f e ( x ) ∈ B , implying that ¯ f e ( x ) =
Q C ( x ) =
c i j for some j = 1 , ..., m .
This means that f e ( x ) ∈
A i j for some j = 1 , ..., m .
Therefore, we reach ¯ f − 1 e ( B ) ⊂ ∪ mj =1 f − 1 e (cid:0)
A i j (cid:1) .
We now take x ∈ ∪ mj =1 f − 1 e (cid:0)
A i j (cid:1) .
Then f e ( x ) ∈
A i j for j = 1 , ..., m , hence ¯ f e ( x ) = Q C ( x ) =
c i j for some j = 1 , ..., m .
Thus, ¯ f e ( x ) ⊂ B or equivalently x ∈ ¯ f − 1 e ( B ) , implying ¯ f − 1 e ( B ) ⊃ ∪ mj =1 f − 1 e (cid:0)
A i j (cid:1) .
Finally, we reach ¯ f − 1 e ( B ) = ∪ mj =1 f − 1 e (cid:0)
A i j (cid:1) , which concludes our proof because f e is a measurable function and A i j are measurable sets.
Theorem A.2.
(Theorem 3.2 in the main paper)
If we seek f d and f e in a family with infinite capacity (e.g., the space of all measurable functions), the three OPs of interest in (7, 10, and 15) are equivalent.
Proof of Theorem A.2 .
Given the optimal solution C ∗ 1 , π ∗ 1 , f ∗ 1 d , and f ∗ 1 e of the OP in (15), we conduct the optimal solution for the OP in (10).
Let us conduct C ∗ 2 =
C ∗ 1 , f ∗ 2 d = f ∗ 1 d .
We next define ¯ f ∗ 2 e ( x ) = argmin c d z (cid:0) f ∗ 1 e ( x ) , c (cid:1) =
Q C ∗ 1 (cid:0) f
[Equation]
1 e ( x ) (cid:1) = Q C
[Equation]
2 (cid:0) f ∗ 1 e ( x ) (cid:1) .
We prove that C ∗ 2 , π ∗ 2 , f ∗ 2 d , and ¯ f ∗ 2 e are optimal solution of the OP in (10).
By this definition, we yield ¯ f ∗ 2 e
# P x
= P c ∗ 2 ,π ∗ 2 and hence W d z (cid:0) ¯ f ∗ 2 e # P x , P c ∗ 2 ,π ∗ 2 (cid:1) = 0 .
Therefore, we need to verify the following: (i) ¯ f ∗ 2 e is a Borel-measurable function. (ii) Given a feasible solution C π, f d , and ¯ f e of (10), we have
[Equation]
We first prove (i).
It is a direct conclusion because the application of Lemma A.2 to C ∗ 1 , π ∗ 1 , f ∗ 1 d , and f ∗ 1 e . We next prove (ii).
We further derive as
[Equation]
Moreover
[Equation]
because ¯ f e # P x = P c
[Equation]
π which is a discrete distribution over the set of codewords C
[Equation]
we obtain Q C ( ¯ f e ( x ))
= ¯ f e ( x ) .
Note that C, π, f d , and ¯ f e is also a feasible solution of (15) because ¯ f e is also a specific encoder mapping from the data space to the latent space, we achieve [Equation] Noting that ¯ f e # P x = P c,π and Q C ( ¯ f e ( x )) =
¯ f e ( x ) , we arrive at
[Equation]
Combining the inequalities in (17) and (18), we obtain Inequality (16) as
[Equation]
[Equation]
This concludes our proof.
Corollary A.1.
(Corollary 3.1 in the main paper) Consider minimizing the second term: min f e ,C W d z ( f e # P x , P c,π ) in (15) given π and assume K < N , its optimal solution f ∗ e and C ∗ are also the optimal solution of the following OP:
[Equation]
where Σ π is the set of assignment functions σ : { 1 , ..., N } → { 1 , ..., K } such that the cardinalities (cid:12)(cid:12) σ − 1 ( k ) (cid:12)(cid:12) , k = 1 , ..., K are proportional to π k , k = 1 , ..., K .
Proof of Corollary A.1 .
By the Monge definition, we have
[Equation]
Since T # ( f e # P x ) = P c,π , T ( f e ( x n ))
=
c k for some k .
Additionally, (cid:12)(cid:12) T − 1 ( c k ) (cid:12)(cid:12) , k = 1 , ..., K are proportional to π k , k = 1 , ..., K .
Denote σ : { 1 , ..., N } → { 1 , ..., K } such that T ( f e ( x n )) =
c σ ( n ) , ∀ i = 1 , ..., N , we have σ ∈ Σ π .
It also follows that
[Equation]
B V ISUALIZATION OF RECONSTRUCTION RESULTS
Qualitative assessment : We present the reconstructed samples from FFHQ (high-resolution images) for qualitative evaluation.
It can be clearly seen that the high-level semantic features of the input image and colors are better preserved with VQ-WAE than the baseline.
Particularly, we notice that VQGAN often produces repeated artifact patterns in image synthesis (see the hair of man is second column in Figure 4) while VQ-WAE does not.
This is because VQ-GAN is lack of diversity in the codebook, which will be further analyzed in Section 5.2.1.
Consequently, the quantization operator embeds similar patches into the same quantization index and ignores the variance in these patches (e.g., VQ-GAN reconstructs the background in third column of Figure 4 as hair of woman).
C E XPERIMENTAL S ETTINGS C. VQ- MODEL
Implementation :
For fair comparison, we utilize the same framework architecture and hyperparameters for both VQ-VAE and VQ-WAE.
Specifically, we construct the VQ-VAE and VQ-WAE models as follows:
• For CIFAR10, MNIST and SVHN datasets, the models have an encoder with two convolutional layers of stride 2 and filter size of 4 × 4 with ReLU activation, followed by 2 residual blocks, which contained a 3 × 3, stride 1 convolutional layer with ReLU activation followed by a 1 × 1 convolution.
The decoder was similar, with two of these residual blocks followed by two deconvolutional layers.
• For CelebA dataset, the models have an encoder with two convolutional layers of strideand filter size of 4 × 4 with ReLU activation, followed by 6 residual blocks, which contained a 3 × 3, stride 1 convolutional layer with ReLU activation followed by a 1 × 1 convolution.
The decoder was similar, with two of these residual blocks followed by two deconvolutional layers.
• For high-quality image dataset FFHQ, we utilize the well-known VQGAN framework Esser et al.
(2021) as the baseline. We only replace the regularization module of VQ-VAE i.e., two last terms of objective function: d z ( sg ( f e ( x )) , C ) + βd z ( f e ( x ) , sg ( C )) by our proposed by Wasserstein regularization λ W d z ( f e # P x , P c,π ) ) in Eq.
(4) for VQ-WAE.
Additional, we employ the POT library (Flamary et al., 2021) to compute WS distance for simplicity.
However, our VQ-WAE does not require optimal transport map the from WS distance in (6) to update the model.
Therefore, we can employ a wide range of speed-up algorithms to solve optimization problem (OP) in (6) such as Sinkhorn algorithm (Cuturi, 2013) or entropic regularized dual for (Genevay et al., 2016).
Hyper-parameters : following (Takida et al., 2022), we adopt the adam optimizer for training with: learning-rate is e − 4 , batch size of 32 , embedding dimension of 64 and codebook size | C | = 512 for all datasets except FFHQ with embedding dimension of 256 and | C | = 1024 .
Finally, we train model for CIFAR10, MNIST, SVHN, FFHQ in 100 epoches and for CelebA in 70 epoches respectively.
C. G ENERATION MODEL
Implementation : It is worth to noting that we employ the codebooks learned from reported VQmodels to extract codeword indices and we employ PixelCNN (Van den Oord et al., 2016) with the same setting for generation for all VQ-VAE, SQ-VAE and VQ-WAE.
In particular, we feed
PixelCNN over the ”pixel” values of the 8 × 8 1 -channel latent space for CIAR10, MNIST, SVHN, and 16 × 16 1 -channel latent space for CelebA.
Hyper-parameters : we adopt the adam optimizer for training with: learning-rate is 3 e − 4 , batch size of 32 .
Finally, we PixelCNN over the ”pixel” values of the 8 × 8 1 -channel latent space inepoches.