realnvp
Implementation of RealNVP in Tensorflow
view repo
Unsupervised learning of probabilistic models is a central yet challenging problem in machine learning. Specifically, designing models with tractable learning, sampling, inference and evaluation is crucial in solving this task. We extend the space of such models using realvalued nonvolume preserving (real NVP) transformations, a set of powerful invertible and learnable transformations, resulting in an unsupervised learning algorithm with exact loglikelihood computation, exact sampling, exact inference of latent variables, and an interpretable latent space. We demonstrate its ability to model natural images on four datasets through sampling, loglikelihood evaluation and latent variable manipulations.
READ FULL TEXT VIEW PDF
Many sequencetosequence generation tasks, including machine translatio...
read it
Variational Autoencoders are powerful models for unsupervised learning.
...
read it
One of the most surprising and exciting discoveries in supervising learn...
read it
This paper presents the recurrent estimation of distributions (RED) for
...
read it
Although with progress in introducing auxiliary amortized inference mode...
read it
Energybased models (EBMs) are powerful probabilistic models, but suffer...
read it
Making sense of a dataset in an automatic and unsupervised fashion is a
...
read it
Implementation of RealNVP in Tensorflow
None
The domain of representation learning has undergone tremendous advances due to improved supervised learning techniques. However, unsupervised learning has the potential to leverage large pools of unlabeled data, and extend these advances to modalities that are otherwise impractical or impossible.
One principled approach to unsupervised learning is generative probabilistic modeling. Not only do generative probabilistic models have the ability to create novel content, they also have a wide range of reconstruction related applications including inpainting [61, 46, 59], denoising [3]
[71], and superresolution
[9].As data of interest are generally highdimensional and highly structured, the challenge in this domain is building models that are powerful enough to capture its complexity yet still trainable. We address this challenge by introducing realvalued nonvolume preserving (real NVP) transformations
, a tractable yet expressive approach to modeling highdimensional data.
This model can perform efficient and exact inference, sampling and logdensity estimation of data points. Moreover, the architecture presented in this paper enables exact and efficient
reconstruction of input images from the hierarchical features extracted by this model.
Data space  Latent space  

Inference 


Generation 

