Title: YOUR CLASSIFIER IS SECRETLY AN ENERGY BASED MODEL AND YOU SHOULD TREAT IT LIKE ONE

Abstract: We propose to reinterpret a standard discriminative classifier of p(y|x) as an energy based model for the joint distribution p(x, y). In this setting, the standard class probabilities can be easily computed as well as unnormalized values of p(x) and p(x|y). Within this framework, standard discriminative architectures may be used and the model can also be trained on unlabeled data. We demonstrate that energy based training of the joint distribution improves calibration, robustness, and out-of-distribution detection while also enabling our models to generate samples rivaling the quality of recent GAN approaches. We improve upon recently proposed techniques for scaling up the training of energy based models and present an approach which adds little overhead compared to standard classification training. Our approach is able to achieve performance rivaling the state-of-the-art in both generative and discriminative learning within one hybrid model.

1. INTRODUCTION:

For decades, research on generative models has been motivated by the promise that generative models can benefit downstream problems such as semi-supervised learning, imputation of missing data, and calibration of uncertainty (e.g., Chapelle et al. (2006) ; Dempster et al. (1977) ). Yet, most recent research on deep generative models ignores these problems, and instead focuses on qualitative sample quality and log-likelihood on heldout validation sets.

Currently, there is a large performance gap between the strongest generative modeling approach to downstream tasks of interest and hand-tailored solutions for each specific problem. One potential explanation is that most downstream tasks are discriminative in nature and state-of-the-art generative models have diverged quite heavily from state-of-the-art discriminative architectures. Thus, even when trained solely as classifiers, the performance of generative models is far below the performance of the best discriminative models. Hence, the potential benefit from the generative component of the model is far outweighed by the decrease in discriminative performance. Recent work (Behrmann et al., 2018; Chen et al., 2019) attempts to improve the discriminative performance of generative models by leveraging invertible architectures, but these methods still underperform their purely discriminative counterparts jointly trained as generative models.

This paper advocates the use of energy based models (EBMs) to help realize the potential of generative models on downstream discriminative problems. While EBMs are currently challenging to work with, they fit more naturally within a discriminative framework than other generative models and facilitate the use of modern classifier architectures. Figure 1 illustrates an overview of the architecture, where the logits of a classifier are re-interpreted to define the joint density of data points and labels and the density of data points alone. The contributions of this paper can be summarized as: 1) We present a novel and intuitive framework for joint modeling of labels and data. 2) Our models considerably outperform previous state-of-the-art hybrid models at both generative and discriminative modeling. 3) We show that the incorporation of generative modeling gives our models improved calibration, out-of-distribution detection, and adversarial robustness, performing on par with or better than hand-tailored methods for multiple tasks.

2. ENERGY BASED MODELS:

Energy based models (LeCun et al., 2006) hinge on the observation that any probability density p(x) for x ∈ R D can be expressed as

EQUATION

where E θ (x) : R D → R, known as the energy function, maps each point to a scalar, and Z(θ) = x exp(-E θ (x))

is the normalizing constant (with respect to x) also known as the partition function. Thus, one can parameterize an EBM using any function that takes x as the input and returns a scalar.

For most choices of E θ , one cannot compute or even reliably estimate Z(θ), which means estimating normalized densities is intractable and standard maximum likelihood estimation of the parameters, θ, is not straightforward. Thus, we must rely on other methods to train EBMs. We note that the derivative of the log-likelihood for a single example x with respect to θ can be expressed as

EQUATION

where the expectation is over the model distribution. Unfortunately, we cannot easily draw samples from p θ (x), so we must resort to MCMC to use this gradient estimator. This approach was used to train some of the earliest EBMs. For example, Restricted Boltzmann Machines (Hinton, 2002) were trained using a block Gibbs sampler to approximate the expectation in Eq. ( 2).

Despite a long period of little development, there has been recent work using this method to train large-scale EBMs on high-dimensional data, parameterized by deep neural networks (Nijkamp et al., 2019b; a; Du & Mordatch, 2019; Xie et al., 2016) . These recent successes have approximated the expectation in Eq. ( 2) using a sampler based on Stochastic Gradient Langevin Dynamics (SGLD) (Welling & Teh, 2011) which draws samples following

EQUATION

where p 0 (x) is typically a Uniform distribution over the input domain and the step-size α should be decayed following a polynomial schedule. In practice the step-size, α, and the standard deviation of is often chosen separately leading to a biased sampler which allows for faster training. See Appendix H.1 for further discussion of samplers for EBM training.

3. WHAT YOUR CLASSIFIER IS HIDING:

In modern machine learning, a classification problem with K classes is typically addressed using a parametric function, f θ : R D → R K , which maps each data point x ∈ R D to K real-valued numbers known as logits. These logits are used to parameterize a categorical distribution using the so-called Softmax transfer function:

EQUATION

where f θ (x)[y] indicates the y th index of f θ (x), i.e., the logit corresponding the the y th class label.

Our key observation in this work is that one can slightly re-interpret the logits obtained from f θ to define p(x, y) and p(x) as well. Without changing f θ , one can re-use the logits to define an energy based model of the joint distribution of data point x and labels y via:

