Year: 2020

Abstract:
How can we perform posterior inference for deep latent variable models in an efficient and flexible manner? Markov chain Monte Carlo (MCMC) methods, such as Langevin dynamics, provide sample approximations of such posteriors with an asymptotic convergence guarantee. However, it is difficult to apply these methods to large-scale datasets owing to their slow convergence and datapointwise iterations. In this study, we propose amortized Langevin dynamics, wherein datapoint-wise MCMC iterations are replaced with updates of an inference model that maps observations into latent variables. The amortization enables scalable inference from large-scale datasets. Developing a latent variable model and an inference model with neural networks, yields Langevin autoencoders (LAEs), a novel Langevin-based framework for deep generative models. Moreover, if we define a latent prior distribution with an unnormalized energy function for more flexible generative modeling, LAEs are extended to a more general framework, which we refer to as contrastive Langevin autoencoders (CLAEs). We experimentally show that LAEs and CLAEs can generate sharp image samples. Moreover, we report their performance of unsupervised anomaly detection.1

heading: 1 INTRODUCTION
text: Latent variable models are widely used for generative modeling (Bishop, 1998; Kingma & Welling, 2013), principal component analysis (Wold et al., 1987), and factor analysis (Harman, 1976). To learn a latent variable model, it is essential to estimate the latent variables, z, from the observations, x. Bayesian inference is a probabilistic approach for estimation, wherein the estimate is represented as a posterior distribution, i.e., p (z | x) = p (z) p (x | z) /p (x). A major challenge while using the Bayesian approach is that the posterior distribution is typically intractable. Markov chain Monte Carlo (MCMC) methods such as Langevin dynamics (LD) provide sample approximations for posterior distribution with an asymptotic convergence guarantee. However, MCMC methods converge slowly. Thus, it is inefficient to perform time-consuming MCMC iterations for each latent variable, particularly for large-scale datasets. Furthermore, when we obtain new observations that we would like to perform inference for, we would need to re-run the sampling procedure for them.
In the context of variational inference, a method to amortize the cost of datapoint-wise optimization known as amortized variational inference (AVI) (Kingma & Welling, 2013; Rezende et al., 2014) was recently proposed. In this method, the optimization of datapoint-wise parameters of variational distributions is replaced with the optimization of an inference model that predicts the variational parameters from observations. This amortization enables posterior inference to be performed efficiently on large-scale datasets. In addition, inference for new observations can be efficiently performed using the optimized inference model. AVI is widely used for the training of deep generative models, and such models are known as variational autoencoders (VAEs). However, methods based on variational inference have less approximation power, because distributions with tractable densities are used for approximations. Although there have been attempts to improve their flexibility (e.g., normalizing flows (Rezende & Mohamed, 2015; Kingma et al., 2016; Van Den Berg et al., 2018; Huang et al., 2018)), such methods typically have constraints in terms of the model architectures (e.g., invertibility in normalizing flows).
1An implementation is available at: https://bit.ly/2Shmsq3
Therefore, we propose an amortization method for LD, amortized Langevin dynamics (ALD). In ALD, datapoint-wise MCMC iterations are replaced with updates of an inference model that maps observations into latent variables. This amortization enables simultaneous sampling from posteriors over massive datasets. In particular, when a minibatch training is used for the inference model, the computational cost is constant with data size. Moreover, when inference is performed for new test data, the trained inference model can be used as initialization of MCMC to improve the mixing, because it is expected that the properly trained inference model can map data into the high-density area of the posteriors. We experimentally show that the ALD can accurately perform sampling from posteriors without datapoint-wise iterations. Furthermore, we demonstrate its applicability to the training of deep generative models. Neural networks are used for both generative and inference models to yield Langevin autoencoders (LAEs). LAEs can be easily extended for more flexible generative modeling, in which the latent prior distribution, p (z), is also intractable and defined with unnormalized energy function, by combining them with contrastive divergence learning (Hinton, 2002; Carreira-Perpinan & Hinton, 2005). We refer to this extension of LAEs as contrastive Langevin autoencoders (CLAEs). We experimentally show that our LAEs and CLAEs can generate sharper images than existing explicit generative models, such as VAEs. Moreover, we report their performance of unsupervised anomaly detection.

heading: 2 PRELIMINARIES

heading: 2.1 PROBLEM DEFINITION
text: Consider a probabilistic model with observations x, continuous latent variables z, and model parameters θ, as described by the probabilistic graphical model shown in Figure 1(A). Although the posterior distribution over the latent variable is proportional to the product of the prior and likelihood: p (z | x) = p (z) p (x | z) /p (x), this is intractable owing to the normalizing constant p (x) = ∫ p (z) p (x | z) dz. This study aims to approximate the posterior p (z | x) for all n ob-
servations x(1), . . .x(n) efficiently by obtaining samples from it.