Substantial work on probabilistic generative models has focused on training models using maximum likelihood. One class of maximum likelihood models are those described by probabilistic undirected graphs, such as Restricted Boltzmann Machines [58] and Deep Boltzmann Machines [53]. These models are trained by taking advantage of the conditional independence property of their bipartite structure to allow efficient exact or approximate posterior inference on latent variables. However, because of the intractability of the associated marginal distribution over latent variables, their training, evaluation, and sampling procedures necessitate the use of approximations like Mean Field inference and Markov Chain Monte Carlo, whose convergence time for such complex models remains undetermined, often resulting in generation of highly correlated samples. Furthermore, these approximations can often hinder their performance [7].
Directed graphical models are instead defined in terms of an ancestral sampling procedure, which is appealing both for its conceptual and computational simplicity. They lack, however, the conditional independence structure of undirected models, making exact and approximate posterior inference on latent variables cumbersome [56]. Recent advances in stochastic variational inference [27] and amortized inference [13, 43, 35, 49], allowed efficient approximate inference and learning of deep directed graphical models by maximizing a variational lower bound on the loglikelihood [45]. In particular, the
variational autoencoder algorithm
[35, 49] simultaneously learns a generative network, that maps gaussian latent variables to samples , and a matched approximate inference network that maps samples to a semantically meaningful latent representation , by exploiting the reparametrization trick [68]. Its success in leveraging recent advances in backpropagation
[51, 39]in deep neural networks resulted in its adoption for several applications ranging from speech synthesis
[12] to language modeling [8]. Still, the approximation in the inference process limits its ability to learn high dimensional deep representations, motivating recent work in improving approximate inference [42, 48, 55, 63, 10, 59, 34].Such approximations can be avoided altogether by abstaining from using latent variables. Autoregressive models [18, 6, 37, 20]
can implement this strategy while typically retaining a great deal of flexibility. This class of algorithms tractably models the joint distribution by decomposing it into a product of conditionals using the
probability chain rule according to a fixed ordering over dimensions, simplifying loglikelihood evaluation and sampling. Recent work in this line of research has taken advantage of recent advances in recurrent networks [51], in particular longshort term memory [26], and residual networks [25, 24] in order to learn stateoftheart generative image models [61, 46] and language models [32]. The ordering of the dimensions, although often arbitrary, can be critical to the training of the model [66]. The sequential nature of this model limits its computational efficiency. For example, its sampling procedure is sequential and nonparallelizable, which can become cumbersome in applications like speech and music synthesis, or realtime rendering.. Additionally, there is no natural latent representation associated with autoregressive models, and they have not yet been shown to be useful for semisupervised learning.
Generative Adversarial Networks (GANs) [21] on the other hand can train any differentiable generative network by avoiding the maximum likelihood principle altogether. Instead, the generative network is associated with a discriminator network whose task is to distinguish between samples and real data. Rather than using an intractable loglikelihood, this discriminator network provides the training signal in an adversarial fashion. Successfully trained GAN models [21, 15, 47] can consistently generate sharp and realistically looking samples [38]. However, metrics that measure the diversity in the generated samples are currently intractable [62, 22, 30]. Additionally, instability in their training process [47]
requires careful hyperparameter tuning to avoid diverging behavior.
Training such a generative network that maps latent variable to a sample does not in theory require a discriminator network as in GANs, or approximate inference as in variational autoencoders. Indeed, if is bijective, it can be trained through maximum likelihood using the change of variable formula:
(1) 
This formula has been discussed in several papers including the maximum likelihood formulation of independent components analysis (ICA) [4, 28], gaussianization [14, 11] and deep density models [5, 50, 17, 3]. As the existence proof of nonlinear ICA solutions [29] suggests, autoregressive models can be seen as tractable instance of maximum likelihood nonlinear ICA, where the residual corresponds to the independent components. However, naive application of the change of variable formula produces models which are computationally expensive and poorly conditioned, and so large scale models of this type have not entered general use.
In this paper, we will tackle the problem of learning highly nonlinear models in highdimensional continuous spaces through maximum likelihood. In order to optimize the loglikelihood, we introduce a more flexible class of architectures that enables the computation of loglikelihood on continuous data using the change of variable formula. Building on our previous work in [17], we define a powerful class of bijective functions which enable exact and tractable density evaluation and exact and tractable inference. Moreover, the resulting cost function does not to rely on a fixed form reconstruction cost such as square error [38, 47]
, and generates sharper samples as a result. Also, this flexibility helps us leverage recent advances in batch normalization
[31] and residual networks [24, 25] to define a very deep multiscale architecture with multiple levels of abstraction.Given an observed data variable
, a simple prior probability distribution
on a latent variable , and a bijection (with ), the change of variable formula defines a model distribution on by(2)  
(3) 
where is the Jacobian of at .
Exact samples from the resulting distribution can be generated by using the inverse transform sampling rule [16]. A sample is drawn in the latent space, and its inverse image generates a sample in the original space. Computing the density on a point is accomplished by computing the density of its image and multiplying by the associated Jacobian determinant . See also Figure 1. Exact and efficient inference enables the accurate and fast evaluation of the model.
of the input vector conditioned on the remaining part of the input vector
. Because of its simple nature, this transformation is both easily invertible and possesses a tractable determinant. However, the conditional nature of this transformation, captured by the functions and , significantly increase the flexibility of this otherwise weak function. The forward and inverse propagation operations have identical computational cost.Computing the Jacobian of functions with highdimensional domain and codomain and computing the determinants of large matrices are in general computationally very expensive. This combined with the restriction to bijective functions makes Equation 2 appear impractical for modeling arbitrary distributions.
As shown however in [17], by careful design of the function , a bijective model can be learned which is both tractable and extremely flexible. As computing the Jacobian determinant of the transformation is crucial to effectively train using this principle, this work exploits the simple observation that the determinant of a triangular matrix can be efficiently computed as the product of its diagonal terms.
We will build a flexible and tractable bijective function by stacking a sequence of simple bijections. In each simple bijection, part of the input vector is updated using a function which is simple to invert, but which depends on the remainder of the input vector in a complex way. We refer to each of these simple bijections as an affine coupling layer. Given a dimensional input and , the output of an affine coupling layer follows the equations
(4)  
(5) 
where and stand for scale and translation, and are functions from , and is the Hadamard product or elementwise product (see Figure 2(a)).
The Jacobian of this transformation is
(6) 
where is the diagonal matrix whose diagonal elements correspond to the vector . Given the observation that this Jacobian is triangular, we can efficiently compute its determinant as . Since computing the Jacobian determinant of the coupling layer operation does not involve computing the Jacobian of or
, those functions can be arbitrarily complex. We will make them deep convolutional neural networks. Note that the hidden layers of
and can have more features than their input and output layers.Another interesting property of these coupling layers in the context of defining probabilistic models is their invertibility. Indeed, computing the inverse is no more complex than the forward propagation (see Figure 2(b)),
(7)  
(8) 
meaning that sampling is as efficient as inference for this model. Note again that computing the inverse of the coupling layer does not require computing the inverse of or , so these functions can be arbitrarily complex and difficult to invert.
Partitioning can be implemented using a binary mask , and using the functional form for ,
(9) 
We use two partitionings that exploit the local correlation structure of images: spatial checkerboard patterns, and channelwise masking (see Figure 3). The spatial checkerboard pattern mask has value
where the sum of spatial coordinates is odd, and
otherwise. The channelwise mask is for the first half of the channel dimensions and for the second half. For the models presented here, both and are rectified convolutional networks.Although coupling layers can be powerful, their forward transformation leaves some components unchanged. This difficulty can be overcome by composing coupling layers in an alternating pattern, such that the components that are left unchanged in one coupling layer are updated in the next (see Figure 4(a)).
The Jacobian determinant of the resulting function remains tractable, relying on the fact that
(10)  
(11) 
Similarly, its inverse can be computed easily as
(12) 
We implement a multiscale architecture using a squeezing operation: for each channel, it divides the image into subsquares of shape , then reshapes them into subsquares of shape . The squeezing operation transforms an tensor into an tensor (see Figure 3), effectively trading spatial size for number of channels.
At each scale, we combine several operations into a sequence: we first apply three coupling layers with alternating checkerboard masks, then perform a squeezing operation, and finally apply three more coupling layers with alternating channelwise masking. The channelwise masking is chosen so that the resulting partitioning is not redundant with the previous checkerboard masking (see Figure 3). For the final scale, we only apply four coupling layers with alternating checkerboard masks.
Propagating a dimensional vector through all the coupling layers would be cumbersome, in terms of computational and memory cost, and in terms of the number of parameters that would need to be trained. For this reason we follow the design choice of [57] and factor out half of the dimensions at regular intervals (see Equation 14). We can define this operation recursively (see Figure 4(b)),
(13)  
(14)  
(15)  
(16) 
In our experiments, we use this operation for . The sequence of couplingsqueezingcoupling operations described above is performed per layer when computing (Equation 14). At each layer, as the spatial resolution is reduced, the number of hidden layer features in and is doubled. All variables which have been factored out at different scales are concatenated to obtain the final transformed output (Equation 16).
As a consequence, the model must Gaussianize units which are factored out at a finer scale (in an earlier layer) before those which are factored out at a coarser scale (in a later layer). This results in the definition of intermediary levels of representation [53, 49] corresponding to more local, finegrained features as shown in Appendix D.
Moreover, Gaussianizing and factoring out units in earlier layers has the practical benefit of distributing the loss function throughout the network, following the philosophy similar to guiding intermediate layers using intermediate classifiers
[40]. It also reduces significantly the amount of computation and memory used by the model, allowing us to train larger models.To further improve the propagation of training signal, we use deep residual networks [24, 25] with batch normalization [31] and weight normalization [2, 54] in and . As described in Appendix E we introduce and use a novel variant of batch normalization which is based on a running average over recent minibatches, and is thus more robust when training with very small minibatches.
We also use apply batch normalization to the whole coupling layer output. The effects of batch normalization are easily included in the Jacobian computation, since it acts as a linear rescaling on each dimension. That is, given the estimated batch statistics and , the rescaling function
(17) 
has a Jacobian determinant
(18) 
This form of batch normalization can be seen as similar to reward normalization in deep reinforcement learning
[44, 65].We found that the use of this technique not only allowed training with a deeper stack of coupling layers, but also alleviated the instability problem that practitioners often encounter when training conditional distributions with a scale parameter through a gradientbased approach.
The algorithm described in Equation 2 shows how to learn distributions on unbounded space. In general, the data of interest have bounded magnitude. For examples, the pixel values of an image typically lie in after application of the recommended jittering procedure [64, 62]. In order to reduce the impact of boundary effects, we instead model the density of , where is picked here as . We take into account this transformation when computing loglikelihood and bits per dimension. We also augment the CIFAR10, CelebA and LSUN datasets during training to also include horizontal flips of the training examples.
We train our model on four natural image datasets: CIFAR10 [36], Imagenet [52],
Largescale Scene Understanding (LSUN)
[70], CelebFaces Attributes (CelebA) [41]. More specifically, we train on the downsampled to andversions of Imagenet
[46]. For the LSUN dataset, we train on the bedroom, tower and church outdoor categories. The procedure for LSUN is the same as in [47]: we downsample the image so that the smallest side is pixels and take random crops of . For CelebA, we use the same procedure as in [38]: we take an approximately central crop of then resize it to .We use the multiscale architecture described in Section 3.6 and use deep convolutional residual networks in the coupling layers with rectifier nonlinearity and skipconnections as suggested by [46]. To compute the scaling functions , we use a hyperbolic tangent function multiplied by a learned scale, whereas the translation function has an affine output. Our multiscale architecture is repeated recursively until the input of the last recursion is a tensor. For datasets of images of size , we use residual blocks with hidden feature maps for the first coupling layers with checkerboard masking. Only residual blocks are used for images of size . We use a batch size of . For CIFAR10, we use residual blocks, feature maps, and downscale only once. We optimize with ADAM [33] with default hyperparameters and use an regularization on the weight scale parameters with coefficient .
We set the prior to be an isotropic unit norm Gaussian. However, any distribution could be used for , including distributions that are also learned during training, such as from an autoregressive model, or (with slight modifications to the training objective) a variational autoencoder.
Dataset  PixelRNN [46]  Real NVP  Conv DRAW [22]  IAFVAE [34] 
CIFAR10  3.00  3.49  < 3.59  < 3.28 
Imagenet ()  3.86 (3.83)  4.28 (4.26)  < 4.40 (4.35)  
Imagenet ()  3.63 (3.57)  3.98 (3.75)  < 4.10 (4.04)  
LSUN (bedroom)  2.72 (2.70)  
LSUN (tower)  2.81 (2.78)  
LSUN (church outdoor)  3.08 (2.94)  
CelebA  3.02 (2.97) 
We show in Table 1 that the number of bits per dimension, while not improving over the Pixel RNN [46] baseline, is competitive with other generative methods. As we notice that our performance increases with the number of parameters, larger models are likely to further improve performance. For CelebA and LSUN, the bits per dimension for the validation set was decreasing throughout training, so little overfitting is expected.
We show in Figure 5 samples generated from the model with training examples from the dataset for comparison. As mentioned in [62, 22], maximum likelihood is a principle that values diversity over sample quality in a limited capacity setting. As a result, our model outputs sometimes highly improbable samples as we can notice especially on CelebA. As opposed to variational autoencoders, the samples generated from our model look not only globally coherent but also sharp. Our hypothesis is that as opposed to these models, real NVP does not rely on fixed form reconstruction cost like an norm which tends to reward capturing low frequency components more heavily than high frequency components. Unlike autoregressive models, sampling from our model is done very efficiently as it is parallelized over input dimensions. On Imagenet and LSUN, our model seems to have captured well the notion of background/foreground and lighting interactions such as luminosity and consistent light source direction for reflectance and shadows.
We also illustrate the smooth semantically consistent meaning of our latent variables. In the latent space, we define a manifold based on four validation examples , , , , and parametrized by two parameters and by,
(19) 
We project the resulting manifold back into the data space by computing . Results are shown Figure 6
. We observe that the model seems to have organized the latent space with a notion of meaning that goes well beyond pixel space interpolation. More visualization are shown in the Appendix. To further test whether the latent space has a consistent semantic interpretation, we trained a classconditional model on CelebA, and found that the learned representation had a consistent semantic meaning across class labels (see Appendix
F).In this paper, we have defined a class of invertible functions with tractable Jacobian determinant, enabling exact and tractable loglikelihood evaluation, inference, and sampling. We have shown that this class of generative model achieves competitive performances, both in terms of sample quality and loglikelihood. Many avenues exist to further improve the functional form of the transformations, for instance by exploiting the latest advances in dilated convolutions [69] and residual networks architectures [60].
This paper presented a technique bridging the gap between autoregressive models, variational autoencoders, and generative adversarial networks. Like autoregressive models, it allows tractable and exact loglikelihood evaluation for training. It allows however a much more flexible functional form, similar to that in the generative model of variational autoencoders. This allows for fast and exact sampling from the model distribution. Like GANs, and unlike variational autoencoders, our technique does not require the use of a fixed form reconstruction cost, and instead defines a cost in terms of higher level features, generating sharper images. Finally, unlike both variational autoencoders and GANs, our technique is able to learn a semantically meaningful latent space which is as high dimensional as the input space. This may make the algorithm particularly well suited to semisupervised learning tasks, as we hope to explore in future work.
Real NVP generative models can additionally be conditioned on additional variables (for instance class labels) to create a structured output algorithm. More so, as the resulting class of invertible transformations can be treated as a probability distribution in a modular way, it can also be used to improve upon other probabilistic models like autoregressive models and variational autoencoders. For variational autoencoders, these transformations could be used both to enable a more flexible reconstruction cost [38] and a more flexible stochastic inference distribution [48]. Probabilistic models in general can also benefit from batch normalization techniques as applied in this paper.
The definition of powerful and trainable invertible functions can also benefit domains other than generative unsupervised learning. For example, in reinforcement learning, these invertible functions can help extend the set of functions for which an operation is tractable for continuous Qlearning [23] or find representation where local linear Gaussian approximations are more appropriate [67].
The authors thank the developers of Tensorflow
[1]. We thank Sherry Moore, David Andersen and Jon Shlens for their help in implementing the model. We thank Aäron van den Oord, Yann Dauphin, Kyle Kastner, Chelsea Finn, Maithra Raghu, David WardeFarley, Daniel Jiwoong Im and Oriol Vinyals for fruitful discussions. Finally, we thank Ben Poole, Rafal Jozefowicz and George Dahl for their input on a draft of the paper.Proceedings of International Conference on Computer Vision (ICCV)
, December 2015.International conference on artificial intelligence and statistics
, pages 448–455, 2009.Inspired by the texture generation work by [19, 61] and extrapolation test with DCGAN [47], we also evaluate the statistics captured by our model by generating images twice or ten times as large as present in the dataset. As we can observe in the following figures, our model seems to successfully create a “texture” representation of the dataset while maintaining a spatial smoothness through the image. Our convolutional architecture is only aware of the position of considered pixel through edge effects in convolutions, therefore our model is similar to a stationary process. This also explains why these samples are more consistent in LSUN, where the training data was obtained using random crops.
As in [22], we further try to grasp the semantic of our learned layers latent variables by doing ablation tests. We infer the latent variables and resample the lowest levels of latent variables from a standard gaussian, increasing the highest level affected by this resampling. As we can see in the following figures, the semantic of our latent space seems to be more on a graphic level rather than higher level concept. Although the heavy use of convolution improves learning by exploiting image prior knowledge, it is also likely to be responsible for this limitation.
We further experimented with batch normalization by using a weighted average of a moving average of the layer statistics and the current batch batch statistics ,
(20)  
(21) 
where is the momentum. When using , we only propagate gradient through the current batch statistics . We observe that using this lag helps the model train with very small minibatches.
We used batch normalization with a moving average for our results on CIFAR10.
Additionally, we exploit the attribute information in CelebA to build a conditional model, i.e. the invertible function from image to latent variable uses the labels in to define its parameters. In order to observe the information stored in the latent variables, we choose to encode a batch of images with their original attribute and decode them using a new set of attributes , build by shuffling the original attributes inside the batch. We obtain the new images .
We observe that, although the faces are changed as to respect the new attributes, several properties remain unchanged like position and background.
Comments
There are no comments yet.