N EGATIVE S AMPLING IN V ARIATIONAL A UTOENCODERS
Anonymous authors
Paper under double-blind review
A BSTRACT
We propose negative sampling as an approach to improve the notoriously bad out-of-distribution likelihood estimates of Variational Autoencoder models.
Our model pushes latent images of negative samples away from the prior.
When the source of negative samples is an auxiliary dataset, such a model can vastly improve on baselines when evaluated on OOD detection tasks.
Perhaps more surprisingly, we present a fully unsupervised version of employing negative sampling in VAEs: when the generator is trained in an adversarial manner, using the generator’s own outputs as negative samples can also signiﬁcantly improve the robustness of OOD likelihood estimates.
I NTRODUCTION
Learning semantically meaningful and useful representations for downstream tasks in an unsupervised manner is a big promise of generative modeling.
While a plethora of work demonstrates the effectiveness of deep generative models in this regard, recent work of Nalisnick et al. (2019a) and
Choi et al.
(2018) show that these models often fail even at a task that is supposed to be close to their original goal of learning densities.
Variational Autoencoders, PixelCNN and ﬂow-based models cannot distinguish common objects like cats and dogs from house numbers.
That is, when trained e.g., on CIFAR-10, the models consistently assign higher likelihoods to the elements of the SVHN test set than for the elements of the CIFAR-10 test set or even the elements of the CIFAR-10 train set.
As generative models are becoming more and more ubiquitous due to the massive progress in this area in recent years, it is of fundamental importance to understand these phenomena.
In this work we study Variational Autoencoder (VAE) models, and besides the likelihood, we also investigate to what extent the latent representation of a data point can be used to identify out-ofdistribution (OOD) samples (points that are not from the true data distribution).
For this purpose, we consider the KL divergence between the prior and the posterior distribution of a data point as a score to distinguish inliers and outliers.
Our contributions are summarized as follows:
• We demonstrate empirically that the extent of this notorious phenomenon — of bad outof-distribution likelihood estimates — present in VAEs largely depends on the observation model of the VAE.
In particular, our experiments show that it diminishes when a Gaussian noise model is considered (with a reasonably sized ﬁxed or learned variance) instead of a Bernoulli.
Meanwhile, when examining only the KL divergence between the prior and the posterior distributions in the latent space (instead of the full likelihood), the weak separating capability more consistently prevails between inliers and outliers.
• We propose negative sampling in Variational Autoencoders as an approach to alleviate the above weaknesses of the model family.
In this method, we introduce an additional prior distribution ¯ p ( z ) in the latent space, where the representations of negative samples are meant to be mapped by the inference model of the VAE machinery.
Negative samples can be obtained from an auxiliary dataset, or — to remain completely in the unsupervised setting — from a generative model trained on the ground truth distribution itself.
• We present empirical evidence that utilizing negative samples either from an auxiliary dataset or from an adversarial training scheme (using the adversarially trained generative model itself to provide the negative samples) signiﬁcantly and consistently improves the discriminative power of VAE models regarding out-of-distribution samples.
The general intuition behind our approach is that if the posterior distribution of each and every point is pulled towards the prior then it is rather natural to expect that the system will map outof-distribution samples close to the prior, as well.
This viewpoint suggests that providing negative signals throughout the learning process would be beneﬁcial to enhance the OOD discriminative power of the system.
Hendrycks et al.
(2019) demonstrate that utilizing auxiliary datasets as OOD examples (as a supervised signal) signiﬁcantly improves the performance of existing anomaly detection models on image and text data.
First, we study how this approach can be employed in the VAE setting.
Beyond that, we also propose a method which remains completely in the unsupervised learning paradigm (without using an auxiliary dataset for supervised signal).
The core idea of this unsupervised approach is to use a generative model to provide near-manifold negative samples throughout the training process for which the model is either implicitly or explicitly encouraged to give low likelihood estimates.
In our proposed method, these negative samples are obtained from the currently trained VAE model itself by utilizing the generated samples.
B ACKGROUND
The generative modeling task aims to model a ground truth data density p ∗ ( x ) on a space X by learning to generate samples from the corresponding distribution.
The learning is done in an unsupervised manner with sampled observables X
= { x ( i ) } Ni =1 as training points assumed to be drawn independently from p ∗ ( x ) , where N is the sample size.
In latent variable models, the observables are modeled together with hidden variables z on which a prior distribution p ( z ) is imposed.
The Variational Autoencoder (VAE) (Kingma & Welling, 2013) is a latent variable model that takes the maximum likelihood approach and maximizes a lower bound of the sample data log likelihood (cid:80)
Ni =1 log p θ ( x ( i ) ) , where θ are the generator parameters.
The utilized lower bound L ( θ, φ, x ( i ) ) (called the ELBO) comes from a variational approximation q φ ( z | x ( i ) ) of the intractable posterior p θ ( z | x ( i ) ) , where φ are the variational parameters:
[Equation]
In the VAE model the parametrized distributions p θ and q φ are modeled with neural networks and are trained jointly to maximize L with some variant of the SGD.
The prior is often chosen to be the multivariate standard normal distribution, and a Bernoulli or Gaussian noise model is considered in the observable space to deﬁne the likelihood.
To give likelihood estimates for unseen data points at test time, one can use the trained inference model q φ ( z | x ( i ) ) (also referred to as encoder) and generative model p θ ( x ( i ) | z ) (also referred to as decoder) to estimate the ELBO, thus giving a lower bound of the likelihood.
Throughout our paper, we are considering these ELBO estimates to measure the likelihood of data points.
N EGATIVE SAMPLING IN V ARIATIONAL A UTOENCODERS
To incorporate negative samples in the VAE training process, we introduce an additional prior distribution ¯ p ( z ) (called the negative prior ) on the latent variables z into which the representations of negative samples X
= { ¯x ( i ) } Mi =1 are meant to be mapped by the inference model.
This is encouraged in the training process by adding to the regular ELBO a new loss term: the KL divergence of the posterior distributions of negative samples to this negative prior.
Thus the joint loss function (to be minimized) is as follows:
[Equation]
Motivating our loss function The loss function deﬁned in equation 1 is still an upper bound of the [-latent variable y , which is a Bernoulli random variable with p = 1 / 2 , y = 
 meaning a choice from X and y = 1 meaning a choice from X , giving rise to-]negative data log likelihood (for the positive samples) as the added loss term is non-negative.