heading: 2.2 LANGEVIN DYNAMICS
text: Langevin dynamics (LD) (Neal, 2011) is a sampling algorithm based on the following Langevin equation: dz = −∇zU (x, z) dt+ √
2β−1dB, (1) where U is a potential function that is Lipschitz continuous and satisfies an appropriate growth condition, β is an inverse temperature parameter, and B is a Brownian motion. This stochastic differential equation has exp (−βU (x, z)) / ∫ exp (−βU (x, z′)) dz′ as its equilibrium distribution. We set β = 1 and define the potential as follows to obtain the target posterior p (z | x) as its equilibrium:
U (x, z) = − log p (z)− log p (x | z) . (2)
Algorithm 1 Amortized Langevin dynamics (training time) φ← Initialize parameters Z(1), . . . ,Z(n) ← ∅ . Initialize sample sets for all n datapoints repeat φ← φ′ ∼ N ( φ′;φ− ηφ ∑n i=1∇φU ( x(i), z(i) = fz|x ( x(i);φ )) , 2ηφI
) Z(1), . . . ,Z(n) ← Z(1) ∪ { fφ ( x(1) )} , . . . ,Z(N) ∪ { fφ ( x(n) )} . Add samples
until convergence of parameters return Z(1), . . . ,Z(n)
Algorithm 2 Amortized Langevin dynamics (test time) z ← fz|x (x;φ∗) . Initialize a sample using a trained inference model Z← ∅ . Initialize a sample set repeat z ← z′ ∼ N (z′; z − η∇zU (x, z) , 2ηI) . Update the sample using traditional LD Z← Z ∪ {z} . Add samples
until convergence of parameters return Z
We can obtain samples from the posterior by simulating Eq. (1) using the Euler–Maruyama method (Kloeden & Platen, 2013) as follows:
z ← z′ ∼ N (z′; z − η∇zU (x, z) , 2ηI) , (3) where η is the step size for the discretization. When the step size is sufficiently small, the samples asymptotically move to the target posterior by repeating this sampling iteration. LD can be applied to any posterior inference problems for continuous latent variables provided the potential energy is differentiable on the latent space. However, to obtain samples of the posterior p (z | x) for all observations x(1), . . .x(n), we should perform an iteration on Eq. (3) per datapoint as shown in Figure 1(B1). It is inefficient particularly if the dataset is large. In the next section, we demonstrate a method that addresses the inefficiency by amortization.

heading: 3 AMORTIZED LANGEVIN DYNAMICS
text: In traditional LD, we perform MCMC iterations for each latent variable per datapoint. This is inefficient particularly if managing massive datasets. As an alternative to performing the simulation of latent dynamics directly, we define an inference model, fz|x, which is a differentiable mapping from observations into latent variables, and consider the dynamics of its parameter φ as follows:
dφ = − n∑ i=1 ∇φU ( x(i), z(i) = fz|x ( x(i);φ )) + √ 2dB. (4)
Because function fz|x outputs latent variables, the stochastic dynamics on the parameter space induces other dynamics on the latent space and is represented as the total gradient of fz|x:
dz(i) = dimφ∑ k=1 ∂z(i) ∂φk dφk
= − dimφ∑ k=1 ∂z(i) ∂φk ( ∂ ∂φk U ( x(i), fz|x ( x(i);φ ))) dt
− dimφ∑ k=1 ∂z(i) ∂φk  n∑ j=1,j 6=i ∂ ∂φk U ( x(j), fz|x ( x(j);φ )) dt+√2 dimφ∑ k=1 ∂z(i) ∂φk dB. (5)
MSE ESS (1 std.) MSCE
LD 0.00145 570.24 (25.25) 0.00136 ALD 0.00233 634.27 (40.94) 0.00151
Table 1: Quantitative comparison of the sample quality between traditional LD and our ALD. The mean squared error (MSE) between the true mean and the sample average, the effective sample size (ESS), and the Monte Carlo standard error (MSCE) are provided as evaluation metrics.
The first term of Eq. (5) approximates −∇z(i)U ( x(i), z(i) ) dt in Eq. (1), and the remaining terms introduce a random walk behavior to the dynamics as in the Brownian term of Eq. (1). For the simulation of Eq. (4), we use the Euler–Maruyama method, as in traditional LD:
φ← φ′ ∼ N ( φ′;φ− ηφ
n∑ i=1 ∇φU ( x(i), z(i) = fz|x ( x(i);φ )) , 2ηφI ) , (6)
where ηφ is the step size. Through the iterations, the posterior sampling is implicitly performed by collecting outputs of the inference model for all datapoints in the training set as described in Algorithm 1. When we perform inference for new test data, the trained inference model can be used as initialization of a MCMC method (e.g., traditional LD) as shown in Algorithm 2, because it is expected that the trained inference model can map data into the high-density area of the posteriors.
For minibatch training, we can substitute the minibatch statistics of m datapoints for the derivative for all n data in Eq. (6):
n∑ i=1 ∇φU ( x(i), z(i) = fz|x ( x(i);φ )) ≈ n m m∑ i=1 ∇φU ( x(i), z(i) = fz|x ( x(i);φ )) . (7)
In this case, we refer to the algorithm as stochastic gradient amortized Langevin dynamics (SGALD). SGALD enables us to sample from posteriors of a massive dataset with a constant computational cost. By contrast, performing traditional LD requires a linearly increasing cost with data size. For minibatch training of LD, adaptive preconditioning is known to be effective to improve convergence, which is referred to as preconditioned stochastic gradient Langevin dynamics (pSGLD) (Li et al., 2015). This preconditioning technique is also applicable to our SGALD, and we employ it throughout our experiments.
Figure 2 shows a simple example of sampling from a posterior distribution, where its prior and likelihood are defined using conjugate bivariate Gaussian distributions (see Appendix F for more details). ALD produces samples that match well the shape of the target distributions. The mean squared error (MSE) between the true mean and the sample average, the effective sample size (ESS), and the Monte Carlo standard error (MSCE) are provided for quantitative comparison, as shown in Table 1. It can be observed that the sample quality of ALD is competitive to standard LD, even though ALD does not perform direct update of samples in the latent space. Figure 3 shows the evolution of obtained sample values by traditional LD and our SGALD for posteriors defined by a
simple univariate conjugate Gaussian (see Appendix F.2 for more experimental details). SGALD’s samples converges much faster than traditional LD.
The advantage of our ALD over amortized variational inference (AVI) is the flexibility of posterior approximation. Figure 4 is an example where the likelihood p (x | z) is defined using a neural network, therefore the posterior p (z | x) is highly multimodal. AVI methods typically approximate posteriors using variational distributions, which have tractable density function (e.g., Gaussian distributions). Hence, their approximation power is limited by the choice of variational distribution family, and they often fail to approximate such complex posteriors. On the other hand, ALD can capture well such posteriors by obtaining samples. The results in other examples are summarized in Appendix F.