EQUATION

where Z(θ) is the unknown normalizing constant and

E θ (x, y) = -f θ (x)[y].

By marginalizing out y, we obtain an unnormalized density model for x as well,

EQUATION

)

Notice now that the LogSumExp(•) of the logits of any classifier can be re-used to define the energy function at a data point x as

EQUATION

Unlike typical classifiers, where shifting the logits f θ (x) by an arbitrary scalar does not affect the model at all, in our framework, shifting the logits for a data point x will affect log p θ (x). Thus, we are making use of the extra degree of freedom hidden within the logits to define the density function over input examples as well as the joint density among examples and labels. Finally, when we compute p θ (y | x) via p θ (x, y)/p θ (x) by dividing Eq. ( 5) to Eq. ( 6), the normalizing constant cancels out, yielding the standard Softmax parameterization in Eq. ( 4). Thus, we have found a generative model hidden within every standard discriminative model! Since our approach proposes to reinterpret a classifier as a Joint Energy-based Model we refer to it throughout this work as JEM.

4. OPTIMIZATION:

We now wish to take advantage of our new interpretation of classifier architectures to gain the benefits of generative models while retaining strong discriminative performance. Since our model's parameterization of p(y|x) is normalized over y, it is simple to maximize its likelihood as in standard classifier training. Since our models for p(x) and p(x, y) are unnormalized, maximizing their likelihood is not as easy. There are many ways we could train f θ to maximize the likelihood of the data under this model. We could apply the gradient estimator of Equation 2 to the likelihood under the joint distribution of Equation 5. Using Equations 6 and 4, we can also factor the likelihood as

EQUATION

The estimator of Equation 2 is biased when using a MCMC sampler with a finite number of steps.

Given that the goal of our work is to incorporate EBM training into the standard classification setting, the distribution of interest is p(y|x). For this reason we propose to train using the factorization of Equation 8 to ensure this distribution is being optimized with an unbiased objective. We optimize p(y|x) using standard cross-entropy and optimize log p(x) using Equation 2with SGLD where gradients are taken with respect to LogSumExp y (f θ (x)[y]). We find alternative factorings of the likelihood lead to considerably worse performance as can be seen in Section 5.1.

Following Du & Mordatch (2019) we use persistent contrastive divergence (Tieleman, 2008) to estimate the expectation in the right-hand-side of Equation 2 since it gives an order of magnitude savings in computation compared to seeding new chains at each iteration as in Nijkamp et al. (2019b) . This comes at the cost of decreased training stability. These trade-offs are discussed in Appendix H.2.

5. APPLICATIONS:

We completed a thorough empirical investigation to demonstrate the benefits of JEM over standard classifiers. First, we achieved performance rivaling the state of the art in both discriminative and generative modeling. Even more interesting, we observed a number of benefits related to the practical application of discriminative models including improved uncertainty quantification, outof-distribution detection, and robustness to adversarial examples. Generative models have been long-expected to provide these benefits but have never been demonstrated to do so at this scale. (Du & Mordatch, 2019) , SNGAN (Miyato et al., 2018) , NCSN (Song & Ermon, 2019) Figure 2 : CIFAR10 classconditional samples.

All architectures used are based on Wide Residual Networks (Zagoruyko & Komodakis, 2016) where we have removed batch-normalization1 to ensure that our models' outputs are deterministic functions of the input. This slightly increases classification error of a WRN-28-10 from 4.2% to 6.4% on CIFAR10 and from 2.3 to 3.4% on SVHN.

All models were trained in the same way with the same hyper-parameters which were tuned on CIFAR10. Intriguingly, the SGLD sampler parameters found here generalized well across datasets and model architectures. All models are trained on a single GPU in approximately 36 hours. Full experimental details can be found in Appendix A. First, we show that a given classifier architecture can be trained as an EBM to achieve competitive performance as both a classifier and a generative model. We train JEM on CIFAR10, SVHN, and CIFAR100 and compare against other hybrid models as well as standalone generative and discriminative models. We find JEM performs near the state of the art in both tasks simultaneously, outperforming other hybrid models (Table 1 ).

Given that we cannot compute normalized likelihoods, we present inception scores (IS) (Salimans et al., 2016) and Frechet Inception Distance (FID) (Heusel et al., 2017) as a proxy for this quantity. We find that JEM is competitive with SOTA generative models at these metrics. These metrics are not commonly reported on CIFAR100 and SVHN so we present accuracy and qualitative samples on these datasets. Our models achieve 96.7% and 72.2% accuracy on SVHN and CIFAR100, respectively. Samples from JEM can be seen in Figures 2, 3 and in Appendix C.

JEM is trained to maximize the likelihood factorization shown in Eq. 8. This was to ensure that no bias is added into our estimate of log p(y|x) which can be computed exactly in our setup. Prior work (Du & Mordatch, 2019; Xie et al., 2016) proposes to factorize the objective as log p(x|y)+log p(y). In these works, each p(x|y) is a separate EBM with a distinct, unknown normalizing constant, meaning that their model cannot be used to compute p(y|x) or p(x). This explains why the model of Du & Mordatch (2019) (we will refer to this model as IGEBM) is not a competitive classifier. As an ablation, we trained JEM to maximize this objective and found a considerable decrease in discriminative performance (see Table 1 , row 4).

