\section{Introduction}

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 \citet{nalisnick2018deep} and \citet{choi2018waic} 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 flow-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 to 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-of-distribution (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:
\begin{itemize}
    \item We demonstrate empirically that the extent of this notorious phenomenon --- of bad out-of-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 fixed 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.
    \item We propose \textit{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 $\pzbar$ 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.
    \item 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) significantly and consistently improves the discriminative power of VAE models regarding out-of-distribution samples.
\end{itemize}

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 out-of-distribution samples close to the prior, as well. This viewpoint suggests that providing negative signals throughout the learning process would be beneficial to enhance the OOD discriminative power of the system.

\citet{hendrycks2018deep} demonstrate that utilizing auxiliary datasets as OOD examples (as a supervised signal) significantly 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.
\section{Background}

The generative modeling task aims to model a ground truth data density $p^{*}({\bm{x}})$ on a space $\mathcal{X}$ by learning to generate samples from the corresponding distribution. The learning is done in an unsupervised manner with sampled observables $\mathbf{X} = \{\vxi\}_{i=1}^{N}$ as training points assumed to be drawn independently from $p^{*}({\bm{x}})$, where $N$ is the sample size. In latent variable models, the observables are modeled together with hidden variables ${\bm{z}}$ on which a prior distribution $p({\bm{z}})$ is imposed.

The Variational Autoencoder (VAE) \citep{diederik2014auto} is a latent variable model that takes the maximum likelihood approach and maximizes a lower bound of the sample data log likelihood $\sum_{i=1}^{N}$ $\log p_{\theta}(\vxi)$, where $\theta$ are the generator parameters. The utilized lower bound $\mathcal{L}(\theta,\phi,\vxi)$ (called the ELBO) comes from a variational approximation $\qpzxi$ of the intractable posterior $\ptzxi$, where $\phi$ are the variational parameters:
\begin{multline*}
\log\ptxi = \log\int p_{\theta}(\vxi|{\bm{z}}) p({\bm{z}}) \geq \\
\geq \underbrace{\mathbb{E}_{\qpzxi} \log\ptxiz}_{\text{Reconstruction term}} - \underbrace{\kl{\qpzxi}{p({\bm{z}})}}_{\text{KL divergence term}} \triangleq \mathcal{L}(\theta,\phi,\vxi).
\end{multline*}

In the VAE model the parametrized distributions $\pt$ and $\qp$ are modeled with neural networks and are trained jointly to maximize $\mathcal{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 define the likelihood.

To give likelihood estimates for unseen data points at test time, one can use the trained inference model $\qpzxi$ (also referred to as encoder) and generative model $\ptxiz$ (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.
\section{Negative sampling in Variational Autoencoders}

To incorporate negative samples in the VAE training process, we introduce an additional prior distribution $\pzbar$ (called the \emph{negative prior}) on the latent variables ${\bm{z}}$ into which the representations of \emph{negative samples} $\mathbf{\overline{X}} = \{\vxineg\}_{i=1}^{M}$ 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:

\begin{multline}
\label{eq:loss}
L(\theta,\phi,\vxi,\vxineg) \triangleq -\mathcal{L}(\theta,\phi,\vxi) + \kl{\qpzxnegi}{\pvzbar} = \\
= \underbrace{-\mathbb{E}_{\qpzxi} \log\ptxiz + \kl{\qpzxi}{p({\bm{z}})}}_{-1 \cdot \text{ ELBO for }\vxi} + \underbrace{\kl{\qpzxnegi}{\pvzbar}}_{\text{KL term for negative sample}\;\vxineg}.
\end{multline}

\paragraph{Motivating our loss function} The loss function defined in equation \ref{eq:loss} is still an upper bound of the 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 $\bar{p}({\bm{z}})$, thus, supposedly far from the prior $p({\bm{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.

\subsection{The 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 unspecified hyperparameter of such a model. The assumption of diagonal covariance posterior breaks rotational symmetry in principle, but our exploratory experiments have demonstrated that the magnitude of the shift is a more significant modeling choice than the direction/sparsity of the shift.

\paragraph{The role of $\kl{\bar{p}({\bm{z}})}{p({\bm{z}})}$} The magnitude of KL divergence between the negative and the positive prior plays an important role. Larger $\kl{\bar{p}({\bm{z}})}{p({\bm{z}})}$ values result in larger $\kl{\qpzxnegi}{p({\bm{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.

\paragraph{The role of the latent dimension} The above argument gives rise to an interesting side effect: increasing the latent dimension also increases $\kl{\bar{p}({\bm{z}})}{p({\bm{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 reflect the inlier-outlier structure of different datasets. We leave this investigation for future work.

\subsection{Source 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:
\begin{itemize}
    \item samples from an auxiliary dataset,
    \item the data with isotropic Gaussian noise added,
    \item generated samples from the trained model itself,
    \item generated samples utilizing an adversarial training scheme.
\end{itemize}

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 near-manifold negative samples, as we will experimentally demonstrate.

\paragraph{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 confirm 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 \emph{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~\ref{table:grayscale_summary} 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 \citet{lee2017training} for an incarnation of this idea in the context of classification and generative adversarial networks.)

\paragraph{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:
$$\kl{\qpzxgeni}{p({\bm{z}})},$$
where $\vxigen$ denotes a generated image obtained from the generator $\ptxhatiz$, where ${\bm{z}}$ is sampled from the prior $p({\bm{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:
$$L^{adv}_{enc}(\theta,\phi,\vxi,\vxigen)= L(\theta,\phi,\vxi,\vxigen), $$
$$L^{adv}_{gen}(\theta,\phi,\vxi,\vxigen)= L(\theta,\phi,\vxi,\vxigen)  + \kl{\qpzxgeni}{p({\bm{z}})}.$$

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 specific 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.
\section{Experimental results}

\paragraph{The general setup} Our main concern is
the discriminative power of VAE models regarding out-of-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 Fashion-MNIST) 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.

\paragraph{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. The bits per dimension values employed and reported in the paper are $\log_2$-likelihood per dimension values directly calculated from the ELBO, not to be compared with bits per dimension values reported when modeling discrete $\{0, \dots, 255\}$ pixel intensities as defined, e.g. in \cite{Theis2015d}.

\paragraph{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 28x28 (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 to 10 for grayscale images.
For a more detailed description of the utilized datasets, models, and training methodology, see Appendix~\ref{appendix:model_details}. We present generated samples from the models in Appendix~\ref{appendix:generated_samples}. The samples demonstrate that the models preserve their generative capability even after adding the extra loss terms.

\paragraph{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 \cdot \mathbf{1}$, for grayscale images it is centered at $8 \cdot \mathbf{1}$. The magnitude of the shift is set based on a parameter sweep, which was evaluated using Fashion-MNIST 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.

\subsection{The effectiveness of negative sampling}

To demonstrate the effectiveness of negative sampling we present two different sets of experiments: first we incorporate negative samples from an auxiliary dataset, second we explore the use of adversarially generated negative samples.

\setlength{\tabcolsep}{0.3em}
\begin{table}[ht]
\caption{Comparing the out-of-distribution discriminative power of baseline VAE models and VAE models with negative sampling on grayscale images. Numbers for all permutations with the different possible roles of the three datasets (MINST, Fashion-MNIST and EMNIST-Letters) are reported. When an auxiliary dataset is utilized, the auxiliary dataset is the one out of the three that is not utilized neither as inlier nor for OOD testing purposes.}
\begin{center}
\begin{tabular}{ c  c  c  c  c  c  c }
  \toprule
  & \multirow{2}{*}{Inlier} & \multirow{2}{*}{OOD} & \multirow{2}{*}{Noise model}& Baseline VAE & Negative: & Negative: \\
  & & & & (no negative) & auxiliary & adversarial \\
  \midrule
  \parbox[t]{4mm}{\multirow{12}{*}{\rotatebox[origin=c]{90}{AUC BPD}}}
  &Fashion-MNIST & MNIST & Bernoulli & $0.46$ $(0.05)$ & $1.00$ $(0.00)$ & $0.70$ $(0.13)$ \\
  &Fashion-MNIST & MNIST& Gaussian & $0.98$ $(0.00)$ & $1.00$ $(0.00)$ & $0.80$ $(0.04)$ \\
  & Fashion-MNIST & Letters & Bernoulli & $0.61$ $(0.01)$ & $0.99$ $(0.00)$ & $0.78$ $(0.07)$ \\
  & Fashion-MNIST & Letters & Gaussian & $0.97$ $(0.00)$ & $1.00$ $(0.00)$ & $0.85$ $(0.04)$ \\
  \cmidrule{2-7}
  &MNIST & Fashion-MNIST & Bernoulli & $1.00$ $(0.00)$ & $1.00$ $(0.00)$ & $1.00$ $(0.00)$ \\
  &MNIST & Fashion-MNIST & Gaussian & $0.97$ $(0.00)$ & $1.00$ $(0.00)$ & $0.98$ $(0.01)$ \\
  &MNIST & Letters & Bernoulli & $0.99$ $(0.00)$ & $0.99$ $(0.00)$ & $0.98$ $(0.00)$ \\
  &MNIST & Letters & Gaussian & $0.78$ $(0.14)$ & $0.93$ $(0.08)$ & $0.79$ $(0.04)$ \\
  \cmidrule{2-7}
  &Letters & Fashion-MNIST & Bernoulli & $0.98.$ $(0.00)$ & $0.98$ $(0.00)$ & $0.99$ $(0.00)$ \\
  &Letters & Fashion-MNIST & Gaussian & $0.80$ $(0.07)$ & $0.76$ $(0.08)$ & $0.93$ $(0.04)$ \\
  &Letters&  MNIST & Bernoulli & $0.58$ $(0.02)$ & $0.58$ $(0.02)$ & $0.73$ $(0.07)$ \\
  &Letters & MNIST & Gaussian & $0.67$ $(0.17)$ & $0.58$ $(0.20)$ & $0.65$ $(0.04)$ \\
  \midrule
  \parbox[t]{4mm}{\multirow{12}{*}{\rotatebox[origin=c]{90}{AUC KL}}}
  &Fashion-MNIST & MNIST & Bernoulli & $0.61$ $(0.09)$ & $1.00$ $(0.00)$ & $0.88$ $(0.07)$ \\
  &Fashion-MNIST & MNIST & Gaussian & $0.26$ $(0.03)$ & $1.00$ $(0.00)$ & $0.74$ $(0.05)$ \\
  &Fashion-MNIST & Letters & Bernoulli & $0.68$ $(0.07)$ & $1.00$ $(0.00)$ & $0.84$ $(0.04)$ \\
  &Fashion-MNIST & Letters & Gaussian & $0.38$ $(0.04)$ & $0.99$ $(0.00)$ & $0.79$ $(0.05)$ \\
  \cmidrule{2-7}
  &MNIST & Fashion-MNIST & Bernoulli & $0.73$ $(0.14)$ & $1.00$ $(0.00)$ & $0.94$ $(0.10)$ \\
  &MNIST & Fashion-MNIST & Gaussian & $0.71$ $(0.04)$ & $1.00$ $(0.00)$ & $0.98$ $(0.01)$ \\
  &MNIST & Letters & Bernoulli & $0.64$ $(0.03)$ & $0.76$ $(0.03)$ & $0.89$ $(0.02)$ \\
  &MNIST & Letters & Gaussian & $0.54$ $(0.07)$ & $0.75$ $(0.08)$ & $0.74$ $(0.04)$ \\
  \cmidrule{2-7}
  &Letters & Fashion-MNIST & Bernoulli & $0.66$ $(0.14)$ & $0.54$ $(0.09)$ & $0.98$ $(0.00)$ \\
  &Letters & Fashion-MNIST & Gaussian & $0.54$ $(0.10)$ & $0.49$ $(0.23)$ & $0.91$ $(0.05)$ \\
  &Letters & MNIST & Bernoulli & $0.37$ $(0.05)$ & $0.45$ $(0.03)$ & $0.75$ $(0.06)$ \\
  &Letters & MNIST & Gaussian & $0.36$ $(0.08)$ & $0.43$ $(0.10)$ & $0.64$ $(0.04)$ \\
  \bottomrule
\end{tabular}
\end{center}
\label{table:grayscale_summary}
\end{table}

\paragraph{Almost perfect discrimination with auxiliary datasets} The AUC scores in Table~\ref{table:grayscale_summary} 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~\ref{table:color_summary} show.

\paragraph{Failure modes with auxiliary datasets} One can observe in Table~\ref{table:grayscale_summary} 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~\ref{table:grayscale_summary}). 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 beneficial 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 specific 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.

\paragraph{Unsupervised method: improvements in all permutations in AUC KL} In the case of the grayscale images, the last column in Table~\ref{table:grayscale_summary} shows the effectiveness of the fully unsupervised approach: regardless of whether using a Gaussian and a Bernoulli noise model\footnote{Even though the Bernoulli noise model might not be a particularly good choice for modeling grayscale or color images, here we follow the literature when considering it as a baseline.}, the trained models achieve higher AUC KL scores than the baseline in \emph{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~\ref{table:color_summary} shows that in the case of color images, the unsupervised method also achieves notable discriminative performance
improving on the baseline.

\setlength{\tabcolsep}{0.3em}
\begin{table}[t]
\caption{Comparing baseline VAEs and VAEs with negative sampling with Bernoulli, Gaussian, and Quantized Gaussian (Q. Gaussian) noise models on color image datasets.}
\begin{center}
\begin{tabular}{ c  c  c  c  c  c  c }
  \toprule
  & \multirow{2}{*}{Inlier} & \multirow{2}{*}{OOD} & \multirow{2}{*}{Noise model} & Baseline VAE & Negative: & Negative: \\
  & & & & (no negative) & auxiliary & adversarial \\
  \midrule
  \parbox[t]{4mm}{\multirow{5}{*}{\rotatebox[origin=c]{90}{AUC BPD}}}
  &CIFAR-10 & SVHN & Bernoulli &  $0.59$ $(0.00)$ & $0.90$ $(0.05)$ &$0.81$ $(0.04)$ \\
  &CIFAR-10 & SVHN & Gaussian & $0.25$ $(0.02)$ & $0.93$ $(0.01)$ & $0.84$ $(0.03)$ \\
  &CIFAR-10 & SVHN & Q. Gaussian & $0.19$ $(0.00)$ &$0.92$ $(0.03)$ & $0.82$ $(0.03)$\\
  \cmidrule{2-7}
  &SVHN  & CIFAR-10 & Bernoulli &  $0.51$ $(0.00)$ & $1.00$ $(0.00)$& $0.70$ $(0.03)$\\
  &SVHN & CIFAR-10 & Gaussian & $0.92$ $(0.00)$ &
  $1.00$ $(0.00)$ & $0.75$ $(0.11)$\\
  \midrule
  \parbox[t]{4mm}{\multirow{5}{*}{\rotatebox[origin=c]{90}{AUC KL}}}
  &CIFAR-10 & SVHN & Bernoulli & $0.29$ $(0.00)$ & $0.90$ $(0.06)$ & $0.81$ $(0.04)$  \\
  &CIFAR-10 & SVHN & Gaussian &$0.25$ $(0.01)$ & $0.93$ $(0.01)$ &  $0.84$ $(0.03)$ \\
  &CIFAR-10 & SVHN & Q. Gaussian & $0.28$ $(0.01)$ & $0.92$ $(0.03)$  &  $0.82$ $(0.03)$ \\
  \cmidrule{2-7}
  &SVHN  & CIFAR-10 & Bernoulli &  $0.87$ $(0.00)$ &
  $1.00$ $(0.00)$ & $0.70$ $(0.03)$\\
  &SVHN & CIFAR-10 & Gaussian &$0.74$ $(0.01)$ &
  $1.00$ $(0.00)$  & $0.74$ $(0.11)$\\
  \bottomrule
\end{tabular}
\end{center}
\label{table:color_summary}
\end{table}

\paragraph{Random noise and additive isotropic Gaussian noise does not help} We also investigated how the choice of negative samples influences the performance of the trained model. We conducted further experiments with the following negative samples: 1) Kuzushiji-MNIST\footnote{EMNIST-Letters, Kuzushiji-MNIST and Fashion-MNIST are datasets that can be utilized as drop-in replacements for MNIST.} (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~\ref{table:negative_sample_sources} 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 significance 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 $\sigma = 0.25$, 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.

\begin{table}[t]
\vspace{0.5cm}
\caption{Comparing baseline model and negative sampling with different sources for negatives. Columns correspond to different sources for negative samples. Results for the baseline (i.e., VAE without negative sampling) are indicated again in the first column for comparison. Samples from the different data sets are also depicted in the last row to show their general visual characteristics.}
\begin{center}
\begin{tabular}{ l c c c c c c c   }
  \toprule
  Inlier & & OOD & \multicolumn{3}{c}{Auxiliary dataset as negative} & Negative: \\
  Fashion-MNIST & & MNIST & Random & KMNIST & Letters & Adversarial \\
  \midrule
  AUC BPD  &  & $0.46$ $(0.05)$ & $0.47$ $(0.05)$ & $1.00$ $(0.00)$ & $1.00$ $(0.00)$ &$0.70$ $(0.13)$  \\
  AUC KL   &  & $0.61$ $(0.09)$ & $0.56$ $(0.08)$ & $1.00$ $(0.00)$ & $1.00$ $(0.00)$ &$0.88$ $(0.07)$ \\
  \cmidrule{1-7}
  Test BPD & &
  $0.44$ $(0.00)$ & $0.44$ $(0.00)$ & $0.44$ $(0.00)$ & $0.44$ $(0.00)$ &$0.68$ $(0.13)$ \\
  OOD BPD  & &
  $0.45$ $(0.12)$ & $0.46$ $(0.06)$ & $1.63$ $(0.11)$ & $2.09$ $(0.28)$ &$10^{18}$ $(10^{19})$ \\
  \midrule
  \multicolumn{1}{l}{
  \includegraphics[width=1.7cm]{assets/fashion_mnist.png}} &  & \includegraphics[width=1.7cm]{assets/mnist.png} & \includegraphics[width=1.7cm]{assets/random_uniform.png} &
  \includegraphics[width=1.7cm]{assets/kmnist.png} &
  \includegraphics[width=1.7cm]{assets/letters.png} &
  \includegraphics[width=1.7cm]{assets/adversarial_negative.png} \\
  \bottomrule
\end{tabular}
\end{center}
\label{table:negative_sample_sources}
\end{table}

\newpage
\subsection{The effect of the noise model}

Examining the results for baseline VAE models (i.e., models without negative sampling) in Table~\ref{table:grayscale_summary} and Table~\ref{table:color_summary}, we can observe great variability in the OOD detection performance.

\paragraph{The noise model greatly influences the phenomenon} The results suggest that the intriguing phenomenon in VAEs discussed by \citet{nalisnick2018deep} and \citet{choi2018waic} 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 \citet{nalisnick2018deep}), 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 \cite{nalisnick2018deep}) both the likelihood-estimates and the KL divergences fail to discriminate. The other results in the table (where models are trained on MNIST) confirm the asymmetric behaviour already described by \citet{nalisnick2018deep}, 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~\ref{table:color_summary} 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.
\section{Related Work}

Our investigations are mostly inspired by and related to recent work on the evaluation of generative models on OOD data \citep{shafaei2018does, nalisnick2018deep, choi2018waic, hendrycks2018deep}. 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.

\citet{nalisnick2018deep} examine the phenomenon in detail, focusing on finding the cause of it by analyzing flow-based models that allow exact likelihood calculation. \citet{choi2018waic} 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 $\beta$-VAE loss function) to encourage a higher distortion penalty during training, resulting in a better performing model. This observation also confirms the importance of the noise model and the balance between the KL and the reconstruction term.

The ominous observation is presented also by \citet{hendrycks2018deep}, 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, significantly 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, \citet{lee2017training} demonstrate that adversarially generated samples improve the confidence of classifiers in their correct predictions. They train a classifier simultaneously with a GAN and require it to have lower confidence on GAN samples. For each class distribution, they tune the classifier 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 \citet{lee2017training} work within a classification setting.

\citet{nalisnick2019detecting} 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.
\section{Conclusions}

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 significantly and consistently improves the discriminative power of VAE models regarding out-of-distribution samples.
\section{Experimental details}
\label{appendix:model_details}
\subsection{Datasets 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.

\paragraph{Datasets of grayscale images of size 28x28:}
\begin{itemize}
    \item MNIST \citep{lecun2010mnist}: 28x28x1, 60.000 train + 10.000 test, 10 classes
    \item Fashion-MNIST \citep{xiao2017fashion}: 28x28x1, 60.000 train + 10.000 test, 10 classes
    \item Kuzushiji-MNIST \citep{clanuwat2018deep}: 28x28x1, 60.000 train + 10.000 test, 10 classes
    \item EMNIST-Letters \citep{cohen_afshar_tapson_schaik_2017}: 28x28x1, 60.000 train + 10.000 test, 10 classes
\end{itemize}

\paragraph{Datasets of color images of size 32x32:}
\begin{itemize}
    \item CIFAR-10 \citep{Krizhevsky09learningmultiple}: 32x32x3 images, 50.000 train + 10.000 test, 10 classes
    \item SVHN (cropped) \citep{netzer2011reading}: 32x32x3 images, 73.257 train + 26,032 test (+ 531.131 extra unlabeled), 10 classes
    \item Downsampled ImageNet \citep{DBLP:journals/corr/OordKK16}: 32x32x3 images, 1.281.149 train + 49.999 validation, 1000 classes
\end{itemize}

We apply no preprocessing step other than normalizing the input images to $[0,1]$.

\subsection{Network architecture and training details}

\paragraph{Details for grayscale images}

Following \citet{nalisnick2018deep}, for grayscale images, we use the encoder architecture described in \cite{rosca2018distribution} in appendix K table 4. Also, as in \cite{rosca2018distribution}, 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.

\paragraph{Details for color images}

For color images we use a DCGAN-style CNN architecture with Conv--Batch\-Norm--ReLU modules for both the encoder and the decoder. The size of the kernels are $4 \times 4$, and the number of filters are $32, 64, 128, 256$ for the encoder; and $128, 64, 32$ for the decoder. In the encoder, the result of the last convolutional layer is flattened, and then two dense layers produce the parameters of the posterior distribution. In the decoder, from the latent vector a dense layer produces a $4096$ dimensional vector which is then reshaped to a $256 \times 4 \times 4$ sized tensor before the convolutions, and at the top, a convolution with $3$ filters for the three RGB color channels produces the output. (Note that in this paper we experiment with a fixed variance or a globally learnt scalar variance in the case of Gaussian noise model.) All of the models are trained with the Adam optimizer ($\beta_1=0.9, \beta_2=0.999$) 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 \citep{Miyato2018SpectralNorm} 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.
\section{Reconstruction of negatives}
\label{appendix:reconstruction_of_negatives}

\begin{table}[ht]
\small
\caption{Comparing the discriminative power of VAE models with negative sampling with Bernoulli noise model, with Fashion-MNIST and MNIST as inlier and OOD datasets, respectively, when reconstruction of negative samples from  EMNIST-Letters is also taken into account, with $\alpha$ weight. One can observe that the models are able to reconstruct OOD samples, while the discriminative power does not diminish. The same was observed when using Gaussian noise model.}
\begin{center}
\begin{tabular}{ l c c c c c c c c c c c}
  \toprule
  $\alpha$ & $0.0$ & $0.1$ & $0.2$ & $0.3$ & $0.4$ &
  $0.5$ & $0.6$ & $0.7$ & $0.8$ & $0.9$ & $1.0$  \\
  \midrule
  AUC BPD &
  $0.999$ &
  $0.999$ &
  $0.998$ &
  $0.998$ &
  $0.998$ &
  $0.998$ &
  $0.998$ &
  $0.997$ &
  $0.997$ &
  $0.998$ &
  $0.99$ $(0.01)$ \\

  Test BPD &

  $0.440$ &
  $0.439$ &
  $0.441$ &
  $0.442$ &
  $0.444$ &
  $0.443$ &
  $0.445$ &
  $0.445$ &
  $0.446$ &
  $0.447$ &
  $0.45$ $(0.00)$ \\

  OOD BPD &

  $2.196$ &
  $0.983$ &
  $0.919$ &
  $0.0923$ &
  $0.925$ &
  $0.912$ &
  $0.921$ &
  $0.916$ &
  $0.921$ &
  $0.931$ &
  $0.86$ $(0.04)$ \\
  \midrule

  AUC KL &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.000$ &
  $1.00$ $(0.00)$ \\

  Test KL &
  $15.12$ &
  $15.73$ &
  $15.26$ &
  $15.85$ &
  $16.38$ &
  $16.53$ &
  $16.59$ &
  $16.66$ &
  $16.94$ &
  $17.04$ &
  $15.86$ $(0.23)$ \\

  OOD KL &
  $325.1$ &
  $325.4$ &
  $325.5$ &
  $340.2$ &
  $350.4$ &
  $343.0$ &
  $354.3$ &
  $350.1$ &
  $353.6$ &
  $363.2$ &
  $321.9$ $(19.9)$ \\
  \bottomrule
\end{tabular}
\end{center}
\label{table:reconstruct_negatives_summary}
\end{table}

\begin{table}[!ht]
\vspace{-0.6cm}
\small
\begin{center}
\begin{tabular}{ l l c c c c c c }
  \toprule
  $\alpha$ & Inlier & & OOD  & Generated & Reconstructed & Reconstructed & Reconstructed \\
  & Fashion-MNIST & & MNIST & samples & train samples  & test samples & OOD samples \\
  \midrule
  \vspace{-0.3cm}
  \multirow{9}{*}{$0.0$ } &&&&\\
  & \multicolumn{1}{l}{
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_original.png}} &  & \includegraphics[width=1.6cm]{assets/mnist.png} & \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1885_generated.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1885_reconstructed.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1885_test_a.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1885_test_b.png} \\
  \cmidrule{1-8}
  \vspace{-0.3cm}
  \multirow{9}{*}{$0.1$ } &&&&\\
   & \multicolumn{1}{l}{
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_original.png}} &  & \includegraphics[width=1.6cm]{assets/mnist.png} & \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1887_generated.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1887_reconstructed.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1887_test_a.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1887_test_b.png} \\
  \cmidrule{1-8}
  \vspace{-0.3cm}
  \multirow{9}{*}{$0.5$ } &&&&\\
   & \multicolumn{1}{l}{
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_original.png}} &  & \includegraphics[width=1.6cm]{assets/mnist.png} & \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_generated.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_reconstructed.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_test_a.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_test_b.png} \\
  \cmidrule{1-8}
  \vspace{-0.3cm}
  \multirow{9}{*}{$1.0$ } &&&&\\
  & \multicolumn{1}{l}{
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1895_original.png}} &  & \includegraphics[width=1.6cm]{assets/mnist.png} & \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1983_generated.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1983_reconstructed.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1983_test_a.png} &
  \includegraphics[width=1.6cm]{assets/reconstruction_of_negatives/ON-1983_test_b.png} \\
  \bottomrule
\end{tabular}
\end{center}
\label{table:reconstruct_negatives}
\end{table}

\section{Increasing the latent dimension}
\label{appendix:increasing_latent_dimension}

\begin{table}[!ht]
\caption{Comparing the discriminative performance of baseline VAE models with different latent dimension sizes, trained on Fashion-MNIST, and MNIST used as OOD dataset, with Bernoulli noise model. First column corresponds to our default setup. Reconstructed training samples and generated samples from the models are also provided. Our exploratory experiments indicate that simply increasing the latent dimension size does not help to overcome the problem of assigning higher likelihoods to OOD data, and even the generative performance is diminishing.}
\begin{center}
\begin{tabular}{ l c c c c c }
  \toprule
  Latent dimension & 10 & 50 & 100& 250 & 500 \\
  \midrule
  AUC BPD      & $0.46$ $(0.05)$ & $0.35$  & $0.35$  & $0.39$ & $0.34$  \\
  AUC KL       & $0.61$ $(0.09)$ & $0.76$& $0.73$ & $0.42$  &$0.64$  \\
  \cmidrule{1-6}
  Test BPD &
  $0.44$ $(0.00)$ & $0.45$  & $0.46$ & $0.47$  &$0.48$ \\
  OOD BPD
  & $0.45$ $(0.12)$ & $0.38$  & $0.39$  & $0.42$ &$0.41$ \\
  \cmidrule{1-6}
  Test KL & $15.61$ $(0.55)$ & $16.06$  & $16.14$ & $18.02$  &$17.65$ \\
  OOD KL  & $31.91$ $(16.89)$ & $19.37$  & $18.70$  & $17.43$ &$18.75$ \\
  \midrule
  \vspace{-0.3cm}
  \multirow{8}{*}{Reconstruction } &&&&\\
  &\includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=10_ON-2143_reconstructed.png}& \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=50_ON-2137_reconstructed.png}& \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=100_ON-2327_reconstructed.png}& \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=250_ON-2126_reconstructed.png} &   \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=500_ON-2109_reconstructed.png} \\
   \cmidrule{1-6}
   \vspace{-0.3cm}
   \multirow{8}{*}{Generated samples } &&&&\\
   &\includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=10_ON-2143_generated.png}& \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=50_ON-2137_generated.png}& \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=100_ON-2327_generated.png}& \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=250_ON-2126_generated.png} &
   \includegraphics[width=1.7cm]{assets/increasing_latent_dimension/ld=500_ON-2109_generated.png}\\
   \bottomrule
\end{tabular}
\end{center}
\label{table:increasing_latent_dim}
\end{table}

\section{Generated samples}
\label{appendix:generated_samples}
\begin{table}[!ht]
\small
\caption{Generated samples from models trained on grayscale and color images.}
\begin{center}
\begin{tabular}{ l c c c c c c }
  \toprule
  Trained on & \multicolumn{2}{c}{ \multirow{2}{*}{Baseline VAE}} & \multicolumn{2}{c}{VAE with negative sampling } & \multicolumn{2}{c}{VAE with negative sampling } \\
  Fashion-MNIST  & \multicolumn{2}{c}{ } & \multicolumn{2}{c}{negative: EMNIST-Letters}& \multicolumn{2}{c}{negative: adversarial} \\
  \midrule
  & Bernoulli & Gaussian & Bernoulli & Gaussian & Bernoulli & Gaussian  \\
  \includegraphics[width=1.7cm]{assets/fashion_mnist.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/fashion_mnist_baseline_bernoulli_generated_ON-1608.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/fashion_mnist_baseline_gaussian_generated_ON-1596.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/fashion_mnist_neg_letters_bernoulli_generated_ON-1647.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/fashion_mnist_neg_letters_gaussian_generated_ON-1666.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/fashion_mnist_neg_adv_bernoulli_generated_ON-1650.png}& \includegraphics[width=1.7cm]{assets/generated_samples/fashion_mnist_neg_adv_gaussian_generated_ON-1664.png} \\
  \midrule
  Trained on &\multicolumn{2}{c}{  \multirow{2}{*}{Baseline VAE} }& \multicolumn{2}{c}{VAE with negative sampling } & \multicolumn{2}{c}{VAE with negative sampling } \\

  MNIST & \multicolumn{2}{c}{ } & \multicolumn{2}{c}{negative: EMNIST-Letters}& \multicolumn{2}{c}{negative: adversarial} \\
  \cmidrule{1-7}
  & Bernoulli & Gaussian & Bernoulli & Gaussian & Bernoulli & Gaussian  \\
  \includegraphics[width=1.7cm]{assets/mnist.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/mnist_baseline_bernoulli_generated_ON-1609.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/mnist_baselilne_gaussian_generated_ON-1595.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/mnist_neg_letters_bernoulli_generated_ON-1346.png} &
   \includegraphics[width=1.7cm]{assets/generated_samples/mnist_neg_letters_gaussian_generated_ON-1770.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/mnist_neg_adv_bernoulli_generated_ON-1359.png}&
  \includegraphics[width=1.7cm]{assets/generated_samples/mnist_neg_adv_gaussian_generated_ON-1385.png} \\
  \midrule
  Trained on & \multicolumn{2}{c}{ \multirow{2}{*}{Baseline VAE}} & \multicolumn{2}{c}{VAE with negative sampling } & \multicolumn{2}{c}{VAE with negative sampling } \\
  CIFAR-10 & \multicolumn{2}{c}{ } & \multicolumn{2}{c}{negative: Ds. ImageNet}& \multicolumn{2}{c}{negative: adversarial} \\
  \cmidrule{1-7}
  & Bernoulli & Gaussian & Bernoulli & Gaussian & Bernoulli & Gaussian  \\
  \includegraphics[width=1.7cm]{assets/cifar10.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/cifar10_baseline_bernoulli_generated_ON-1735.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/cifar10_baseline_gaussian_generated_ON-1734.png} & \includegraphics[width=1.7cm]{assets/generated_samples/cifar10_neg_imagenet_bernoulli_generated_ON-1737.png} & \includegraphics[width=1.7cm]{assets/generated_samples/cifar10_neg_imagenet_gaussian_generated_ON-1746.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/cifar10_neg_adv_bernoulli_sn_generated_epoch300_ON-2077.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/cifar10_neg_adv_gaussian_generated_epoch300_ON-2079.png} \\
  \midrule
  Trained on & \multicolumn{2}{c}{ \multirow{2}{*}{Baseline VAE}} & \multicolumn{2}{c}{VAE with negative sampling } & \multicolumn{2}{c}{VAE with negative sampling } \\
  SVHN & \multicolumn{2}{c}{ } & \multicolumn{2}{c}{negative: Ds. ImageNet}& \multicolumn{2}{c}{negative: adversarial} \\
  \cmidrule{1-7}
  & Bernoulli & Gaussian & Bernoulli & Gaussian & Bernoulli & Gaussian  \\
  \includegraphics[width=1.7cm]{assets/svhn_cropped.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/svhn_baseline_bernoulli_generated_ON-1773.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/svhn_baseline_gaussian_generated_ON-1774.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/svhn_neg_imagenet_bernoulli_generated_ON-1776.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/svhn_neg_imagenet_gaussian_generated_ON-1775.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/svhn_neg_adv_bernoulli_generated_epoch300_ON-2082.png} &
  \includegraphics[width=1.7cm]{assets/generated_samples/svhn_neg_adv_gaussian_generated_epoch300_ON-2070.png} \\
  \bottomrule
\end{tabular}
\end{center}
\label{table:generated_samples}
\end{table}

\section{Plots from the latent space}
\label{appendix:latent_images}

\setlength{\tabcolsep}{0.2em}
\renewcommand{\arraystretch}{1.8}
\begin{table}[!ht]
\small
\caption{First two coordinates of the latent space of baseline VAE and VAE with negative sampling, with Bernoulli noise model, trained on Fashion-MNIST, and MNIST used as OOD dataset.}
\begin{center}
\begin{tabular}{l l c c c }
  \multicolumn{2}{c}{\multirow{8}{*}{\rotatebox[origin=c]{90}{Baseline VAE}}} & Epoch 1  & Epoch 10  & Epoch 100 \\
    & &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_baseline_latent_coords0-1_epoch1.pdf} &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_baseline_latent_coords0-1_epoch10.pdf} &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_baseline_latent_coords0-1_epoch100.pdf} \\
  \cmidrule{1-5}
  \multirow{8}{*}{\rotatebox[origin=c]{90}{VAE with negative sampling}} & \multirow{8}{*}{\rotatebox[origin=c]{90}{ negative: EMNIST-Letters}}  & Epoch 1  & Epoch 10  & Epoch 100 \\
  & &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_letters_latent_coords0-1_epoch1.pdf}&
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_letters_latent_coords0-1_epoch10.pdf} &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_letters_latent_coords0-1_epoch100.pdf} \\
  \cmidrule{1-5}
  \multicolumn{2}{c}{ \multirow{16}{*}{\rotatebox[origin=c]{90}{VAE with negative sampling, negative: adversarial generated}}} & Epoch 1  & Epoch 5  & Epoch 10 \\
  & &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_adv_latent_coords0-1_epoch1.pdf}&
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_adv_latent_coords0-1_epoch5.pdf} &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_adv_latent_coords0-1_epoch10.pdf} \\
  & & Epoch 45  & Epoch 95  & Epoch 100 \\
  & &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_adv_latent_coords0-1_epoch45.pdf}&
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_adv_latent_coords0-1_epoch95.pdf} &
  \includegraphics[width=4.2cm]{assets/latent_images/fashion_mnist_vs_mnist_bernoulli_neg_adv_latent_coords0-1_epoch100.pdf}
\end{tabular}
\end{center}
\label{table:latent_images}
\end{table}