heading: 4 LANGEVIN AUTOENCODERS
text: Suppose we consider sampling the model parameter θ in addition to the local latent variables z from the joint posterior p (z, θ | x); then, we can ingenuously extend ALD to a full Bayesian approach by combining it with standard Langevin dynamics. Herein, the prior of the model parameter p (θ) is added to the potential U , and θ is sampled using standard LD or its minibatch version, stochastic gradient Langevin dynamics (SGLD) (Welling & Teh, 2011) as follows:
U (X,Z,θ) = − log p (θ)− n∑ i=1 log p ( z(i) | θ ) + log p ( x(i) | z(i),θ ) , (8)
θ ← θ′ ∼ N ( θ′;θ − ηθ∇θU ( X,Z = fz|x (X;φ) ) , 2ηθI ) , (9)
where ηθ is a step size. If we omit the Gaussian noise injection in Eq. (9), it corresponds to gradient descent for maximum a posteriori (MAP) estimation of θ; if we additionally use a flat prior for p (θ), it yields the maximum likelihood estimation (MLE). In this study, we assume a flat prior for p (θ) and omit the notation for simplicity.
Typically, the latent prior p (z | θ) and the likelihood p (x | z, θ) are defined as diagonal Gaussians: p (z | θ) = N ( z;µz,diag ( σ2z )) , p (x | z,θ) = N ( x;µx = fx|z (z;θ) , σ 2 xI ) , (10) where µz,µx and σ2z, σ 2 x are mean and variance parameters of Gaussian distributions respectively. fx|z (z;θ) is a mapping from the latent space to the observation space. The parameters of the latent prior µz and σ2z can be included to θ as learnable model parameters, or be fixed to manually decided values (e.g., µz = 0,σ2z = 1). For the observation variance σ 2 x, many existing works treat it as a hyperparameter. However, its tuning is difficult, and often requires heuristic techniques for proper training (Fu et al., 2019). Instead, here, we apply a different approach, in which the variance parameter is marginalized out, and the likelihood can be calculated only with the mean parameterµx (see Appendix B for further details). Furthermore, when the original data are quantized into discrete representation (e.g., 8-bit RGB images), it is not desirable to use continuous distributions, such as Gaussians, as likelihood functions. Thus, we should map quantized data into the continuous space in advance. This process is often referred to as dequantization (Salimans et al., 2017; Ho et al., 2019). Our ALD is also applicable as a dequantization method by formulating it as the posterior inference problem. Further detailed explanations are provided in Appendix C.
We can choose arbitrary differentiable functions for the generative model fx|z and the inference model fz|x. If neural networks are chosen for both, we achieve Langevin autoencoder (LAE), a new deep generative model within the auto-encoding scheme. The algorithm of LAEs is summarized in Algorithm 3 in the appendix.

heading: 5 CONTRASTIVE LANGEVIN AUTOENCODERS
text: Currently, we have dealt with the case where the latent prior distribution p (z | θ) is tractable. To enable more flexible modeling, here, we consider that an energy-based model (EBM) (Du & Mordatch,
2019; Pang et al., 2020; Han et al., 2020) is used for the latent prior as follows.
p (z | θ) = exp (−fz (z;θ)) Z (θ) , (11)
where fz (z;θ) is an energy function that maps the latent variable into a scalar value, and Z is a normalizing constant, i.e., Z (θ) = ∫ exp (−fz (z;θ)) dz. In this case, the derivative of the potential energy∇θU (X,Z,θ) is intractable owing to the normalizing constant. However, we can obtain the unbiased estimator of the derivative by obtaining samples from the prior p (z | θ).
∇θU (X,Z,θ) = n∑ i=1 ∇θfz ( z(i);θ ) +∇θ logZ (θ)−∇θ log p ( x(i) | z(i),θ ) (12)
= n∑ i=1 ∇θfz ( z(i);θ ) − Ep(z|θ) [∇θfz (z;θ)]−∇θ log p ( x(i) | z(i),θ ) . (13)
See Appendix D for the derivation. This algorithm used for the training of EBM is known as contrastive divergence learning (Hinton, 2002; Carreira-Perpinan & Hinton, 2005). To obtain samples from the latent prior, we can use standard LD as follows:
z ← z′ ∼ N (z′; z − ηz∇zfz (z;θ) , 2ηzI) , (14)
where ηz is a step size. However, we found that our amortized Langevin algorithm works well even for the case of sampling from an unconditional prior distribution. In the unconditional case, we prepare a sampler function fz|u (u;ψ) that maps its input u into the latent variable z. Here, the input vector u is fixed, because the prior distribution does not have conditional variables as in the posterior inference case except the model parameters θ. To run multiple MCMC chains in parallel, we prepare k fixed inputs u(1), . . . ,u(k), and update the function fz|u as follows.
ψ ← ψ′ ∼ N ( ψ − ηψ
k∑ i=1 ∇ψfz ( z(i) = fz|u ( u(i);ψ ) ;θ ) , 2ηψI ) , (15)
where ηψ is a step size. Typically, the fixed input vector is chosen from samples of a standard Gaussian distribution (i.e., u(1), . . . ,u(k) ∼ N (u; 0, I)). Figure 5 shows an example of sampling from a mixture of eight Gaussians using ALD. We can observe that ALD properly captures the multimodality of the true density and works well also in the unconditional case. For minibatch training, we can substitute the gradient for all k chains with the stochastic gradient of m minibatch chains:
k∑ i=1 ∇ψfz ( z(i) = fz|u ( u(i);ψ ) ;θ ) ≈ k m m∑ i=1 ∇ψfz ( z(i) = fz|u ( u(i);ψ ) ;θ ) . (16)
The advantage of using amortization in the unconditional case is that we can run massive chains in parallel with a constant computational cost using minibatch training. Here, we assume that the number of chains is equal to the number of datapoints for simplicity, i.e., k = n.
In summary, the encoder fz|x, the decoder fx|z, and the latent energy function fz are trained by minimizing the following loss functionL, whereas the latent sampler fz|u are trained by maximizing it, while stochastic noise of Brownian motion is injected in their update to avoid shrinking to MAP estimates (or MLE).
L (θ,φ,ψ) = n∑ i=1 fz ( fz|x ( x(i);φ ) ;θ ) − fz ( fz|u ( u(i);ψ ) ;θ )
− log p ( x(i) | z(i) = fz|x ( x(i);φ ) ,θ ) . (17)
Furthermore, when the energy function fz and the sampler fz|u are parameterized using neural networks, we refer to the whole model as contrastive Langevin autoencoders (CLAEs). At the
convergence of the CLAE’s training, the inference model fz|x and the model parameter θ match to the true posterior p (z | x,θ) and p (θ |X), respectively. Typically, when the number of datapoints gets infinity (i.e., n → ∞), the generative model p (x | θ) = ∫ p (z | θ) p (x | z,θ) dz converges to the data distribution pdata (x). Moreover, when the sampler function’s outputs corresponds to the marginal latent distribution Epdata(x) [p (z | x,θ)], the first and second terms on the right hand side in Eq. (17) are canceled out; therefore the energy function fz and the sampler function fz|u also converge to equilibrium.