5.2. CALIBRATION:

A classifier is considered calibrated if its predictive confidence, max y p(y|x), aligns with its misclassification rate. Thus, when a calibrated classifier predicts label y with confidence .9 it should have a 90% chance of being correct. This is an important feature for a model to have when deployed in real-world scenarios where outputting an incorrect decision can have catastrophic consequences. The classifier's confidence can be used to decide when to output a prediction or deffer to a human, for example. Here, a well-calibrated, but less accurate classifier can be considerably more useful than a more accurate, but less-calibrated model.

Accuracy Confidence Confidence:

Figure 4 : CIFAR100 calbration results. ECE = Expected Calibration Error (Guo et al., 2017) , see Appendix E.1.

While classifiers have grown more accurate in recent years, they have also grown considerably less calibrated (Guo et al., 2017) . Contrary to this behavior, we find that JEM notably improves classification while retaining high accuracy.

We focus on CIFAR100 since SOTA classifiers achieve approximately 80% accuracy. We train JEM on this dataset and compare to a baseline of the same architecure without EBM training. Our baseline model achieves 74.2% accuracy and JEM achieves 72.2% (for reference, a ResNet-110 achieves 74.8% accuracy (Zagoruyko & Komodakis, 2016) ). We find the baseline model is very poorly calibrated outputting highly over-confident predictions. Conversely, we find JEM produces a nearly perfectly calibrated classifier when measured with Expected Calibration Error (see Appendix E.1). Compared to other calibration methods such as Platt scaling (Guo et al., 2017) , JEM requires no additional training data. Results can be seen in Figure 4 and additional results can be found in Appendix E.2.

5.3. OUT-OF-DISTRIBUTION DETECTION:

In general, out-of-distribution (OOD) detection is a binary classification problem, where the model is required to produce a score s θ (x) ∈ R, where x is the query, and θ is the set of learnable parameters. We desire that the scores for in-distribution examples are higher than that out-of-distribution examples. Typically for evaluation, threshold-free metrics are used, such as the area under the receiver-operating curve (AU-ROC) (Hendrycks & Gimpel, 2016) . There exist a number of distinct OOD detection approaches to which JEM can be applied. We expand on them below. Further results and experimental details can be found in Appendix F.2.

5.3.1. INPUT DENSITY:

A natural approach to OOD detection is to fit a density model on the data and consider examples with low likelihood to be OOD. While intuitive, this approach is currently not competitive on highdimensional data. Nalisnick et al. (2018) showed that tractable deep generative models such as Kingma & Dhariwal (2018) and Salimans et al. (2017) can assign higher densities to OOD examples than in-distribution examples. Further work (Nalisnick et al., 2019) shows examples where the densities of an OOD dataset are completely indistinguishable from the in-distribution set, e.g., see Table 2 , column 1. Conversely, Du & Mordatch (2019) have shown that the likelihoods from EBMs can be reliably used as a predictor for OOD inputs. As can be seen in Table 2 column 2, JEM consistently assigns higher likelihoods to in-distribution data than OOD data. One possible explanation for JEM's further improvement over IGEBM is its ability to incorporate labeled information during training while also being able to derive a principled model of p(x). Intriguingly, Glow does not appear to benefit in the same way from this supervision as is demonstrated by the little difference between our unconditional and class-conditional Glow results. Quantitative results can be found in Table 3 (top).

5.3.2. PREDICTIVE DISTRIBUTION:

Many successful approaches have utilized a classifier's predictive distribution for OOD detection (Gal & Ghahramani, 2016; Wang et al., 2018; Liang et al., 2017) . A useful OOD score that can be derived from this distribution is the maximum prediction probability: s θ (x) = max y p θ (y|x) (Hendrycks & Gimpel, 2016) . It has been demonstrated that OOD performance using this score is highly correlated with a model's classification accuracy. Since JEM is a competitive classifier, we find it performs on par (or beyond) the performance of a strong baseline classifier and considerably outperforms other generative models. Results can be seen in Table 3 (middle).

5.3.3. A NEW SCORE: APPROXIMATE MASS:

It has been recently proposed that likelihood may not be enough for OOD detection in high dimensions (Nalisnick et al., 2019) . It is possible for a point to have high likelihood under a distribution yet be nearly impossible to be sampled. Real samples from a distribution lie in what is known as the "typical" set. This is the area of high probability mass. A single point may have high density but if the surrounding areas have very low density, then that point is likely not in the typical set and therefore likely not a sample from the data distribution. For a high-likelihood datapoint outside of the typical set, we expect the density to change rapidly around it, thus the norm of the gradient of the log-density will be large compared to examples in the typical set (otherwise it would be in an area of high mass). We propose an alternative OOD score based on this quantity:

EQUATION

)

