Detecting Out-of-Distribution Inputs to Deep Generative Models Using Typicality
Eric Nalisnick, Akihiro Matsukawa, Yee Whye Teh, Balaji Lakshminarayanan
Introduction
Recent work (Nalisnick et al., 2019; Choi et al., 2019; Shafaei et al., 2018) showed that a variety of deep generative models fail to distinguish training from out-of-distribution (OOD) data according to the model likelihood. This phenomenon occurs not only when the data sets are similar but also when they have dramatically different underlying semantics. For instance, Glow (Kingma & Dhariwal, 2018), a state-of-the-art normalizing flow, trained on CIFAR-10 will assign a higher likelihood to SVHN than to its CIFAR-10 training data (Nalisnick et al., 2019; Choi et al., 2019). This result is surprising since CIFAR-10 contains images of frogs, horses, ships, trucks, etc. and SVHN contains house numbers. A human would be very unlikely to confuse the two sets. These findings are also troubling from an algorithmic standpoint since higher OOD likelihoods break previously proposed methods for classifier validation (Bishop, 1994) and anomaly detection (Pimentel et al., 2014).
We conjecture that these high OOD likelihoods are evidence of the phenomenon of typicality.Choi et al. (2019) also consider typicality as an explanation but ultimately deem it not to be a crucial factor. Nalisnick et al. (2019) implicitly mention typicality in their discussion of the transformed representations’ proximity to the mean and explicitly in a comment on Open Review: https://openreview.net/forum?id=H1xwNhCcYm¬eId=HkgLWfveT7 Due to concentration of measure, a generative model will draw samples from its typical set (Cover & Thomas, 2012), a subset of the model’s full support. However, the typical set may not necessarily intersect with regions of high probability density. For example, consider a -dimensional isotropic Gaussian. Its highest density region is at its mode (the mean) but the typical set resides at a distance of from the mode (Vershynin, 2018). Thus a point near the mode will have high likelihood while being extremely unlikely to be sampled from the model. We believe that deep generative models exhibit a similar phenomenon since, to return to the CIFAR-10 vs SVHN example, Nalisnick et al. (2019) showed that sampling from the model trained on CIFAR-10 never generates SVHN-looking images despite SVHN having higher likelihood.
Based on this insight, we propose that OOD detection should be done by checking if an input resides in the model’s typical set, not just in a region of high density. Unfortunately it is impossible to analytically derive the regions of typicality for the vast majority of deep generative models. To define a widely applicable and scalable OOD-detection algorithm, we formulate Shannon (1948)’s entropy-based definition of typicality into a statistical hypothesis test. To ensure that the test is robust even in the low-data regime, we employ a bootstrap procedure (Efron & Tibshirani, 1994) to set the OOD-decision threshold. In the experiments, we demonstrate that our detection procedure succeeds in many of the challenging cases presented by Nalisnick et al. (2019). In addition to these successes, we also discuss failure modes that reveal drastic variability in OOD detection for the same data set pairs under different generative models. We highlight these cases to inspire future work.
Background: Typical Sets
The typical set of a probability distribution is the set whose elements have an information content sufficiently close to that of the expected information (Shannon, 1948). A formal definition follows.
-Typical Set (Cover & Thomas, 2012) For a distribution with support , the -typical set is comprised of all -length sequences that satisfy
When the joint density in Definition 2.1 factorizes, we can write:
Probability of the Typical Set (Cover & Thomas, 2012) For sufficiently large, the typical set has probability
A Typicality Test for OOD Inputs
We next describe our core contribution: a reformulation of Definition 2.1 into a scalable goodness-of-fit test to determine if a batch of test data was likely drawn from a given deep generative model.
Assume we have a generative model —with denoting the parameters—that was trained on a data set . Take to be high-dimensional () and to be sufficiently large () so as to enable training a high-capacity neural-network parametrized model—a so-called ‘deep generative model’ (DGM). Furthermore, we assume that has a likelihood that can be evaluated either directly or closely approximated via Monte Carlo sampling. Examples of DGMs that meet these specifications include normalizing flows (Tabak & Turner, 2013) such as Glow (Kingma & Dhariwal, 2018), latent variable models such as variational autoencoders (VAEs) (Kingma & Welling, 2014; Rezende et al., 2014), and auto-regressive models such as PixelCNN (van den Oord et al., 2016). We do not consider implicit generative models (Mohamed & Lakshminarayanan, 2016) (such as GANs (Goodfellow et al., 2014)) due to their likelihood being difficult to even approximate.
2 A Hypothesis Test for Typicality
Returning to the results of Nalisnick et al. (2019) and Choi et al. (2019), the high-dimensionality of natural images ( for CIFAR and SVHN) alone is enough to suspect the influence of phenomena akin to the Gaussian Annulus Theorem. Yet there are stronger parallels still: Nalisnick et al. (2019) showed that the all-black image has the highest density of any tested input to their FashionMNIST DGM, but this model is never observed to generate all-black images. Thus we are inspired to critique DGMs not via density but via typical set membership:
The intuition is that if is indeed sampled from , then with high probability it must reside in the typical set (Theorem 2.1). To determine if , we can plug into Equation 1 as a length sequence and check if the -bound holds:
where denotes the test statistic. We provide a sanity check for Equation 3 in Subfigure 1(c), showing calculated for the high-dimensional Gaussian example described in Section 2. We see that achieves its minimum value exactly at -distance from .
3 Implementation Details
The entropy of DGMs is not available in closed-form and therefore we resort to the following sampling-based approximation. Recall from Subsection 2 that the AEP states that the sample entropy will converge to the true entropy as the number of samples grows. Since we have access to the model and can drawn a large number of samples from it, the empirical entropy should be a good approximation for the true model entropy:
where . However, in preliminary experiments (reported in Appendix E.1) we observed markedly better OOD detection when using an alternative estimator known as the resubstitution estimator (Beirlant et al., 1997). This estimator uses the training set for calculating the expectation:
This approximation should be good as well since we assume to be large.The bias and variance of the resubstitution estimator are hard to characterize for DGMs. The work of Joe (1989) is most related, describing its properties under multivariate kernel density estimators.
Setting the OOD-Threshold with the Bootstrap
Concerning the threshold , we propose setting its value through simulation—by constructing a bootstrap confidence interval (BCI) (Efron, 1992; Arcones & Gine, 1992) for the null hypothesis , with the alternative being . In a slight deviation from the tradition procedure for BCI construction, we assume the existence of a validation set that was held-out from before training the generative model (just as is usually done for hyperparameter tuning).This is only to account for the generative model overfitting to the training set. From this validation set, we bootstrap sample ‘new’ data sets of size and then plug each into Equation 3 in place of :
Related Work
As mentioned in Section 3.1, many of the traditional GoF tests are not applicable to the DGMs and high-dimensional data sets that we consider since CDFs and EDFs are both intractable in this setting. Kernelized Stein discrepancy (Chwialkowski et al., 2016; Liu et al., 2016) is a recently-proposed GoF test that can scale to the DGM regime, and we compare against it in the experiments. Several works have proposed GoF tests based on entropy (Gokhale, 1983; Parzen, 1990)—e.g. for normal (Vasicek, 1976), uniform (Dudewicz & Van Der Meulen, 1981), and exponential (Crzcgorzewski & Wirczorkowski, 1999) distributions. However, these tests are derived from maximum entropy results and not motivated from typicality. There are also directed GoF tests such as ones based on likelihood ratios (Neyman & Pearson, 1933; Wilks, 1938) or discrepancies such as KL divergence (Noughabi & Arghami, 2013). These tests require an explicit definition of , which may be difficult in many DGM-appropriate scenarios. Yet the recent work of Ren et al. (2019) does apply likelihood ratios to PixelCNNs by constructing such that it models a background process (i.e. some perturbed version of the original data).
Typical and Minimum Volume Sets
We are aware of only two previous works that use a notion of typicality for GoF tests or OOD detection. Sabeti & Høst-Madsen (2019) propose a typicality framework based on minimum description length. They deem data as ‘atypical’ if it can be represented in less bits than one would expect under the generative model. While our frameworks share the same conceptual foundation, Sabeti & Høst-Madsen (2019)’s implementation relies on strong parametric assumptions and cannot be generalized to deep models (without drastic approximations). Choi et al. (2019), the second work, leverages normalizing flows to test for typicality by transforming the data to a normal distribution and then deeming points outside the annulus to be anomalous. This approach restricts the generative model to be a Gaussian normalizing flow whereas ours is applicable to any generative model with a computable likelihood. Our work is also related to the concept of minimum volume (MV) sets (Sager, 1979; Polonik, 1997; Garcia et al., 2003). MV sets have been used for GoF testing (Polonik, 1999; Glazer et al., 2012) and to detect outliers (Platt et al., 2001; Scott & Nowak, 2006; Clémençon et al., 2018). However, we are not aware of any work that scales MV-set-based methodologies to the degree required to be applicable to DGMs.
Generative Models and Outlier Detection
Probabilistic but non-test-based techniques have also been widely employed to discover outliers and anomalies (Pimentel et al., 2014). One of the most common is to use a (one-sided) threshold on the density function to classify points as OOD (Barnett et al., 1994); this idea is used in Tarassenko et al. (1995) Bishop (1994), and Parra et al. (1996), among others. Other work has applied more sophisticated techniques to density function evaluations—for instance, Clifton et al. (2014) applies extreme value theory. Yet this work and all others of which we are aware do not identify points with abnormally high density as OOD. Thus they would fail in the settings presented by Nalisnick et al. (2019). As for work focusing on DGMs in particular, most previous work proposes training improvements to make the model more robust. For instance, Hendrycks et al. (2019) show that robustness and uncertainty quantification w.r.t. outliers can be improved by exposing the model to an auxiliary data set (a proxy for OOD data) during training. As for post-training outlier and OOD detection, Choi et al. (2019) proposes using an ensemble of models to compute the Watanabe-Akaike information criterion (WAIC). However, there are no rigorous arguments for why WAIC should quantify GoF. Škvára et al. (2018) proposes using a VAE’s conditional likelihood as an outlier criterion, finding that this works well only when the hyperparameters can be tuned using anomalous data. As far as we are aware, we are the first to apply a hypothesis testing framework to the problem of OOD or anomaly detection for DGMs. As mentioned above, Ren et al. (2019) use likelihood ratios, but they do not perform a hypothesis test.
Experiments
We now evaluate our typicality test’s OOD detection abilities, focusing in particular on the image data set pairs highlighted by Nalisnick et al. (2019). We use the same three generative models as they did—Glow (Kingma & Dhariwal, 2018), PixelCNN (van den Oord et al., 2016), and Rosca et al. (2018)’s VAE architecture—attempting to replicate training and evaluation as closely as possible. See Appendix C for a full description of model architectures and training. See Appendix D for more details on evaluation. We consider the following baselinesWe could not replicate the performance of WAIC as reported by Choi et al. (2019). See Appendix E.2.; all statistical tests use :
t-test: We apply a two-sample students’ t-test to check for a difference in means in the empirical likelihoods. In terms of Equation 3, this baseline will reject for any , and thus we expect it to be overly conservative. Moreover, this test does not have access to validation data and therefore improvements upon it can be attributed to our bootstrap procedure.
Kolmogorov-Smirnov test (KS-test): We apply a two-sample KS-test to the likelihood EDFs. This test is stronger than our typicality test since it is checking for equivalence in all moments whereas ours (and the t-test) is restricted to the first moment. In turn, this test has a greater computational complexity— compared to .
Maximum Mean Discrepancy (MMD): We apply a two-sample MMD (Gretton et al., 2012) test to the data directly. Yet we incorporate the generative model by using a Fisher kernel (Jaakkola & Haussler, 1999). We also apply the same bootstrap procedure on validation data to construct the test statistic. MMD has greater runtime still at . It also requires access to (a subset of) the training data at test-time, which is undesirable.
Kernelized Stein Discrepancy (KSD): We apply KSD (Liu et al., 2016) to test for GoF to the generative model and again use a Fisher kernel and the bootstrap procedure on validation data. KSD has runtime . While we have ignored the construction of the kernel in the runtime analysis, KSD is the most costly since it requires computing three model gradients.
Annulus Method: We use a modified version of Choi et al. (2019)’s annulus method applied to Gaussian normalizing flows. Like them, we classify something as OOD based on its distance to the sphere with radius . This is essentially performing our test but via closed-form expressions for entropy made available by the Gaussian base distribution. We use the same bootstrap procedure on validation data to set the ‘slack’ variable .
We first evaluate our typicality test on grayscale images. We trained a Glow, PixelCNN, and VAE each on the FashionMNIST training split and tested OOD detection using the FashionMNIST, MNIST, and NotMNIST test splits. We use the FashionMNIST test split to evaluate for type-I error (incorrect rejection of the null) and the MNIST and NotMNIST splits for type-II error (incorrect rejection of the alternative). In Figure 2 we show the empirical distribution of likelihoods over each data set for each model. We see the same phenomenon as reported by Nalisnick et al. (2019)—namely, that the MNIST OOD test set (green) has a higher likelihood than the training set (black). Lower-sided thresholding (Bishop, 1994) would clearly fail to detect the OOD sets. Table 1 reports a comparison against baselines, showing the fraction of -sized batches classified as OOD. The IN-DIST. column reports the value for the FashionMNIST test set and ideally this number should be ; any deviation from zero corresponds to type-I error. Conversely, the MNIST and NotMNIST columns should be , and any deviation corresponds to type-II error. We see that for all tests find it hard to reject the null hypothesis, which is not surprising given the overlap in the histograms in Figure 2. The exceptions are the annulus method for NotMNIST-Glow (), the typicality test for MNIST-PixelCNN (), and all methods except KS-test for NotMNIST-VAE. One failure mode for almost all methods is NotMNIST for the PixelCNN. None of the likelihood-based tests can distinguish NotMNIST as OOD due to the near perfect overlap in histograms shown in Figure 2(b). KSD and especially MMD are able to perform better in this case due to having access to the original feature-space representations (in addition to the generative model). Yet, surprisingly, KSD and MMD perform comparatively poorly for MNIST, especially at and . The annulus method was unable to detect MNIST, which we found surprising given its close relationship to our typicality test, which does perform well. Yet Choi et al. (2019) note that Gaussian normalizing flows do not necessarily make the latent space normally distributed, and our typicality test may be able to use information from the volume element that is not available to the annulus method.
Natural Images
We next turn to data sets of natural images—in particular SVHN, CIFAR-10, and ImageNet. We train Glow on SVHN, CIFAR-10, and ImageNet and use the two non-training sets for OOD evaluation. We found using MMD and KSD to be too expensive to make OOD decisions in an online system. Table 2 reports the fraction of -sized batches classified as OOD. We see that our method (first row, bolded) is able to easily detect the OOD sets for SVHN, rejecting size-two batches at the rate of while having only type-I error. Performance on the CIFAR-10-trained model is good as well with of OOD batches detected at and at (type-I error at in both cases). The hardest case is Glow trained on ImageNet: the KS-test performed best at with , followed by the t- and typicality tests at and respectively. The annulus method again had varying performance, being conspicuously inferior at detecting SVHN for the CIFAR and ImageNet models while having the best performance on ImageNet for the CIFAR model. We report additional results in Appendix E.3 for our method, showing performance for all and when using CIFAR-100 as an OOD set.
Lastly, we report two challenging cases worthy of note and further attention. Figure 3(a) shows our method applied to Glow when trained on CIFAR-10, tested on CIFAR-100. The -axis again shows fraction of batches reported as OOD and the -axis the batch size . Even at our method classifies only of batches as OOD. Yet this result is not surprising given that CIFAR-10 is a subset of CIFAR-100, which means that our test’s subset assumptions for consistency are violated. More interesting is the case of Glow trained on CelebA, tested on CIFAR-10 and CIFAR-100. Figure 3(b) shows the histogram of log-likelihoods: all distributions peak at nearly the same value. The distribution of observed during the bootstrap procedure () is shown in Figure 3(c), with the red and black dotted lines denoting computed using the whole set. We see that for the OOD set is even less than the in-distribution’s, meaning that it would be impossible to reliably reject the OOD data while not rejecting the in-distribution test set as well. Interestingly, PixelCNN and VAE do not have as dramatic of an overlap in likelihoods—a phenomenon that can also be observed in Figure 2—which implies that the ability to detect OOD sets does not only depend on the data involved but the models as well. Some models may have likelihood functions that are reliably discriminative, and this presents an intriguing area for future work.
Discussion and Conclusions
We have presented a model-agnostic and computationally efficient statistical test for OOD inputs derived from the concept of typical sets. In the experiments we showed that the proposed test is especially well-suited to DGMs, identifying the OOD set for SVHN vs CIFAR-10 vs ImageNet (Nalisnick et al., 2019) with high accuracy (while maintaining type-I error). In this work we used the null hypothesis , which was necessary since we assumed access to only one training data set. One avenue for future work is to use auxiliary data sets (Hendrycks et al., 2019) to construct a test statistic for the null , as would be proper for safety-critical applications. In our experiments we also noticed two cases—PixelCNN trained on FashionMNIST, tested on NotMNIST and Glow trained on CelebA, tested on CIFAR—in which the empirical distributions of in- and out-of-distribution likelihoods matched near perfectly. Thus use of the likelihood distribution produced by DGMs has a fundamental limitation that is seemingly worse than what was reported by Nalisnick et al. (2019).
References
Appendix A Theoretical Properties
For the sake of completeness, we make explicit the connection between Definition 2.1 and the Gaussian annulus example. Plugging in the spherical Gaussian’s entropy and density function into Equation 1, we have:
For , we see that any point that satisfies guarantees the bound for any :
Recalling Figure 1(a), is exactly the radius of the annulus at which the Gaussian’s mass concentrates. Of course as grows, points further from or nearer to the mean than are included as typical. The behavior for finite is harder to characterize, as the definition is essential testing the -bound for the average squared norm. Yet we know that for large samples ,
which again allows the bound to hold for any .
A.2 Consistency of the Test
Below we show that the test presented in Section 3.2 is consistent unless .
Appendix B Algorithmic Implementation
The pseudocode of the procedure is described in Algorithm 1.
Appendix C Generative Model Details
Our Glow (Kingma & Dhariwal, 2018) implementation was derived from OpenAI’s open source repositoryhttps://github.com/openai/glow and modified following the specifications in Appendix A of Nalisnick et al. (2019). All versions were trained with RMSProp, batch size of , with a learning rate of for k steps and decayed by a factor of after k and k steps. All priors were chosen to be standard Normal distributions. We follow Nalisnick et al. (2019)’s zero-initialization strategy (last coupling layer set to zero) and in turn did not apply any normalization. Similarly, our convolutional layers were initialized by sampling from the same truncated Normal distribution (Nalisnick et al., 2019). For our FashionMNIST experiment, Glow had two blocks of 16 affine coupling layers (ACLs) (Dinh et al., 2017). The spatial dimension was only squeezed between blocks. For the SVHN, CIFAR-10, and ImageNet models, we used three blocks of 8 ACLs with multi-scale factorization occurring between each block. All ACL transformations used a three-layer highway network. 200 hidden units were used for fashionMNIST and 400 for all other data sets.
PixelCNN
We trained a GatedPixelCNN (van den Oord et al., 2016) using Adam ( initial learning rate, decayed by at steps k and k, k total steps) for FashionMNIST and RMSProp ( initial learning rate, decayed by at steps k, k, and k, k total steps) for all other data sets. The FashionMNIST network had gated layers ( features) and a -sized skip connection. All other networks used gated layers ( features) and a -sized skip connection
Variational Autoencoder
We used the convolutional decoder VAE (Kingma & Welling, 2014) variant described by Rosca et al. (2018). For Fashion MNIST, the decoder contained three convolutional layers with filter sizes , , and and stides of , , and . Training was done again via RMSProp ( initial learning rate, no decay, k total steps). For all other models, we followed the specifications in Rosca et al. (2018) Appendix K.
Appendix D Experimental Details
We found that MMD and KSD only had good performance when using the Fisher kernel (Jaakkola & Haussler, 1999): . All other kernels attempted required substantial tuning to the scale parameters and we did not want to assume access to enough data to perform this tuning. The ineffectiveness of MMD on pixel-space has been noted previously (Bińkowski et al., 2018). Furthermore, we found the memory cost of implementing the traditional Fisher kernel to be quite costly for Glow, each vector having million+ elements. Hence in the experiments we use the kernel modified such that the derivative is taken w.r.t. the input (making it the likelihood score): .
Data Set Splits and Bootstrap Re-Samples
For each data set we used the canonical train-test splits. To construct the validation set and perform bootstrapping, we extracted samples from the test split and bootstrap sampled (with replacement) data sets to calculate . We didn’t find using to markedly change performance. We then extracted another samples from the test split, divided them into -sized batches, and classified each other as OOD or not according to the various tests. We repeated this whole process times, randomizing the instances in the validation and testing splits, in order to compute the means and standard deviations that are reported in Tables 1 and 2.
α𝛼\alpha-Level
In preliminary experiments, we did not find a notable difference in type-II error when using vs . Using the latter slightly improved type-I error and thus we used that value for all experiments and all methods.
Appendix E Additional Results
In the tables below, we report results comparing the two entropy estimators considered—the Monte Carlo approximation with samples from the model (Equation 4) vs the resubstitution estimator (Equation 5). We see that the samples-based estimator performs better in only one setting, FashionMNIST vs MNIST at . In all other cases, the resubstitution estimator performs equally well or better. In fact, the samples-based estimator could not detect NotMNIST as OOD at all, having even at and . This inferior performance is mostly due to the distribution of likelihoods being more diffuse when computed with samples. We suspect improvements to the generative models that enable them to better capture the true generative process will in turn improve the MC sample-based estimator.
E.2 Replication of WAIC Results
We did not include WAIC because we were not able to replicate the results of Choi et al. (2019). The figure to the right shows a WAIC histogram for CIFAR-10 (blue) vs SVHN (OOD, orange) computed using our Glow implementation (ensemble size 5). We attempted to reproduce Choi et al.’s Figure 3, which shows SVHN having lower and more dispersed scores than CIFAR-10. We did not observe this: all SVHN WAIC scores overlap with or are higher than CIFAR-10’s, meaning that SVHN can not be distinguished as the OOD set. Two differences between our Glow implementation and theirs were that they use Adam (vs RMSprop) and early stopping on a validation set. We found neither difference affected results.
E.3 Varying M𝑀M for Glow
Figure 4 reports results for our typicality test on Glow, varying from $$. Table 2’s results are a subset of these. We also report evaluations using CIFAR-100 as an OOD set.