heading: 6 RELATED WORKS
text: Amortized inference is well-investigated in the context of variational inference, and it is often referred to as amortized variational inference (AVI) (Rezende & Mohamed, 2015; Shu et al., 2018). The basic idea of AVI is to replace the optimization of the datapoint-wise variational parameters with the optimization of shared parameters across all datapoints by introducing an inference model that predicts latent variables from observations. Currently, the AVI is commonly used in fields, such as the training of generative models (Kingma & Welling, 2013), semi-supervised learning (Kingma et al., 2014), anomaly detection (An & Cho, 2015), machine translation (Zhang et al., 2016), and neural rendering (Eslami et al., 2018; Kumar et al., 2018). However, in the MCMC literature, there are few works on such amortization. (Han et al., 2016) uses traditional LD to obtain samples from posteriors for the training of deep latent variable models. Such Langevinbased algorithms for deep latent variable models are known as alternating back-propagation (ABP) and are widely applied in several fields (Xie et al., 2019; Zhang et al., 2020; Xing et al., 2018; Zhu et al., 2019). However, ABP requires datapoint-wise Langevin iterations, causing slow convergence. Moreover, when we perform inference for new data in test time, ABP requires to re-run MCMC iterations from randomly initialized samples. Although (Li et al., 2017; Hoffman, 2017) propose amortization methods for MCMC, they only amortize the cost of initialization in MCMC by using an inference model. Therefore, they do not completely remove datapoint-wise MCMC iterations.
Autoencoders (AEs) (Hinton & Salakhutdinov, 2006) are a special case of LAEs, wherein the Gaussian noise injection to the update of the inference model (encoder) is omitted in Eq. (6), and a flat prior is used for p (z | θ). When a different distribution is used as a latent prior, it is known as sparse autoencoders (SAEs) (Ng et al.). In these cases, the latent dynamics in Eq. (5) are dominated by gradient∇φU ; thereafter, the latent variables converge to MLE or MAP estimates, arg maxz p (z | x), or other stationary points. That is, AEs (and SAEs) can be regarded as a MLE (and MAP) algorithms for both the parameter θ and the latent variables z. Conversely, LAEs can be considered as a special case of (S)AEs, in which whole models are trained with SGLD instead of stochastic optimization methods like stochastic gradient decent (SGD).
Variational Autoencoders (VAEs) are based on AVI, wherein an inference model (encoder) is defined as a variational distribution q (z | x;φ) using a neural network. Its parameter φ is optimized by maximizing the evidence lower bound (ELBO) Eq(z|x;φ) [ log exp(−U(x,z))q(z|x;φ) ] = −Eq(z|x;φ) [U (x, z)]−H (q). There is a contrast between VAEs and LAEs relative to when stochastic noise is used. In VAEs, noise is used to sample from the variational distribution in the calculation of potential U , i.e., in forward calculation. However, in LAEs, noise is used for calculating gradient ∇φU , i.e., in backward calculation. This contrast characterizes their two different approaches to approximate posteriors: the optimization-based approach of VAEs and the sampling-based approach of LAEs. The advantage of LAEs over VAEs is that LAEs can flexibly approximate complex posteriors by obtaining samples, whereas VAEs’ approximation ability is limited by the choice of variational distribution q (z | x;φ) because it requires a tractable density. Although there are several considerations in the improvement of the approximation flexibility, these methods typically have constraints in terms of model architectures (e.g., invertibility and ease of Jacobian calculation in normalizing flows (Rezende & Mohamed, 2015; Kingma et al., 2016; Van Den Berg et al., 2018; Huang et al., 2018; Titsias & Ruiz, 2019)), or they incur more computational costs (e.g., MCMC sampling for the reverse conditional distribution in unbiased implicit variational inference (Titsias & Ruiz, 2019)).
Energy-based Models’ training is challenging, and many researchers have been studying methodology for its stable and practical training. A major challenge is that it requires MCMC sampling from EBMs, which is difficult to perform in high dimensional data space. Our CLAEs avoid this difficulty by defining the energy function in latent space rather than data space. A similar approach is taken by
(Pang et al., 2020), but they do not use amortization for the sampling of the latent prior and posterior as in CLAEs. On the other hand, (Han et al., 2020) proposes to learn VAEs and EBMs in latent space, but their energy function is defined for the joint distribution of the observation and the latent variable rather than the latent prior. For a more direct approach, in which EBMs are directly defined in observation space, (Du & Mordatch, 2019) uses spectral normalization (Miyato et al., 2018) for the energy function to smoothen its density, and stabilize its training. (Nijkamp et al., 2019) shows short-run MCMC is effective for the training of EBMs.
Generative adversarial networks (GANs) (Goodfellow et al., 2014) can be regarded as a special case of CLAEs by interpreting their discriminator and generator as energy function and sampler function, respectively (see Appendix E for further details).