For EBMs (JEM and IGEBM), we find this predictor greatly outperforms our own and other generative model's likelihoods -see Table 2 column 3. For tractable likelihood methods we find this predictor is anti-correlated with the model's likelihood and neither is reliable for OOD detection.

Results can be seen in Table 3 (bottom).

5.4. ROBUSTNESS:

Recent work (Athalye et al., 2017) has demonstrated that classifiers trained to be adversarially robust can be re-purposed to generate convincing images, do in-painting, and translate examples from one class to another. This is done through an iterative refinement procedure, quite similar to the SGLD used to sample from EBMs. We also note that adversarial training (Goodfellow et al., 2014) bears many similarities to SGLD training of EBMs. In both settings, we use a gradient-based optimization procedure to generate examples which activate a specific high-level network activation, then optimize the weights of the network to minimize the generated example's effect on that activation. Further connections have been drawn between adversarial training and regularizing the gradients of the network's activations around the data (Simon-Gabriel et al., 2018) . This is similar to the objective of Score Matching (Hyvärinen, 2005) which can also be used to train EBMs (Kingma & Lecun, 2010; Song & Ermon, 2019) .

Given these connections one may wonder if a classifier derived from an EBM would be more robust to adversarial examples than a standard model. This behavior has been demonstrated in prior work on EBMs (Du & Mordatch, 2019) and is therefore of limited practical application for this purporse. Similarly, we find JEM achieves considerable robustness without sacrificing discriminative performance. This insight has been used to detect and robustly classify adversarial examples with generative models (Song et al., 2017; Li et al., 2018; Fetaya et al., 2019) . The state-of-theart method for adversarial robustness on MNIST classifies by comparing an input to samples generated from a classconditional generative model (Schott et al., 2018) . This can be thought of as classifying an example similar to the input but from an area of higher density under the model's learned distribution. This refined input resides in areas where the model has already "seen" sufficient data and is thus able to accurately classify. Albeit promising, this family of methods has not been able to scale beyond MNIST due to a lack of sufficiently powerful conditional generative models. We believe JEM can help close this gap. We propose to run a few iterations of our model's sampling procedure seeded at a given input. This should be able to transform low-probability inputs to a nearby point of high probability, "undoing" any adversarial attack and enabling the model to classify robustly.

5.4.1. IMPROVED ROBUSTNESS THROUGH EBM TRAINING:



Perturbation Robustness:

We run a number of powerful adversarial attacks on our CIFAR10 models. We run a white-box PGD attack, giving the attacker access to the gradients through our sampling procedure2 . Because our sampling procedure is stochastic, we compute the "expectation over transformations" Athalye et al. (2018) , the expected gradient over multiple runs of the sampling procedure. We also run gradient-free black-box attacks; the boundary attack (Brendel et al., 2017) and the brute-force pointwise attack (Rauber et al., 2017) . All attacks are run with respect to the L 2 and L ∞ norms and we test JEM with 0, 1, and 10 steps of sampling seeded at the input.

Results from the PGD experiments can be seen in Figure 5 . Experimental details and remaining results, including gradient-free attacks, can be found in Appendix G. Our model is considerably more robust than a baseline with standard classifier training. With respect to both norms, JEM delivers considerably improved robustness when compared to the baseline but for many epsilons falls below state-of-the-art adversarial training (Madry et al., 2017; Santurkar et al., 2019) and the state-of-the-art certified robutness method of Salman et al. ( 2019) ("RandAdvSmooth" in Figure 5 ). We note that each of these baseline methods is trained to be robust to the norm through which it is being attacked and it has been shown that attacking an L ∞ adversarially trained model with an L 2 adversary decreases robustness considerably (Madry et al., 2017) . However, we attack the same JEM model with both norms and observe competitive robustness in both cases.

JEM with 0 steps refinement is noticeably more robust than the baseline model trained as a standard classifier, thus simply adding EBM training can produce more robust models. We also find that increasing the number of refinement steps further increases robustness to levels at robustness-specific approaches. We expect that increasing the number of refinement steps will lead to more robust models but due to computational constraints we could not run attacks in this setting.

Figure 6 : Distal Adversarials.

Confidently classified images generated from noise, such that: p(y = "car"|x) > .9.

Distal Adversarials Another common failure mode of non-robust models is their tendency to classify nonsensical inputs with high confidence. To analyze this property, we follow Schott et al. (2018) . Starting from noise we generate images to maximize p(y = "car"|x). Results are shown in figure 6 . The baseline confidently classifies unstructured noise images. The L 2 adversarially trained ResNet with = 0.5 (Santurkar et al., 2019) confidently classifies somewhat structured, but unrealistic images. JEM does not confidently classify nonsensical images, so instead, car attributes and natural image properties visibly emerge.

6. LIMITATIONS:

Energy based models can be very challenging to work with. Since normalized likelihoods cannot be computed, it can be hard to verify that learning is taking place at all. When working in domains such as images, samples can be drawn and checked to assess learning, but this is far from a generalizable strategy. Even so, these samples are only samples from an approximation to the model so they can only be so useful. Furthermore, the gradient estimators we use to train JEM are quite unstable and are prone to diverging if the sampling and optimization parameters are not tuned correctly. Regularizers may be added (Du & Mordatch, 2019) to increase stability but it is not clear what effect they have on the final model. The models used to generate the results in this work regularly diverged throughout training, requiring them to be restarted with lower learning rates or with increased regularization. See Appendix H.3 for a detailed description of how these difficulties were handled.

