A note on the evaluation of generative models
Lucas Theis, Aäron van den Oord, Matthias Bethge
Introduction
Generative models have many applications and can be evaluated in many ways. For density estimation and related tasks, log-likelihood (or equivalently Kullback-Leibler divergence) has been the de-facto standard for training and evaluating generative models. However, the likelihood of many interesting models is computationally intractable. For example, the normalization constant of unnormalized energy-based models is generally difficult to compute, and latent-variable models often require us to solve complex integrals to compute the likelihood. These models may still be trained with respect to a different objective that is more or less related to log-likelihood, such as contrastive divergence (Hinton, 2002), score matching (Hyvärinen, 2005), lower bounds on the log-likelihood (Bishop, 2006), noise-contrastive estimation (Gutmann & Hyvärinen, 2010), probability flow (Sohl-Dickstein et al., 2011), maximum mean discrepancy (MMD) (Gretton et al., 2007; Li et al., 2015), or approximations to the Jensen-Shannon divergence (JSD) (Goodfellow et al., 2014).
For computational reasons, generative models are also often compared in terms of properties more readily accessible than likelihood, even when the task is density estimation. Examples include visualizations of model samples, interpretations of model parameters (Hyvärinen et al., 2009), Parzen window estimates of the model’s log-likelihood (Breuleux et al., 2009), and evaluations of model performance in surrogate tasks such as denoising or missing value imputation.
In this paper, we look at some of the implications of choosing certain training and evaluation criteria. We first show that training objectives such as JSD and MMD can result in very different optima than log-likelihood. We then discuss the relationship between log-likelihood, classification performance, visual fidelity of samples and Parzen window estimates. We show that good or bad performance with respect to one metric is no guarantee of good or bad performance with respect to the other metrics. In particular, we show that the quality of samples is generally uninformative about the likelihood and vice versa, and that Parzen window estimates seem to favor models with neither good likelihood nor samples of highest possible quality. Using Parzen window estimates as a criterion, a simple model based on -means outperforms the true distribution of the data.
Training of generative models
Many objective functions and training procedures have been proposed for optimizing generative models. The motivation for introducing new training methods is typically the wish to fit probabilistic models with computationally intractable likelihoods, rendering direct maximum likelihood learning impractical. Most of the available training procedures are consistent in the sense that if the data is drawn from a model distribution, then this model distribution will be optimal under the training objective in the limit of an infinite number of training examples. That is, if the model is correct, and for extremely large amounts of data, all of these methods will produce the same result. However, when there is a mismatch between the data distribution and the model, different objective functions can lead to very different results.
Figure 1 illustrates this on a simple toy example where an isotropic Gaussian distribution has been fit to a mixture of Gaussians by minimizing various measures of distance. Maximum mean discrepancy (MMD) has been used with generative moment matching networks (Li et al., 2015; Dziugaite et al., 2015) and Jensen-Shannon divergence (JSD) has connections to the objective function optimized by generative adversarial networks (Goodfellow et al., 2014) (see box for a definition). Minimizing MMD or JSD yields a Gaussian which fits one mode well, but which ignores other parts of the data. On the other hand, maximizing average log-likelihood or equivalently minimizing Kullback-Leibler divergence (KLD) avoids assigning extremely small probability to any data point but assigns a lot of probability mass to non-data regions.
Understanding the trade-offs between different measures is important for several reasons. First, different applications require different trade-offs, and we want to choose the right metric for a given application. Assigning sufficient probability to all plausible images is important for compression, but it may be enough to generate a single plausible example in certain image reconstruction applications (e.g., Hays & Efros, 2007). Second, a better understanding of the trade-offs allows us to better interpret and relate empirical findings. Generative image models are often assessed based on the visual fidelity of generated samples (e.g., Goodfellow et al., 2014; Gregor et al., 2015; Denton et al., 2015; Li et al., 2015). Figure 1 suggests that a model optimized with respect to KLD is more likely to produce atypical samples than the same model optimized with respect to one of the other two measures. That is, plausible samples—in the sense of having large density under the target distribution—are not necessarily an indication of a good density model as measured by KLD, but may be expected when optimizing JSD.
Evaluation of generative models
Just as choosing the right training method is important for achieving good performance in a given application, so is choosing the right evaluation metric for drawing the right conclusions. In the following, we first continue to discuss the relationship between average log-likelihood and the visual appearance of model samples.
Model samples can be a useful diagnostic tool, often allowing us to build an intuition for why a model might fail and how it could be improved. However, qualitative as well as quantitative analyses based on model samples can be misleading about a model’s density estimation performance, as well as the probabilistic model’s performance in applications other than image synthesis. Below we summarize a few examples demonstrating this.
Average log-likelihood is widely considered as the default measure for quantifying generative image modeling performance. However, care needs to be taken to ensure that the numbers measured are meaningful. While natural images are typically stored using 8-bit integers, they are often modeled using densities, i.e., an image is treated as an instance of a continuous random variable. Since the discrete data distribution has differential entropy of negative infinity, this can lead to arbitrary high likelihoods even on test data. To avoid this case, it is becoming best practice to add real-valued noise to the integer pixel values to dequantize the data (e.g., Uria et al., 2013; van den Oord & Schrauwen, 2014; Theis & Bethge, 2015).
If we add the right amount of uniform noise, the log-likelihood of the continuous model on the dequantized data is closely related to the log-likelihood of a discrete model on the discrete data. Maximizing the log-likelihood on the continuous data also optimizes the log-likelihood of the discrete model on the original data. This can be seen as follows.
Consider images with a discrete probability distribution , uniform noise , and noisy data . If refers to the noisy data density and refers to the model density, then we have for the average log-likelihood:
where the second step follows from Jensen’s inequality and we have defined
A second motivation for using log-likelihood comes from semi-supervised learning. Consider a dataset consisting of images and corresponding labels for some but not necessarily all of the images. In classification, we are interested in the prediction of a class label for a previously unseen query image . For a given model relating , , and parameters , the only correct way to infer the distribution over —from a Bayesian point of view —is to integrate out the parameters (e.g., Lasserre et al., 2006),
With sufficient data and under certain assumptions, the above integral will be close to , where
When no training labels are given, i.e., in the unsupervised setting, and for a uniform prior over parameters, it is therefore natural to try to optimize the log-likelihood, .
In practice, this approach might fail because of a mismatch between the model and the data, because of an inability to solve Equation 9, or because of overfitting induced by the MAP approximation. These issues can be addressed by better image models (e.g., Kingma et al., 2014), better optimization and inference procedures, or a more Bayesian treatment of the parameters (e.g., Lacoste-Julien et al., 2011; Welling & Teh, 2011).
2 Samples and log-likelihood
For many interesting models, average log-likelihood is difficult to evaluate or even approximate. For some of these models at least, generating samples is a lot easier. It would therefore be useful if we could use generated samples to infer something about a model’s log-likelihood. This approach is also intuitive given that a model with zero KL divergence will produce perfect samples, and visual inspection can work well in low dimensions for assessing a model’s fit to data. Unfortunately these intuitions can be misleading when the image dimensionality is high. A model can have poor log-likelihood and produce great samples, or have great log-likelihood and produce poor samples.
A simple lookup table storing enough training images will generate convincing looking images but will have poor average log-likelihood on unseen test data. Somewhat more generally we might consider a mixture of Gaussian distributions,
where the means are either training images or a number of plausible images derived from the training set (e.g., using a set of image transformations). If is small enough such that the Gaussian noise becomes imperceptible, this model will generate great samples but will still have very poor log-likelihood. This shows that plausible samples are clearly not sufficient for a good log-likelihood.
Gerhard et al. (2013) empirically found a correlation between some models’ log-likelihoods and their samples’ ability to fool human observers into thinking they were extracted from real images. However, the image patches were small and all models used in the study were optimized to minimize KLD. The correlation between log-likelihood and sample quality may disappear, for example, when considering models optimized for different objective functions or already when considering a different set of models.
Great log-likelihood and poor samples
Perhaps surprisingly, the ability to produce plausible samples is not only not sufficient, but also not necessary for high likelihood as a simple argument by van den Oord & Dambre (2015) shows: Assume is the density of a model for dimensional data which performs arbitrarily well with respect to average log-likelihood and corresponds to some bad model (e.g., white noise). Then samples generated by the mixture model
will come from the poor model 99% of the time. Yet the log-likelihood per pixel will hardly change if is large:
For high-dimensional data, will be proportional to while stays constant. For instance, already for the 32 by 32 images found in the CIFAR-10 dataset the difference between log-likelihoods of different models can be in the thousands, while is only about 4.61 nats (van den Oord & Dambre, 2015). This shows that a model can have large average log-likelihood but generate very poor samples.
Good log-likelihood and great samples
Note that we could have also chosen (Equation 11) such that it reproduces training examples, e.g., by choosing as in Equation 10. In this case, the mixture model would generate samples indistinguishable from real images 99% of the time while the log-likelihood would again only change by at most 4.61 nats. This shows that any model can be turned into a model which produces realistic samples at little expense to its log-likelihood. Log-likelihood and visual appearance of samples are therefore largely independent.
3 Samples and applications
One might conclude that something must be wrong with log-likelihood if it does not care about a model’s ability to generate plausible samples. However, note that the mixture model in Equation 11 might also still work very well in applications. While is much more likely a priori, is going to be much more likely a posteriori in tasks like inpainting, denoising, or classification. Consider prediction of a quantity representing, for example, a class label or missing pixels. A model with joint distribution
may again generate poor samples 99% of the time. For a given fixed , the posterior over will be a mixture
where a few simple calculations show that
and is the sigmoidal logistic function. Since we assume that is a good model, is a poor model, and is high-dimensional, we have
and therefore . That is, mixing with has hardly changed the posterior over . While the samples are dominated by , the classification performance is dominated by . This shows that high visual fidelity of samples is generally not necessary for achieving good performance in applications.
4 Evaluation based on samples and nearest neighbors
A qualitative assessment based on samples can be biased towards models which overfit (Breuleux et al., 2009). To detect overfitting to the training data, it is common to show samples next to nearest neighbors from the training set. In the following, we highlight two limitations of this approach and argue that it is unfit to detect any but the starkest forms of overfitting.
Nearest neighbors are typically determined based on Euclidean distance. But already perceptually small changes can lead to large changes in Euclidean distance, as is well known in the psychophysics literature (e.g., Wang & Bovik, 2009). To illustrate this property, we used the top-left 28 by 28 pixels of each image from the 50,000 training images of the CIFAR-10 dataset. We then shifted this 28 by 28 window one pixel down and one pixel to the right and extracted another set of images. We repeated this 4 times, giving us 4 sets of images which are increasingly different from the training set. Figure 2A shows nearest neighbors of corresponding images from the query set. Although the images have hardly changed visually, a shift by only two pixels already caused a different nearest neighbor. The plot also shows Euclidean distances to 100 randomly picked images from the training set. Note that with a bigger dataset, a switch to a different nearest neighbor becomes more likely. Figure 2B shows the fraction of query images assigned to the correct training image in our example. A model which stores transformed training images can trivially pass the nearest-neighbor overfitting test. This problem can be alleviated by choosing nearest neighbors based on perceptual metrics, and by showing more than one nearest neighbor.
A second problem concerns the entropy of the model distribution and is harder to address. There are different ways a model can overfit. Even when overfitting, most models will not reproduce perfect or trivially transformed copies of the training data. In this case, no distance metric will find a close match in the training set. A model which overfits might still never generate a plausible image or might only be able to generate a small fraction of all plausible images (e.g., a model as in Equation 10 where instead of training images we store several transformed versions of the training images, or a model which only describes data in a lower-dimensional subspace). Because the number of images we can process is vanishingly small compared to the vast number of possible images, we would not be able to detect this by looking at samples from the model.
5 Evaluation based on Parzen window estimates
When log-likelihoods are unavailable, a common alternative is to use Parzen window estimates. Here, samples are generated from the model and used to construct a tractable model, typically a kernel density estimator with Gaussian kernel. A test log-likelihood is then evaluated under this model and used as a proxy for the true model’s log-likelihood (Breuleux et al., 2009). Breuleux et al. (2009) suggested to fit the Parzen windows on both samples and training data, and to use at least as many samples as there are images in the training set. Following Bengio et al. (2013a), Parzen windows are in practice commonly fit to only 10,000 samples (e.g., Bengio et al., 2013b; Goodfellow et al., 2014; Li et al., 2015; Sohl-Dickstein et al., 2015). But even for a large number of samples Parzen window estimates generally do not come close to a model’s true log-likelihood when the data dimensionality is high. In Figure 4 we plot Parzen window estimates for a multivariate Gaussian distribution fit to small CIFAR-10 image patches (of size 6 by 6). We added uniform noise to the data (as explained in Section 3.1) and rescaled between 0 and 1. As we can see, a completely infeasible number of samples would be needed to get close to the actual log-likelihood even for this small scale example. For higher dimensional data this effect would only be more pronounced.
While the Parzen window estimate may be far removed from a model’s true log-likelihood, one could still hope that it produces a similar or otherwise useful ranking when applied to different models. Counter to this idea, Parzen window estimates of the likelihood have been observed to produce rankings different from other estimates (Bachman & Precup, 2015). More worryingly, a GMMN+AE (Li et al., 2015) is assigned a higher score than images from the training set (which are samples from the true distribution) when evaluated on MNIST (Table 4). Furthermore it is relatively easy to exploit the Parzen window loss function to achieve even better results. To illustrate this, we fitted 10,000 centroids to the training data using -means. We then generated 10,000 independent samples by sampling centroids with replacement. Note that this corresponds to the model in Equation 10, where the standard deviation of the Gaussian noise is zero and instead of training examples we use the centroids. We find that samples from this -means based model are assigned a higher score than any other model, while its actual log-likelihood would be .
Conclusion
We have discussed the optimization and evaluation of generative image models. Different metrics can lead to different trade-offs, and different evaluations favor different models. It is therefore important that training and evaluation match the target application. Furthermore, we should be cautious not to take good performance in one application as evidence of good performance in another application.
An evaluation based on samples is biased towards models which overfit and therefore a poor indicator of a good density model in a log-likelihood sense, which favors models with large entropy. Conversely, a high likelihood does not guarantee visually pleasing samples. Samples can take on arbitrary form only a few bits from the optimum. It is therefore unsurprising that other approaches than density estimation are much more effective for image synthesis (Portilla & Simoncelli, 2000; Dosovitskiy et al., 2015; Gatys et al., 2015). Samples are in general also an unreliable proxy for a model’s performance in applications such as classification or inpainting, as discussed in Section 3.3.
A subjective evaluation based on visual fidelity of samples is still clearly appropriate when the goal is image synthesis. Such an analysis at least has the property that the data distribution will perform very well in this task. This cannot be said about Parzen window estimates, where the data distribution performs worse than much less desirable modelsIn decision theory, such a metric is called an improper scoring function.. We therefore argue Parzen window estimates should be avoided for evaluating generative models, unless the application specifically requires such a loss function. In this case, we have shown that a k-means based model can perform better than the true density. To summarize, our results demonstrate that for generative models there is no one-fits-all loss function but a proper assessment of model performance is only possible in the the context of an application.
The authors would like to thank Jascha Sohl-Dickstein, Ivo Danihelka, Andriy Mnih, and Leon Gatys for their valuable input on this manuscript.