heading: 7 IMAGE GENERATION
text: To demonstrate the applicability of our framework to the generative model training, we perform an experiment on image generation tasks using binarized MNIST (BMNIST), MNIST, SVHN, CIFAR10, and CelebA datasets. As baselines, we use VAEs and the ABP (Han et al., 2016), which is an algorithm to train deep latent variable models using LD without amortization. We also provide the performance of deep latent Gaussian models (DLGMs) (Hoffman, 2017), in which VAE-like encoders are used to initialize MCMC for posterior inference, as an alternative approach of amortization. For quantitative evaluation, we report the reconstruction error (RE) as an alternative of marginal likelihood p (x | θ), which cannot be calculated for LAEs and CLAEs. Because the RE cannot be a measure of sample quality, we provide the Fréchet Inception Distance (FID) (Heusel et al., 2017) for SVHN, CIFAR-10 and CelebA. The results are summarized in Table 2. We also provide the performance on denoising by trained VAEs, LAEs and CLAEs in Table 5 in the appendix. CLAEs consistently outperform the others, and LAEs also provide competitive results to the baselines.
In addition, LAEs’ training is faster than ABP due to amortization as shown in Figure 6. ABP cannot update the inference for datapoints that are not included in a minibatch, whereas LAEs can through the update of their inference model (encoder). This amortization enables scalable inference for large scale datasets, and accelerates the training of generative models. Qualitatively, images generated by LAEs and CLAEs are sharper than those of VAEs and ABPs as shown in Figure 7. Other examples are summarized in the appendix.

heading: 8 ANOMALY DETECTION
text: In addition to image generation, the potential energy in Eq. (2) can be useful for performing unsupervised anomaly detection, because it can be a measure of the probability density. For CLAEs, the potential energy itself cannot be calculated, because it includes the logarithm of the normalizing
constant of their latent prior logZ (θ). However, it can be ignored because it is constant with values of the observation x and the latent z. Therefore, we use the pseudo potential energy, Ũ , as a measure as follows.
Ũ (x, z) = − log p (x | z;θ)− fz (z;θ) . (18)
We test the efficacy of our LAEs and CLAEs for anomaly detection using MNIST. We assume that each digit class is normal and treat the remaining nine digits as anomaly examples. We use AEs and VAEs as baselines, and provide the area under the precision-recall curve (AUPRC) as the metric for comparing the models. We use the RE as a measure of anomaly for AEs and the negative ELBO for VAEs. From Table 3, it can be observed that our LAEs and CLAEs outperforms AEs and VAEs.

heading: 9 CONCLUSION
text: We proposed amortized Langevin dynamics (ALD), which is an efficient MCMC method for latent variable models. The ALD amortizes the cost of datapoint-wise iteration by using inference models. By experiments, we demonstrated that the ALD can accurately approximate posteriors. Using ALD, we derived a novel scheme of deep generative models called Langevin autoencoders (LAEs). LAEs are extended to a more general setting, where the latent prior is defined with an unnormalized energy function, and we refer to it as contrastive Langevin autoencoders (CLAEs). We demonstrated that our LAEs and CLAEs can generate sharp images, and they can be used for unsupervised anomaly detection. Furthermore, we investigated the relationship between our framework and existing models, and showed that traditional autoencoders (AEs) and generative adversarial networks (GANs) can be regarded as special cases of our LAEs and CLAEs. For future research on ALD, theories providing a solid proof of convergence, deriving a Metropolis-Hastings rejection step, and deriving algorithms based on more sophisticated Hamiltonian Monte Carlo approaches should be investigated.

heading: Numbers and Arrays
text: a A scalar (integer or real)
a A vector
I Identity matrix with dimensionality implied by context
diag(a) A square, diagonal matrix with diagonal entries given by a
a A scalar random variable
a A vector-valued random variable

heading: Sets and Graphs
text: A A set R The set of real numbers {0, 1} The set containing 0 and 1 {0, 1, . . . , n} The set of all integers between 0 and n [a, b] The real interval including a and b
(a, b] The real interval excluding a but including b

heading: Indexing
text: ai or a[i] Element i of vector a, with indexing starting at 1
ai or a[i] Element i of the random vector a
Calculus
dy dx Derivative of y with respect to x
∂y ∂x Partial derivative of y with respect to x ∇xy Gradient of y with respect to x∫ f(x)dx Definite integral over the entire domain of x

heading: Probability and Information Theory
text: P (a) A probability distribution over a discrete variable
p(a) A probability distribution over a continuous variable, or over a variable whose type has not been specified
a ∼ P Random variable a has distribution P Ex∼P [f(x)] or Ef(x) Expectation of f(x) with respect to P (x) H(x) or H(P ) Shannon entropy of the random variable x that has distri-
bution P
DKL(P‖Q) Kullback-Leibler divergence of P and Q N (x;µ,Σ) Gaussian distribution over x with mean µ and covariance
Σ
U(x;a, b) Uniform distribution over x with lower range a and upper range b
Gam(x;α, β) Gamma distribution over x with shape α and rate range β