While this may seem prohibitive, we believe the results presented in this work are sufficient to motivate the community to find solutions to these issues as any improvement in the training of energy based models will further improve the results we have presented in this work.

7. RELATED WORK:

Prior work (Xie et al., 2016) made a similar observation to ours about classifiers and EBMs but define the model differently. They reinterpret the logits to define a class-conditional EBM p(x|y), similar to Du & Mordatch (2019) . This setting requires additional parameters to be learned to derive a classifier and an unconditional model. We believe this subtle distinction is responsible for our model's success. The model of (Song & Ou, 2018 ) is similar as well but is trained using a GAN-like generator and is applied to different applications. Also related are Introspective Networks (Jin et al., 2017; Lee et al., 2018) which have drawn a similar connection between discriminative classifiers and generative models. They derive a generative model from a classifier which learns to distinguish between data and negative examples generative via an MCMC-like procedure. Training in this way has also been shown to improve adversarial robustness.

Our work builds heavily on Nijkamp et al. (2019b; a) ; Du & Mordatch (2019) which scales the training of EBMs to high-dimensional data using Contrastive Divergence and SGLD. While these works have pushed the boundaries of the types of data to which we can apply EBMs, many issues still exist. These methods require many steps of SGLD to take place at each training iteration. Each step requires approximately the same amount of computation as one iteration of standard discriminitive model training, therefore training EBMs at this scale is orders of magnitude slower than training a classifier -limiting the size of problems we can attack with these methods. There exist orthogonal approaches to training EBMs which we believe have promise to scale more gracefully.

Score matching (Hyvärinen, 2005) attempts to match the derivative of the model's density with the derivative of the data density. This approach saw some development towards high-dimensional data (Kingma & Lecun, 2010) and recently has been successfully applied to large natural images (Song & Ermon, 2019) . This approach required a model to output the derivatives of the density function, not the density function itself, so it is unclear what utility this model can provide to the applications we have discussed in this work. Regardless, we believe this is a promising avenue for further research. Noise Contrastive Estimation (Gutmann & Hyvärinen, 2010) rephrases the density estimation problem as a classification problem, attempting to distinguish data from a known noise distribution. If the classifier is properly structured, then once the classification problem is solved, an unnormalized density estimator can be derived from the classifier and noise distribution. While this method has been recently extended (Ceylan & Gutmann, 2018) , these methods are challenging to extend to high-dimensional data.

8. CONCLUSION AND FURTHER WORK:

In this work we have presented JEM, a novel reinterpretation of standard classifier architectures which retains the strong performance of SOTA discriminative models while adding the benefits of generative modeling approaches. Our work is enabled by recent work scaling techniques for training EBMs to high dimensional data. We have demonstrated the utility of incorporating this type of training into discriminative models. While there exist many issues in training EBMs we hope the results presented here will encourage the community to improve upon current approaches.

A TRAINING DETAILS:

We train all models with the Adam optimizer (Kingma & Ba, 2014) for 150 epochs through the dataset using a staircase decay schedule. All network architecutres are based on WideResNet-28-10 with no batch normalization. We generate samples using PCD with hyperparameters in Table 4 . We evolve the chains with 20-steps of SGLD per iteration and with probability .05 we reiniatilize the chains with uniform random noise. For preprocessing, we scale images to the range [-1, 1] and add Gaussian noise of stddev = .03. Pseudo-code for our training procedure is in Algorithm 1.

When training via contrastive divergence there are a few different ways one could potentially draw samples from p θ (x). We could:

1. Sample y ∼ p(y) then sample x ∼ p θ (x|y) via SGLD with energy E(x|y) = -f θ (x) [y] then throw away y.

2. Sample x ∼ p θ (x) via SGLD with energy E(x) = -LogSumExp y f θ (x)[y].