The new loss term explicitly imposes the discriminative task for the inference model: to distinguish inliers and outliers in the latent space.
With these two components, while still preserving the aim of maximizing the likelihood for inliers, we also expect the implicit behavior of reducing likelihood estimates for outliers.
For the outliers, a trained inference model produces latent representations that are close to the negative prior ¯ p ( z ) , thus, supposedly far from the prior p ( z ) . Also, the system is not encouraged to learn to generate from the vicinity of the negative prior, therefore not only the KL term of the likelihood, but the reconstruction part of a negative sample is affected when inferring the likelihood estimate of an outlier.
3. T HE CHOICE OF THE NEGATIVE PRIOR
One has numerous options to choose the positive and negative priors.
In this paper, we simply choose to use a standard normal for the positive prior, and a shifted standard normal for the negative prior.
With a rotationally symmetric posterior distribution, the distance between the two priors would be the only unspeciﬁed hyperparameter of such a model.
The assumption of diagonal covariance matrix posterior breaks rotational symmetry in principle, but our exploratory experiments have demonstrated that the magnitude of the shift is a more signiﬁcant modeling choice than the direction/sparsity of the shift. The role of D KL (¯ p ( z ) (cid:107) p ( z )) The magnitude of KL divergence between the negative and the positive prior plays an important role.
Larger D KL (¯ p ( z ) (cid:107) p ( z )) values result in larger D KL ( q φ ( z | ¯x ( i ) ) (cid:107) p ( z )) terms when evaluating the KL divergence term of the likelihood in a trained model, and also result in heavier weighted KL divergence terms during the optimization process.
E.g., with a farther shifted negative prior mean, a larger penalty is given for a wrong inference. The role of the latent dimension The above argument gives rise to an interesting side effect: increasing the latent dimension also increases D KL (¯ p ( z ) (cid:107) p ( z )) , thus resulting in a larger weight of the discriminative KL terms.
With the above simple choice of the shifted normal for the negative prior, our experiments already demonstrate the effectiveness of our proposed method.
One possible direction for further improvement would be to explore positive-negative prior pairs that better reﬂect the inlier-outlier structure of different datasets. We leave this investigation for future work. 3. S OURCE OF NEGATIVE SAMPLES Negative samples can also be obtained in different ways.
The task of our models is to generalize from the negative samples as much as possible to all possible out-of-distribution samples, so that they can push down likelihood estimates of those.
Depending on the source of negative samples, this generalization can be easier or harder.
We conduct experiments with several variants:
• samples from an auxiliary dataset, • the data with isotropic Gaussian noise added, • generated samples from the trained model itself, • generated samples utilizing an adversarial training scheme.
Negative samples that are very far from the data manifold do not facilitate generalization.
Noise added to data points is a simple and principled way to sample from the vicinity of the data manifold, but as we will see, it does not provide good generalization.
We argue that the reason for this is that discriminating between noisy and noiseless points is too easy for the encoder, so “semantically” the noisy versions are far from the data manifold.
In contrast, utilizing samples produced by a generative model (which could be the trained generative model itself) is a more suitable way to acquire nearmanifold negative samples, as we will experimentally demonstrate.
Why using generated data as negative samples could help? An immediate counterargument against utilizing generated samples as negatives could be the following: for a well-trained model, the generated images are indistinguishable from the ground truth images, so training the model to discriminate them is nonsensical. There are several reasons why such an unsupervised method could still work. First, in practice, a trained generative model is typically not perfect.
True data samples and generated samples can be distinguished even for fully trained models.
Second, even assuming a perfect generator at the end, during the training process, the generated samples might still help to guide the model toward an equilibrium that promotes a lower likelihood for OOD samples.
Moreover, when utilizing auxiliary datasets, we have to choose the auxiliary dataset (or multiple datasets) carefully to wedge in between the training set and a potential out-of-distribution data point, otherwise the weak separating capability could prevail.
In contrast, learning to discriminate the generated near-manifold examples from the ground truth data is a harder task, and could result in discriminating a more diverse set of potential out-of-distribution samples.
Our experiments also conﬁrm this.
When utilizing an adversarial training scheme (to be introduced later in this section), generated images not only facilitate our discriminative training procedure, but even achieve a higher level of generalization in the following sense: utilizing generated images improves on the baseline in all permutations of the roles for the grayscale datasets when considering discrimination in the latent space, while utilizing auxiliary datasets fails to achieve notable improvement in some cases.
(See the results in Table 1 in rows with AUC KL, and more details in the experiments section.)
In our preliminary experiments, we observed that in some examined cases, utilizing simply the generated images of the currently trained VAE model fail to provide a good signal for the discriminative task.
We achieved greater success when we augmented our model with adversarial training.
We hypothesize that the reason behind this is that obtained negative samples are richer in features and semantically more meaningful for the task.
(See Lee et al.
(2018) for an incarnation of this idea in the context of classiﬁcation and generative adversarial networks.)
The utilized adversarial training scheme When experimenting with generated samples as negatives, we utilize an adversarial training scheme where the generator (and only the generator) gets an additional gradient signal through the encoder to map the randomly generated images into the prior.
This is encouraged via the following additional loss term: [Equation] where ˆx ( i ) denotes a generated image obtained from the generator p θ ( ˆx ( i ) | z ) , where z is sampled from the prior p ( z ) . Together with the fact that the encoder also gets the generated images as negative samples, this results in an adversarial training procedure.
In this setup, the separate loss functions of the encoder and generator are:
[Equation]
Our utilized scheme is simple yet effective.
However, it is just one of the options.
Another choice would be to use a separate generative model with the speciﬁc task to provide negative samples.
We invite the research community to develop methods that can provide near-manifold examples that can facilitate the training of models with better OOD likelihood properties.
E XPERIMENTAL RESULTS
The general setup Our main concern is the discriminative power of VAE models regarding outof-distribution samples.
Following the conventions of related work, the general experimental setup in this section is as follows: we train a model on a train set of a dataset (e.g. train set of FashionMNIST) and then require the model to discriminate between the test set of the train dataset (e.g. test set of Fashion-MNIST) and the test set of an out-of-distribution dataset (e.g. test set of MNIST).
During the training phase, the models do not encounter examples from the OOD dataset, only at test time are they expected to able to distinguish between inliers and out-of-distribution samples.
Quantitative assessment For quantitative assessment, we use the threshold independent AUC metric calculated with the bits-per-dimension score (denoted by AUC BPD) and also with the KL divergence of the posterior distribution of a data point to the prior (denoted by AUC KL).
All reported numbers in this section are averages of 5 runs with standard deviations denoted in parentheses.
Datasets and experimental details We conduct experiments on two sets of datasets: color images of size 32x32 (CIFAR-10, SVHN, downscaled ImageNet) and grayscale images of size 28x (MNIST, Fashion-MNIST, Kuzushiji-MNIST, EMNIST-Letters).
For both cases, the (positive) prior is chosen to be standard normal, and the latent dimension is set to 100 for color images, and tofor grayscale images.
For a more detailed description of the utilized datasets, models, and training methodology, see Appendix A. We present
generated samples from the models in Appendix D. The samples demonstrate that the models preserve their generative capability even after adding the extra loss terms.
The choice of the negative prior In our experiments, the negative prior is a standard normal with a shifted mean. For color images it is centered at 25 · 1 , for grayscale images it is centered at 8 · 1 .
The magnitude of the shift is set based on a parameter sweep, which was evaluated using FashionMNIST and MNIST in the range of { 2, 4, 6, 8, 10 } for grayscale images, and using CIFAR-10 and
SVHN in the range of { 5, 10, 15, 20, 25, 30 } for color images.
After observing a clear trend, we have chosen the mode.
4. T HE EFFECTIVENESS OF NEGATIVE SAMPLING
To demonstrate the effectiveness of negative sampling we present two different sets of experiments: ﬁrst we incorporate negative samples from an auxiliary dataset, second we explore the use of adversarially generated negative samples. Almost perfect discrimination with auxiliary datasets The AUC scores in Table 1 show that using the auxiliary dataset as a source of negative samples in most cases proved to result in models that are capable to distinguish nearly perfectly between inliers and OOD samples.
This is also the case with color images, as experimental results in Table 2 show.
Failure modes with auxiliary datasets One can observe in Table 1 that — despite the above mentioned improvements — there are cases when utilizing an auxiliary dataset fails to improve on the OOD separating capability.
One example for this is when the inlier set is the EMNIST-Letters, the OOD test set is MNIST, and the utilized auxliary dataset is Fashion-MNIST (the results for this setup are in the last row of Table 1).
Showing skirts and boots for the model in training time does not help discriminating between letters and numbers at test time.
We hypothesize, that this as an example of the case, when the auxiliary dataset (regarding its features) does not wedge in between the inlier and the outlier test set. One possible way of improvement in this regard is to utilize several auxiliary datasets to present a more diverse set of examples for possible out-of-distribution samples in terms of features and semantic content.
Of course, the most beneﬁcial would be to train the system to distinguish between the inliers and every possible outlier data points instead of just learning to separate only one or a speciﬁc set of auxiliary dataset. This motivates our experiments utilizing generated samples as negative, with the idea that learning to separate from near-manifold examples could facilitate a better generalization in terms of OOD detection. Unsupervised method: improvements in all permutations in AUC KL In the case of the grayscale images, the last column in Table 1 shows the effectiveness of the fully unsupervised approach: regardless of whether using a Gaussian and a Bernoulli noise model 1 , the trained models
[Table]
AU [Table] achieve higher AUC KL scores than the baseline in all permutations .
The method also shows better AUC BPD scores than the baseline in most of the cases where the baseline fails (i.e., baselines with below 0.6 AUC BPD scores).
One can observe that when the train set is EMNIST-Letters and the OOD set is MNIST, the separation is still not achieved with this method either.
The possible reason behind this is that the visual features of these two datasets are very close to each other and it is a hard task to switch the default relation between them (note that when these two datasets switch roles, the likelihood estimates are correct).
Table 2 shows that in the case of color images, the unsupervised method also achieves notable discriminative performance improving on the baseline.
Random noise and additive isotropic Gaussian noise does not help We also investigated how the choice of negative samples inﬂuences the performance of the trained model.
We conducted further experiments with the following negative samples: 1) Kuzushiji-MNIST 2 (KMNIST) as an another auxiliary dataset, 2) random noise (in which we sample each pixel intensity from the uniform distribution on [0 , 1] — modeling a dataset with less structure), 3) with an additive isotropic Gaussian noise added to the inlier dataset.
The results in Table 3 show that utilizing either KMNIST or MNIST-Letters results in perfect separation of the inliers (Fashion-MNIST) and outliers (MNIST).
The weak results with random noise as negative samples show the signiﬁcance of the choice of negative samples.
We also experimented with utilizing the training set itself with an additive isotropic Gaussian noise as negative samples — a rather natural choice to provide near-manifold examples.
With an additive noise of σ = 0 . [Table]
[Table] [Table] the results for the AUC BPD metric is 0 .
44 (0 . 01) and 0 . 70 (0 . 09) for the AUC KL, showing weak discriminative power.
Examining the results for baseline VAE models (i
[Table] e., models without negative sampling) in Tableand Table [Table] we can observe great variability in the OOD detection performance
[Table]
The noise model greatly inﬂuences the phenomenon
The results suggest that the intriguing phenomenon in VAEs discussed by Nalisnick et al. (2019a) and Choi et al.
(2018) is highly dependent on modelling choices.
In the case of grayscale images, when changing the noise model from
Bernoulli to Gaussian (and otherwise remaining in the same experimental setting as Nalisnick et al.
(2019a))
, the issue of assigning higher likelihood estimates to OOD samples simply does not occur.
However, one can observe that discrimination between inliers and OOD samples based on the KL divergence between approximate posterior and prior is hardly feasible, with below- 1 / 2 AUC scores.
Meanwhile, with a Bernoulli noise model (also used in Nalisnick et al. (2019a)) both the likelihoodestimates and the KL divergences fail to discriminate.
The other results in the table (where models are trained on MNIST) conﬁrm the asymmetric behaviour already described by Nalisnick et al.
(2019a), that is, switching the roles of the inlier and outlier dataset affects the presence of the phenomenon.
Concerning experiments with color images, the corresponding rows of Table 2 again show the importance of modelling choices.
When CIFAR-10 is the training set, the phenomenon persistently occurs with Bernoulli, Gaussian and Quantized Gaussian noise models.
When SVHN is the training set, one can observe again a great variability in the AUC scores.
Our investigations are mostly inspired by and related to recent work on the evaluation of generative models on OOD data (Shafaei et al., 2018;
Nalisnick et al., 2019a; Choi et al., 2018; Hendrycks et al., 2019).
These works report that despite intuitive expectations, generative models — including but not limited to VAEs — consistently fail at distinguishing OOD data from the training data, yielding higher likelihood estimates on unseen OOD samples.
Nalisnick et al.
(2019a) examine the phenomenon in detail, focusing on ﬁnding the cause of it by analyzing ﬂow-based models that allow exact likelihood calculation.
Choi et al.
(2018) also notice the above-mentioned phenomenon, while they address the task of OOD sample detection with Generative Ensembles.
They decrease the weight of the KL divergence term in the ELBO (contrarily to what is promoted by the β -VAE loss function) to encourage a higher distortion penalty during training, resulting in a better performing model.
This observation also conﬁrms the importance of the noise model and the balance between the KL and the reconstruction term.
The ominous observation is presented also by Hendrycks et al.
, but they concentrate on improving the OOD data detection with Outlier Exposure.
Their work demonstrates that utilizing samples from an auxiliary data set as OOD examples, i.e., training models to discriminate between training and auxiliary samples, signiﬁcantly improves on the performance of existing OOD detection models on image and text data. However, they do not investigate the VAE model, and their general setup always requires an auxiliary dataset. Our work also sheds light on an issue with this approach: one should choose the auxiliary datasets carefully to obtain robust OOD detection.
Within the context of uncertainty estimation, Lee et al.
(2018) demonstrate that adversarially generated samples improve the conﬁdence of classiﬁers in their correct predictions.
They train a classiﬁer simultaneously with a GAN and require it to have lower conﬁdence on GAN samples.
For each class distribution, they tune the classiﬁer and GAN using samples from that OOD dataset.
Their method of utilizing generated samples of GANs is closest to our approach of using generated data points as negative samples, but Lee et al.
(2018) work within a classiﬁcation setting.
Nalisnick et al.
(2019b) propose a solution that can alleviate the issue without modifying existing generative models, but the issue they aim to address (distributional shift) is very different from the standard concerns of OOD sample detection.
Their model works by using the likelihood estimates coming from likelihood-based models as inputs to detect distributional shift, as opposed to using them as raw OOD sample detectors.
The model operates under the assumption that at evaluation time, samples come in batches, and thus can be the inputs of statistical tests differentiating between likelihood estimates for inlier datasets and likelihood estimates for evaluation datasets.
In the limiting case where the evaluation dataset has batch-size 1, the performance of this model can be compared meaningfully with our unsupervised models.
C ONCLUSIONS
In this work, we studied Variational Autoencoder models and investigated to what extent the latent representations of data points or the likelihood estimates given by the model can be used to identify out-of-distribution samples.
We demonstrated empirically that the extent of the notorious phenomenon of wrong out-of-distribution likelihood estimates present in VAEs is highly dependent on the observation model.
We introduced negative sampling as an approach to alleviate the above weakness of the Variational Autoencoder model family.
We presented empirical evidence that utilizing negative samples either from an auxiliary dataset or from an adversarial training scheme signiﬁcantly and consistently improves the discriminative power of VAE models regarding out-ofdistribution samples.
A E XPERIMENTAL DETAILS A. D ATASETS AND PREPROCESSING
We conduct experiments with two types of data set: color images of size 32x32 and grayscale images of size 28x28.
The utilized datasets are listed below.
Datasets of grayscale images of size 28x28:
• MNIST (LeCun et al., 2010): 28x28x1, 60.000 train + 10.000 test, 10 classes •
Fashion-MNIST
(Xiao et al., 2017): 28x28x1, 60.000 train + 10.000 test, 10 classes •
Kuzushiji-MNIST
(Clanuwat et al., 2018): 28x28x1, 60.000 train + 10.000 test, 10 classes •
EMNIST-Letters
(Cohen et al., 2017): 28x28x1, 60.000 train + 10.000 test, 10 classes
Datasets of color images of size 32x32:
• CIFAR-10 (Krizhevsky, 2009): 32x32x3 images, 50.000 train + 10.000 test, 10 classes • SVHN (cropped) (Netzer et al., 2011): 32x32x3 images, 73.257 train + 26,032 test (+ 531.131 extra unlabeled), 10 classes • Downsampled ImageNet (van den Oord et al., 2016): 32x32x3 images, 1.281.149 train + 49.999 validation, 1000 classes
We apply no preprocessing step other than normalizing the input images to [0 , 1] .
A. N ETWORK ARCHITECTURE AND TRAINING DETAILS
Details for grayscale images Following Nalisnick et al. (2019a), for grayscale images, we use the encoder architecture described in Rosca et al.
(2018) in appendix K table 4.
Also, as in Rosca et al.
(2018), all of the models are trained with the RMSProp optimizer with learning rate set to 10 − 4 .
We train the models for 100 epochs with mini-batch size of 50 .
We update the parameters of the encoder and decoder network in an alternating fashion.
Details for color images For color images we use a DCGAN-style CNN architecture with Conv– BatchNorm–ReLU modules for both the encoder and the decoder.
The size of the kernels are 4 × 4 , and the number of ﬁlters are 32 , 64 , 128 for the encoder; and 128 , 64 , 1 for the decoder.
All of the models are trained with the Adam optimizer ( β 1 = 0 .
9 , β 2 = 0 .
for 100 epochs with mini-batch size 50 .
The learning rate is set to 10 − 4 .
We update the parameters of the encoder and decoder network in an alternating fashion.
When generated images are used as negative samples, we employ spectral normalization (Miyato et al., 2018) for the convolutional weights of the encoder in order to stabilize and enhance the performance of the respective models, and in this case the models are trained for 300 epochs.
B R ECONSTRUCTION OF NEGATIVES
[Table]
C I NCREASING THE LATENT DIMENSION
[Table] D G ENERATED SAMPLES [Table] E P LOTS FROM THE LATENT SPACE
[Table]
VA E w it h n e g a ti v e s a m p li ng , n e g a ti v e : a d v e r s a r i
a l g e n e r a t e d
Epoch
Epoch
[Table]
Epoch
[Table]
[Figure]
[Table]
[Figure]
[Table]
[Figure]
[Table]
[Figure]
[Table]
Epoch
Epoch
Epoch
[Table]