heading: Functions
text: f : A→ B The function f with domain A and range B f(x;θ) A function of x parametrized by θ. (Sometimes we write
f(x) and omit the argument θ to lighten notation)
log x Natural logarithm of x
σ(x) Logistic sigmoid, 1 1 + exp(−x) Γ(z) Gamma function, ∫ ∞ 0 tz−1e−tdt bxc Integer part of x, i.e., bxc = max{n ∈ N | n ≤ x} 1condition is 1 if the condition is true, 0 otherwise

heading: B MARGINALIZING OUT OBSERVATION VARIANCE
text: When we use a diagonal Gaussian distribution for the likelihood function (i.e., p (x | z,θ) = N ( x;µx = fx|z (z;θ) , σ 2 xI ) ), we have to decide the parameter of observation variance σ2x. A simple and popular way is to manually choose the parameter in advance (e.g., σ2x = 1). However, it is difficult to choose a proper value, and it is desirable that the variance is calibrated for each datapoint. To address it, we use an alternative approach, in which the variance is marginalized out and the likelihood can be calculated only with the mean parameter. First, we define the precision parameter, which is the reciprocal of variance, i.e., λx = 1/σ2x. The precision is defined per datapoint, and shared across all dimension of the observation. When we define the prior distribution of the precision using an uninformative flat prior (e.g., p (λx) = Gam (λx; 0, 0)), the marginal distribution
Algorithm 3 Langevin Autoencoders θ,φ← Initialize parameters repeat θ ← θ′ ∼ N ( θ′;θ − ηθ∇θU ( X,Z = fz|x (X;φ) ,θ ) , 2ηθI
) φ← φ′ ∼ N ( φ′;φ− ηφ∇φU ( X,Z = fz|x (X;φ) ,θ ) , 2ηφI
) until convergence of parameters return θ,φ
Algorithm 4 Contrastive Langevin Autoencoders θ,φ,ψ ← Initialize parameters repeat θ ← θ′ ∼ N (θ′;θ − ηθ∇θL (θ,φ,ψ) , 2ηθI) φ← φ′ ∼ N (φ′;φ− ηφ∇φL (θ,φ,ψ) , 2ηφI) ψ ← ψ′ ∼ N (ψ′;ψ + ηψ∇ψL (θ,φ,ψ) , 2ηψI)
until convergence of parameters return θ,φ,ψ
will be simply the integral of the Gaussian likelihood over λx:∫ N ( x;µx = fx|z (z;θ) , λ −1 x I ) dλx (19)
= ∫ d∏ i=1 √ λx 2π exp ( −λx(xi − µx[i]) 2 2 ) dλx (20)
= ∫ ( λx 2π )d/2 exp ( − λx ∑d i=1(xi − µx[i])2 2 ) dλx (21)
= 2π−d/2 ( d∑ i=1 (xi − µx[i])2 )− d+22 Γ ( d+ 2 2 ) , (22)
where d is the dimensionality of x and Γ is a gamma function. This marginalized distribution is an improper distribution, whose integral over x diverges and does not correspond to 1. We refer to the distribution as the marginalized Gaussian distribution. Marginalized Gaussians can be widely used for mean parameter estimation of a Gaussian distribution, especially when its variance is unknown.