We experimented with both methods during training and found that while method 1 produced more visually appealing samples (from a human's perspective), method 2 produced slightly stronger discirminative performance -92.9% vs. 91.2% accuracy on CIFAR10. For this reason we use method 2 in all results presented.

Algorithm 1 JEM training: Given network f θ , SGLD step-size α, SGLD noise σ, replay buffer B, SGLD steps η, reinitialization frequency ρ 1: while not converged do 

L clf (θ) = xent(f θ (x), y) 4: Sample x 0 ∼ B with probability 1 -ρ, else x 0 ∼ U(-1, 1)

Initialize SGLD 5:

for t ∈ [1, 2, . . . , η] do SGLD 6:

x t = x t-1 + α • ∂LogSumExp y (f θ ( xt-1)[y ]) ∂ xt-1 + σ • N (0, I) 7:

end for 8:

L gen (θ) = LogSumExp y (f (x)[y ]) -LogSumExp y (f ( x t )[y ])

Surrogate for Eq 2 9:

L(θ) = L clf (θ) + L gen (θ)

10:

Obtain gradients 

B SAMPLE QUALITY EVALUTION:

In this section we describe the details for reproducing the Inception Score (IS) and FID results reported in the paper. First we note that both IS and FID are scores computed based on a pretrained classifier network, and thus can be very dependent on the exact model/code repository used. For a more detailed discussion on the variability of IS, please refer to Barratt & Sharma (2018) . To gauge our model against the other papers, we document our attempt to fairly compare the scores across

D QUALITATIVE ANALYSIS OF SAMPLES:

Visual quality is difficult to quantify. Of the known metrics like IS and FID, using samples that have higher p(y|x) values results in higher scores, but not necessary if we use samples with higher log p(x). However, this is likely because of the downfalls of the evaluation metrics themselves rather than reflecting true sample quality.

Based on our analysis (below), we find 1. Our log p(x) model assigns values that cluster around different means for different classes.

The class automobiles has the highest log p(x). Of all generated samples, all top 100 samples are of this class.

2. Given the class, the samples that have higher log p(x) values all have white background and centered object, and lower log p(x) samples have colorful (e.g., forest-like) background.

3. Of all samples, higher p(y|x) values means clearly centered objects, and lower p(y|x) otherwise. Expected Calibration Error (ECE) is a metric to measure the calibration of a classifier. It works by first computing the confidence, max y p(y|x i ), for each x i in some dataset. We then group the items into equally spaced buckets {B m } M m=1 based on the classifier's output confidence. For example, if M = 20, then B 0 would represent all examples for which the classifier's confidence was between 0.0 and 0.05.

We then define:

EQUATION

)

where For a perfectly calibrated classifier, this value will be 0 for any choice of M . In our analysis, we choose M = 20 throughout.

E.2 FURTHER RESULTS:

We find that JEM also improves calibration on CIFAR10 as can be seen in Table 13 . There we see an improvement in calibration, but both classifiers are well calibrated because their accuracy is so high.

In a more interesting experiment, we limit the size of the training set to 4,000 labeled examples. In this setting the accuracy drops to 78.0% and 74.9% in the baseline and JEM, respectively. Given the JEM can be trained on unlabeled data, we treat the remainder of the training set as unlabeled and train in a semi-supervised manner. We find this gives a noticeable boost in the classifier's calibration as seen in Figure 13 . Surprisingly this did not improve generalization. We leave exploring this phenomenon for future work. To obtain OOD results for unconditional Glow, we used the pre-trained model and implementation of https://github.com/y0ast/Glow-PyTorch. We trained a Class-Conditional model as well using this codebase which was used to generate the class-conditional OOD results.

We obtained the IGEBM of Du & Mordatch (2019) from their open-source implementation at https://github.com/openai/ebm_code_release. For likelihood and likelihoodgradient OOD scores we used their pre-trained cifar10 large model uncond model. We were able to replicate the likelihood based OOD results presented in their work. We implemented our likelihood-gradient approximate-mass score on top of their codebase. For predictive distribution based OOD scores we used their cifar cond model which was the model used in their work to generate their robustness results.

F.2 FURTHER RESULTS:

Figure 7 contains results on two datasets, Constant and Uniform, which were omitted for space. Most models perform very well at the Uniform dataset. On the Constant dataset (all examples = 0) generative models mainly fail -with JEM being the only one whose likelihoods can be used to derive a predictive score function for OOD detection. Intrestinly, we could not obtain approximate mass scores on this dataset from the Glow models due to numerical stability issues. 

G ATTACK DETAILS AND FURTHER ROBUSTNESS RESULTS:

We use foolbox (Rauber et al., 2017) for our experiments. PGD uses binary search to determine minimal epsilons for every input and we plot the resulting robustness-distortion curves. PGD runs with 20 random restarts and 40 iterations. For the boundary attack, we run default foolbox settings with one important difference. The random initialization often fails for JEM and thus we initialize the attack with a correclty classified input of another class. This other class is chosen based on the top-2 prediction for the image to be attacked. As all our attacks are expensive to run, we only attacked 300 randomly chosen inputs. The same randomly chosen inputs were used to attack each model.

In Figure 14 we see the results of the boundary attack and pointwise attack on JEM and a baseline.

The main point to running these attacks was to demonstrate that our model was not able to "cheat" by having vanishing gradients through our gradient-based sampling procedure. Since PGD was more successful than these gradient-free methods, this is clearly not the case and the attacker was able to use the gradients of the sampling procedure to attack our model. Further, we observe the same behavior across all attacks; the EBM with 0 steps sampling is more robust than the baseline and the robustness increases as we add more steps of sampling.

We also compare JEM to the IGEBM of Du & Mordatch (2019) with 10 steps of sampling refinement, see Figure 15 . We run the same gradient-based attacks on their model and find that despite not having competitive clean accuracy, it is quite robust to large attacks -especially with respect to the L ∞ norm. After = 12 their model is more robust than ours and after = 18 it is more robust than the adversarial training baseline. With respect to the L 2 norm their model is more robust than the adversarial training baseline above = 280 but remains less robust than JEM until = 525.

