Abstract
Likelihood-based deep generative models such as score-based diffusion models and variational autoencoders are state-of-the-art machine learning models approximating high-dimensional distributions of data such as images, text, or audio. One of many downstream tasks they can be naturally applied to is out-of-distribution (OOD) detection. However, seminal work by Nalisnick et al. which we reproduce showed that deep generative models consistently infer higher log-likelihoods for OOD data than data they were trained on, marking an open problem. In this work, we analyse using the gradient of a data point with respect to the parameters of the deep generative model for OOD detection, based on the simple intuition that OOD data should have larger gradient norms than training data. We formalise measuring the size of the gradient as approximating the Fisher information metric. We show that the Fisher information matrix (FIM) has large absolute diagonal values, motivating the use of chi-square distributed, layer-wise gradient norms as features. We combine these features to make a simple, model-agnostic and hyperparameter-free method for OOD detection which estimates the joint density of the layer-wise gradient norms for a given data point. We find that these layer-wise gradient norms are weakly correlated, rendering their combined usage informative, and prove that the layer-wise gradient norms satisfy the principle of (data representation) invariance. Our empirical results indicate that this method outperforms the Typicality test for most deep generative models and image dataset pairings.
The Likelihood Paradox in Generative Modeling
In theory, a Deep Generative Model (DGM) like a Variational Autoencoder (VAE) or a Diffusion model should be the perfect tool for Out-Of-Distribution (OOD) detection: if the model assigns a low probability $p(x)$ to an input, it is likely OOD.
However, a famous "likelihood paradox" exists: models trained on complex data (like CIFAR-10 images of trucks and birds) often assign higher likelihoods to simpler, unrelated data (like SVHN house numbers). This happens because likelihood is highly sensitive to image complexity and data representation (e.g., RGB vs. HSV color spaces). This paradox makes raw likelihood a poor metric for safety-critical OOD detection.
Methodology: Leveraging the Fisher Information Metric
To resolve this, the researchers shift the focus from the value of the likelihood to the gradient of the log-likelihood with respect to the model's parameters (the "score"). The intuition is that for training data, gradients should be relatively "flat" as the model has converged. Conversely, an OOD point should exert more leverage, resulting in "steeper" gradients.
To measure this properly, the authors approximate the Fisher Information Metric (FIM). Since the full FIM is computationally intractable for large models, they introduce a layer-wise approximation:
- Diagonal Dominance: They empirically demonstrate that the FIM is diagonally dominant, justifying independent treatment of parameters.
- Layer-wise Grouping: Gradient scales vary significantly between layers. The authors calculate the $L^2$ norm of gradients for each layer separately.
- Density Estimation: They compute the log of the squared $L^2$ norm for each layer and fit a Multivariate Gaussian distribution to these log-norms using a small set of in-distribution data. The OOD score is then the negative log-likelihood under this fitted Gaussian.
Experimental Setup and Datasets
The method was benchmarked using three types of generative models—Glow, Diffusion, and VAEs—across five standard image datasets:
- SVHN (Street View House Numbers)
- CelebA (Celebrity faces)
- GTSRB (German Traffic Signs)
- CIFAR-10 (General objects/animals)
- ImageNet32 (Downsampled ImageNet)
Key Insights
- Representation Invariance: The authors provide a mathematical proof that gradient-based OOD scores are invariant to smooth, invertible transformations of input data. This solves a major flaw in raw likelihood methods, which can be "fooled" by simple coordinate changes.
- Layer-wise Information: By analyzing layers individually, the method captures signals that would be washed out in a global gradient norm. Different layers are often "surprised" by different types of OOD data.
- Weak Correlation: Gradient norms across different layers are only weakly correlated. This independence makes their joint density highly informative, as each layer provides a unique "vote" on the sample's distribution.
- Vulnerability of Diffusion Models: This work is among the first to demonstrate that the likelihood paradox also affects state-of-the-art score-based diffusion models.
Results and Performance
The proposed method was compared against the Typicality Test, the previous standard for likelihood-based OOD detection:
- Glow Models: The method significantly outperformed Typicality. For a model trained on CelebA, detecting SVHN samples reached an AUROC of 0.988, compared to 0.873 for the Typicality test.
- Diffusion Models: The method showed a clear advantage, particularly when using gradients from early diffusion steps where noise levels are low.
- Hyperparameter-Free: Unlike many OOD methods that require tuning temperature or perturbation constants, this approach is "plug-and-play" once the base model is trained.
Limitations and Future Work
While robust, the method's performance is tied to the underlying generative model's quality; it struggled with VAEs that exhibited poor sample quality. Additionally, calculating gradients requires a backpropagation pass per test sample, which introduces some computational overhead compared to simple forward passes. Future research will explore whether these Fisher approximations extend effectively to other modalities, such as Large Language Models (LLMs) or audio data.
Summary updated