heading: C LANGEVIN DEQUANTIZATION
text: Many image datasets, such as MNIST and CIFAR10, are recordings of continuous signals quantized into discrete representations. For example, standard 8-bit RGB images of the resolution of H ×W are represented as {0, 1, . . . , 255}3×H×W . If we naively train a continuous density model to such discrete data, the model will converge to a degenerate solution that places all probability mass on discrete datapoints (Uria et al., 2013). A common solution to this problem is to first convert the discrete data distribution into a continuous distribution via a process called dequantization, and then model the resulting continuous distribution using the continuous density model. Here, we denote the random variable of original discretized data as x ∈ Nd (d = 3×H ×W ), dequantized continuous variable as x̃ ∈ Rd, and the continuous density model as p (x̃). The simplest way of dequantization is to add uniform noise to the discrete data, and deal with it as a continuous distribution, which we refer to as uniform dequantization:
x̃ ∼ U (x̃;x,x+ 1) . (23) However, this approach introduces flat step-wise regions into the data distribution, and it is unnatural and difficult to fit parametric continuous distributions. Moreover, the range of the dequantized data
is still bounded (i.e., x̃ ∈ (0, 256)d), therefore it is not desirable to fit the continuous density model defined for unbounded range, e.g., Gaussian distributions.
To investigate a more sophisticated approach, we first consider the quantization process, the inverse process of dequantization, in which the continuous data x ∈ Rd is discretized into {0, 1, . . . , 255}d. This process is represented as a conditional distribution P (x | x̃). For example, it is defined as follows:
P (x | x̃) = 1x=b256·σ(x̃)c. (24) In this definition, the continuous data is first compressed into (0, 256)d using the logistic sigmoid, then discretized into {0, 1, . . . , 255}d with its integer part. Although the quantization process could be formulated with another definition, we here discuss based on this formulation. When we have a density model of continuous data p (x̃), the dequantization process can be formulated as a posterior inference problem of p (x̃ | x) ∝ p (x̃)P (x | x̃). Although this posterior is typically intractable, we can obtain samples from it using our ALD algorithm in the same way as the posterior sampling of latent variable models. When we construct the inference model fx̃|x : {0, 1, . . . , 255}
d → Rd as follows, the likelihood will be constant, i.e., P ( x | x̃ = fx̃|x (x; ξ) ) = 1.
fx̃|x (x; ξ) = σ −1 ( x+ σ (g (x; ξ))
256
) , (25)
where g (x; ξ) is a mapping from discretized data x into real d-space. Therefore, the potential energy corresponds to the negative log likelihood.
U ( x, x̃ = fx̃|x (x; ξ) ) = − log p ( x̃ = fx̃|x (x; ξ) ) − (((( (((( ((( logP ( x | x̃ = fx̃|x (x; ξ) ) . (26)
The parameter of inference model fx̃|x is updated using our ALD algorithm as in Eq. (6).
ξ ← ξ′ ∼ N ( ξ′; ξ − ηξ
n∑ i=1 ∇ξU ( x̃(i) = fx̃|x ( x(i); ξ )) , 2ηξI ) , (27)
where ηξ is a step size.
When we use this Langevin dequantization for latent variable models like LAEs, the potential energy is rewritten as follows, and all parameters {θ, φ, ξ} are trained in an end-to-end fashion:
U ( X, X̃ = fx̃|x (X; ξ) ,Z = fz|x ( X̃;φ ) ,θ ) = − log p (θ)− n∑ i=1 log p ( z(i) = fz|x ( x̃(i);φ
)) + log p ( x̃(i) = fx̃|x ( x(i); ξ ) | z(i) = fz|x ( x̃(i);φ )) .
(28)
Figure 8 shows the comparison of SVHN data distributions of the top left corner pixel before and after dequantization. Before dequantization, the data are concentrated on discrete points. After dequantization, it can be observed that the distribution gets continuous enough to fit continuous density models. Furthermore, Figure 9 shows the comparison of generated MNIST samples by LAEs to see the effect of Langevin dequantization. The dequantization seems to affect the sharpness of generated samples.

heading: D DERIVATION OF EQ. (13)
text: ∇θ logZ (z) = 1
Z (θ) ∇θZ (θ) (29)
= 1
Z (θ)
∫ ∇θ exp (−fz (z;θ)) dz (30)
= − ∫
exp (−fz (z;θ)) Z (θ) ∇θfz (z;θ) dz (31)
= − ∫ p (z | θ)∇θfz (z;θ) dz (32)
= −Ez∼p(z|θ) [∇θfz (z;θ)] (33)

heading: E ADDITIONAL RELATED WORK
text: Generative Adversarial Networks (GANs) (Goodfellow et al., 2014) are similar to CLAEs in that both are trained by minimax game between two functions (i.e., the energy function and the sampler function in CLAEs; the discriminator and the generator in GANs). However, there are some differences between them. First, the minimax game is performed in the latent space in CLAEs, while it is performed in the observation space in GANs. In other words, the latent variable is identical to the observation (i.e., p (x | z) = 1x=z) in GANs2. Second, the loss function is slightly different. In GANs, the loss function is as follows:
LGAN (θ,ψ) = − n∑ i=1 logD ( x(i);θ ) + log ( 1−D ( G ( u(i);ψ ) ;θ )) , (34)
where G denotes the generator that maps its inputs u into the observation space, and D denotes the discriminator that maps from the observation space into (0, 1), andu(i) ∼ N (u; 0, I). The discriminator is trained to minimize this loss function, whereas the generator is trained to maximize it. The main difference to Eq. (17) is the second term. When we substitute it with− logD ( G ( u(i);ψ ) ;θ ) , it becomes more similar to Eq. (17). This modification is known as the− logD trick, and often used to stabilize the training of GAN’s generator (Goodfellow et al., 2014; Johnson & Zhang, 2018). In this formulation, the counter parts of the energy function and the sampler function are− logD (·;θ) and G (·;ψ), respectively. However, there is still a difference that the range of − logD (·;θ) is bounded within (0,∞), whereas the range of CLAE’s energy function is unbounded. Another difference between CLAEs and GANs is that the input vector of the sampler function is fixed through the training in CLAEs, whereas the input of the generator changes per iteration by sampling fromN (u; 0, I) in GANs. Furthermore, CLAEs are trained using noise injected gradient, whereas GANs are trained with a standard stochastic optimization method like SGD. In the training of CLAEs, as the number of inputs of the sampler function increases, the noise magnitude in Eq. (15) will relatively decreases. Therefore, in the infinite case (i.e., k → ∞), Eq. (15) corresponds to standard (stochastic) gradient descent. Thus, GANs can be interpreted as the infinite case of CLAEs with regard to their training of generators. In the infinite case, the generator (sampler) may converge to a solution where it always generates maximum density points rather than samples from the distribution defined by the energy function. This nature can cause mode collapsing, which is known as a major challenge of GAN’s training. In GANs, the discriminator is also trained with
2Note that the latent variable z is different from the input of GAN’s generators. Here, the input of the GAN’s generators is denoted as u for the analogy with CLAEs.
standard stochastic optimization, which corresponds to the MLE case where noise injection in Eq. (9) is omitted. Although there are some investigations to apply Bayesian approach to GANs (Saatci & Wilson, 2017; He et al., 2019), their discriminators are not defined as energy functions.
In summary, GANs with − logD trick can be considered as a special case of CLAEs, where the latent variable is identical to the observation (i.e., p (x | z) = 1x=z); the energy function and the sampler function are respectively defined as − logD (·;θ) and G (·;ψ); the number of inputs of the sampler function tends to infinity; and the model parameter θ is point-estimated with MLE.
Wasserstein GANs (WGANs) (Arjovsky et al., 2017) also has a loss function similar to CLAE’s:
LWGAN (θ,ψ) = − n∑ i=1 D ( x(i);θ ) −D ( G ( u(i);ψ ) ;θ ) , (35)
where D denotes the discriminator of WGANs that maps from the observation space into the real space R. In this case, the counter part of the energy function is −D (x;θ), although D has a constraint of 1-Lipschitz continuity, which the energy function of CLAE does not has.

heading: F EXPERIMENTAL SETTINGS
text: Unless we explicitly state otherwise, we use tanh activation instead of ReLU for all experiments, because it is desirable that the whole model is differentiable at all points when performing sampling algorithms based on Langevin dynamics, which require the differentiability of the potential energy. In fact, we found that our ALD experimentally performs better when using tanh rather than ReLU.

heading: F.1 CONJUGATE BIVARIATE GAUSSIAN EXAMPLE
text: In the experiment of conjugate bivariate Gaussian example in Section 3, we initially generate three synthetic data x(1),x(2),x(3), where each x(i) is sampled from a bivariate Gaussian distribution as follows:
p (z) = N (z;µz,Σz) , p (x | z) = N (x; z,Σx) .
In this experiment, we set µz =
[ 0
0
] , Σz = [ 1 0
0 1
] , and Σx = [ 0.7 0.6
0.7 0.8
] . We can
calculate the exact posterior as follows: p (z | x) = N ( z;µz|x,Σz|x ) ,
with µz|x = Σz|x ( Σ−1z µz + Σ −1 x x ) , Σz|x = ( Σ−1z + Σ −1 x )−1 .
In this experiment, we obtain 10,000 samples using ALD. We use four fully-connected layers of 128 units with tanh activation for the inference model and set the step size ηφ to 0.003.

heading: F.2 CONJUGATE UNIVARIATE GAUSSIAN EXAMPLE
text: In the experiment of conjugate univariate Gaussian example in Section 3, we initially generate 100 synthetic data x(1),x(2), . . . ,x(100), where each x(i) is sampled from a univariate Gaussian distribution as follows:
p (z) = N ( z;µz, σ 2 z ) , p (x | z) = N ( x; z, σ2x ) .
In this experiment, we set µz = 0, σ2z = 1, σ 2 x = 0.01. In this case, we can calculate the exact posterior as follows:
p (z | x) = N ( z;
1 1 σ2z + 1σ2x ( µz σ2z + x σ2x ) , ( 1 σ2z + 1 σ2x )−1) In this experiment, we obtain 20,000 samples using SGALD. We use four fully-connected layers of 128 units with tanh activation for the inference model and set the step size ηφ to 0.001. We set the batch size to 10.

heading: F.3 NEURAL LIKELIHOOD EXAMPLE
text: We perform an experiment with a complex posterior, wherein the likelihood is defined with a randomly initialized neural network fθ. Particularly, we parameterize fθ by four fully-connected layers of 128 units with ReLU activation and two dimensional outputs like p (x | z) = N ( fθ (z) , σ 2 xI ) . We initialize the weight and bias parameters with N (0, 0.2I) and N (0, 0.1I), respectively. In addition, we set the observation variance σx to 0.25. We used the same neural network architecture for the inference model fφ. Other settings are same as the previous conjugate Gaussian experiment.
The results are shown in Figure 10. The left three columns show the density visualizations of the ground truth or approximation posteriors of AVI methods; the right two columns show the visualizations of 2D histograms and samples obtained using ALD. For AVI method, we use two different models. One uses diagonal Gaussians, i.e., N ( µ (x;φ) ,diag ( σ2 (x;φ) )) , for the variational distribution, and the oher uses Gaussians with full covariance N (µ (x;φ) ,Σ (x;φ)). From the density visualization of GT, the true posterior is multimodal and skewed; this leads to the failure of the Gaussian AVI methods notwithstanding considering covariance. In contrast, the samples of ALD accurately capture such a complex distribution, because ALD does not need to assume any tractable distributions for approximating the true posteriors. The samples of ALD capture well the multimodal and skewed posterior, while Gaussian AVI methods fail it even when considering covariance.
F.4 IMAGE GENERATION
In the experiment of image generation, we resize the original image into 32 × 32 for all datasets. For MNIST, we pad original image with zeros to make the size 32 × 32. We use a diagonal Gaussian N ( µz,diag ( σ2z ))
as a latent prior for VAEs, ABP, DLGM and LAEs, and treat the parameter µz,σ2z as learnable parameters. We use a diagonal Gaussian for the approximate posterior of VAEs. The architecture of neural networks is summarized in Table 4. Conv k x s x p x c denotes a convolutional layer with k × k kernel, s × s stride, p × p padding, and c output channels. ConvTranpose k x s x p x c denotes a transposed convolutional layer with k×k kernel, s×s stride, p× p padding, and c output channels. Upsample denotes a nearest neighbor upsampling with scale factor of 2. Lineard is a fully connected layer of output dimension d. We apply tanh activation after each convolution, transposed convolution or linear layer except the last one. dx, dz and du are the
dimensionality of x, z and u respectively. dout is equal to dz for LAEs and CLAEs, and 2dz for VAEs. For all datasets, we set the minibatch size m and the step size (ηθ, ηφ, ηψ) to 1000, 0.01/n respectively, where n is a size of training set. We set dz = 32, du = 8 throughout the experiments. We use the same setting for the experiment of anomaly detection.

heading: VAE ABP

heading: VAE ABP

heading: VAE ABP

heading: VAE ABP

Review summaries:
In this paper, the author presented an advanced autoencoder framework LAE. Instead of element-wise MCMC, LAE collected samples from the posterior using the amortized Langevin dynamics of a potential energy distribution. In CLAE, an extended version of LAE, the author used an intractable energy function as the prior, and collected samples using its Langevin function. The author claims that LAE and CLAE are more efficient in large scale data and have better performance compared with traditional autoencoders and variational autoencoders.