We believe these results demonstrate that EBMs are a compelling class of models to explore for further work on building robust models. 

G.1 EXPECTATION OVER TRANSFORMATIONS:

Our SGLD-based refinement procedure is stochastic in nature and it has been shown that stochastic defenses to adversarial attacks can provide a false sense of security (Athalye et al., 2018) . To deal with this, when we attack our stochastically refined classifiers, we average the classifier's predictions over multiple samples of this refinement procedure. This makes the defense more deterministic and easier to attack. We redefine the logits of our classifier as:

EQUATION

where we have defined SGLD(x, k) as an SGLD chain run for k steps seeded at x. Intuitively, we draw n different samples {x i } n i=1 from our model seeded at input x, then compute log p(y|x i ) for each of these samples, then average the results. We then attack these averaged logits with PGD to generate the results in Figure 5 . We experimented with different numbers of samples and found that 10 samples yields very similar results to 5 samples on JEM with one refinement step (see Figure 16 ). Because 10 samples took very long to run on the JEM model with ten refinement steps, we settled on using 5 samples in the results reported in the main body of the paper. 

G.2 TRANSFER ATTACKS:

We would like to see if JEM's refinement procedure can correct adversarial perturbed inputs -inputs which cause the model to fail. To do this, we generate a series of adversarial examples for JEM-0, with respect to the l ∞ norm, and test the accuracy of JEM-{1,10} on these examples. Ideally, with further refinement the accuracy will increase. The results of this experiment can be seen in Figure 17 . We see here that JEM's refinement procedure can correct for adversarial perturbations.

H A DISCUSSION ON SAMPLERS H.1 IMPROPER SGLD Recall the transition kernel of SGLD:

x 0 ∼ p 0 (x) In the proper formulation of this sampler (Welling & Teh, 2011) , the step-size and the variance of the Gaussian noise are related Var( ) = α. If the stepsize is decayed with a polynomial schedule, then samples from SGLD converge to samples from our unnomralized density as the number of steps goes to ∞.

x i+1 = x i - α 2 ∂E θ (x i ) ∂θ + , ∼ N (0, α)

In practice, we approximate these samples with a sampler that runs for a finite number of steps. When using the proper step-size to noise ratio, the signal from the gradient is overtaken by the noise when step-sizes are large enough to be informative. In practice the sampler is typically "relaxed" in that different values are used for the step-size and the amount of Guassian noise added -typically the amount of noise is significantly reduced.

While we are no longer working with a valid MCMC sampler, this approximation has been successfully applied in practice in most recent work scaling EBM training to high dimensional data (Nijkamp et al., 2019b; a; Du & Mordatch, 2019) with the exception of Song & Ermon (2019) (which develops a clever work-around). The model they train is actually an ensemble of models trained on data with different amounts of noise added. They use a proper SGLD sampler decaying the step size as they sample, moving from their high-noise models to their low-noise models. This provides one possible explanation for the compelling results of their model.

In our work we have set the step-size α = 2 and draw ∼ N (0, .01 2 ). We have found these parameters to work well across a variety of datasets, domains, architectures, and sampling procedures (persistent vs. short-run). We believe they are a decent "starting place" for energy-functions parameterized by deep neural networks.

H.2 PERSISTENT OR SHORT-RUN CHAINS?:

Both persistent and short-run markov chains have been able to succesfully train EBMs. Nijkamp et al. (2019a) presents a careful study of various samplers which can be used and the tradeoffs one makes when choosing one sampler over another. In our work we have found that if computation allows, short-run MCMC chains are preferable in terms of training stability. Given that each step of SGLD requires approximately the computation of 1 training iteration of a standard classifier we are incentivized to find a sampler which can stably train EBMs requiring as few steps as possible per training iteration.

In our experiments we found the smallest number of SGLD steps we could take to stably train an EBM at the scale of this work was 80 steps. Even so, these models eventually would diverge late into training. At 80 steps, we found the cost of training to be prohibitively high compared to a standard classifier.

We found that by using persistent markov chains, we could further reduce the number of steps per iteration to 20 and still allow for relatively stable training. This gave a 4x speedup over our fastest short-run MCMC sampler. Still, this PCD sampler was noticebly less stable than the fastest shortrun sampler we could use but we found the multiple factor increase in speed to be a worth-while trade-off.

If time allows, we recommend using a short-run MCMC sampler with a large enough number of steps to be stable. Given that is not always possible on problems of scale, PCD can be made to work more efficiently, but at the cost of a greater number of stability-related hyper-parameters. These additional parameters include the buffer size and the re-initialization frequency of the Markov chains. We found both to be important for training stability and found no general recipe for which to set them. We ran most of our experiments with re-initialization frequency at 5%.

A particualrly interesting observation we discovered while using PCD is that the model would use the length of the markov chains to encode semantic information. We found that when training models on CIFAR10, when chains were young they almost always could be identified as frogs.

When chains were old they could almost always be identified as cars. This behavior is likely some degeneracy of PCD which would not be possible with a short-run MCMC since all chains have the same length.

H.3 DEALING WITH INSTABILITY:

Training a model with the gradient estimator of Eq. ( 2) can be quite unstable -especially when combined with other objective as was the case with all models presented in this work. There exists a "stable region" of sorts when training these models where the energy values of the true data are in the same range as the energy values of the generated samples. Intuitively, if the generated samples create energies that are not trivially separated from the training data, then real learning has to take place. Nijkamp et al. (2019b; a) provide a careful analysis of this and we refer the reader there for a more in-depth analysis.

We find that when using PCD occasionally throughout training a sample will be drawn from the replay buffer that has a considerably higher-than average energy (higher than the energy of a random initialization). This causes the gradients w.r.t this example to be orders of magnitude larger than gradients w.r.t the rest of the examples and causes the model to diverge. We tried a number of heuristic approaches such as gradient clipping, energy clipping, ignoring examples with atypical energy values, and many others but could not find an approach that stabilized training and did not hurt generative and discriminative performance.

The only two approaches we found to consistently work to increase stability of a model which has diverged is to 1) decrease the learning rate and 2) increase the number of SGLD steps in each PCD iteration. Unfortunately, both of these approaches slow down learning. We also had some success simply restarting models from a saved checkpoint with a different random seed. This was the main approach taken unless the model was late into training. In this case, random restarts were less effective and we increased the number of SGLD steps from 20 to 40 which stabilized training.

While we are very optimistic about the future of large-scale EBMs we believe these are the most important issues that must be addressed in order for these models to be succeful.

:

This was done to remove sources of stochasticity in early experiments. Since then we have been able to successfully train Joint-EBMs with Batch Normalization and other forms of stochastic regularization (such as dropout) without issue. We leave the incorporation of these methods to further work.

In Du & Mordatch (2019) the attacker was not given access to the gradients of the refinement procedure. We re-run these stronger attacks on their model as well and provide a comparison in Appendix G.

Code taken from https://github.com/y0ast/Glow-PyTorch

Figures and Tables:

Figure 1: Visualization of our method, JEM, which defines a joint EBM from classifier architectures.

Figure 3: Classconditional samples.

Figure 5: Adversarial Robustness Results with PGD attacks. JEM adds considerable robustness.

Figure 9: Each row corresponds to 1 class, subfigures corresponds to different values of log p(x). left: highest, mid: random, right: lowest.

Figure 10: Histograms (oriented horizontally for easier visual alignment) of log p(x) arranged by class.

Figure 11: left: samples with highest log p(x), right: left: samples with lowest log p(x)

n is the number of examples in the dataset, acc(B m ) is the averaged accuracy of the classifier of all examples in B m and conf(B m ) is the averaged confidence over all examples in B m .

Figure 13: CIFAR10 Calibration results

Figure 14: Gradient-free adversarial attacks.

Figure 15: PGD attacks comparing JEM to the IG EBM of Du & Mordatch (2019).

Figure16: Comparing the effect of the number of samples in the EOT attack. We find negligible difference between 5 and 10 for JEM-1 (red and green curves).

Figure17: PGD transfer attack L ∞ . We attack JEM-0 and evaluate success of the same adversarial examples under JEM-1 and JEM-10. Whenever an adversarial example is refined back to its correct class, we set the distance to infinity. Note that the adversarial examples do not transfer well from JEM-0 to JEM-1/-10.

but their work did not produce a competitive discriminative model

Table content: <table><tr><td>Glow log p(x)</td><td>JEM log p(x)</td><td>Approx. Mass JEM</td></tr><tr><td>SVHN</td><td/><td/></tr><tr><td>CIFAR100</td><td/><td/></tr><tr><td>CelebA</td><td/><td/></tr></table>

OOD Detection Results. Models trained on CIFAR10. Values are AUROC.

Table content: <table/>

OOD Detection Results. Values are AUROC.

Table content: <table><tr><td>CIFAR10</td></tr></table>

Peer review (iclr2020):
This paper introduces the idea of energy based model to the traditional classifier, and proposes a new framework to improve the performances of the model in multiple aspects. The idea of reinterpreting the traditional classifier is very interesting, and the experiments show some good results of the proposed method. Here are my main concerns of the current paper: 1. The training procedure seems to be very sensitive, and the SGLD may take a long time at each iteration to converge. This may be a big limitation of the proposed method. 2. According to equation (8), the proposed method is having a trade-off between classification and generation, and this seems to be the key to improve the performance of the model in generation by sacrificing some classification accuracy. I think author should emphasize this instead of energy based model. 3. The presentation is not very clear in section 5. What is the task of calibration, and what is the definition of ECE? 4. The robustness guarantee seems too good to be true. Although the authors claim that they allow the attacker to have access to the gradient of SGLD, the SGLD will add noise during the forward process, this will obfuscate the gradient. In this sense, I don’t think the proposed method will have the strong robustness as they claimed. ---------------- Post-Rebuttal Comments: Thanks for addressing my concerns. Although I think the proposed method is not comprehensive to check obfuscated gradients, I do think the current version is a good fit for ICLR, and I decide to increase my score.