VI-OOD: A Unified Representation Learning Framework for Textual Out-of-distribution Detection

Li-Ming Zhan, Bo Liu, Xiao-Ming Wu

Abstract

Deep Neural Networks (DNNs) often exhibit overconfidence when encountering out-of-distribution (OOD) samples, posing significant risks in safety-critical NLP applications. Traditional OOD detection methods typically rely on post-hoc analysis of models trained solely on conditional likelihood $p(y|x)$, which often discards features essential for distribution boundary identification. We present VI-OOD, a variational inference framework that optimizes the joint distribution $p(x, y)$. By leveraging intermediate Transformer layers and a learnable reconstruction target, VI-OOD preserves "redundant" features that are critical for distinguishing between in-distribution (ID) and OOD data.

Introduction

The deployment of Transformer-based models in real-world scenarios—ranging from medical diagnosis to autonomous systems—requires the ability to identify when an input falls outside the model's training distribution. However, standard training objectives focus exclusively on discriminative performance, leading to representations that are "blind" to the nuances of OOD data. When presented with an unknown sample, these models frequently assign high-confidence labels from known classes rather than signaling uncertainty.

The Problem: Biased Representations

Most existing textual OOD detection methods are post-hoc, meaning they attempt to extract uncertainty scores from a pre-trained classifier. The fundamental flaw in this approach lies in the training objective: maximizing the conditional likelihood $p(y|x)$. This process encourages the model to discard any information that does not directly contribute to class separation. Consequently, the resulting latent space is optimized for classification but lacks the generative awareness necessary to define the boundaries of the known data distribution.

The VI-OOD Framework

To address these limitations, we propose VI-OOD (Variational Inference for OOD). This framework shifts the training paradigm from purely discriminative to a joint distribution optimization ($p(x, y)$).

1. Joint Distribution Optimization

VI-OOD optimizes the Evidence Lower Bound (ELBO), which decomposes the training objective into two synergistic components:

  • Supervised Task: A standard classification loss ($p(y|z)$) to maintain high accuracy on ID tasks.
  • Unsupervised Task: A combination of reconstruction loss ($p(x|z)$) and a KL-divergence term. This forces the model to retain information about the input distribution itself, effectively acting as a Variational Autoencoder (VAE) integrated within the classifier.

2. Hierarchical Feature Utilization

Transformers are inherently hierarchical. Our analysis reveals that while the final layers are optimal for classification, they are often "over-fitted" to the task and lose distributional density information. We discovered that intermediate layers (specifically layers 9–13 in a 24-layer RoBERTa-Large model) contain rich, "redundant" features that are vital for OOD detection.

3. Learnable Reconstruction Target

Reconstructing discrete text tokens is computationally expensive and complex. VI-OOD introduces a learnable weight vector $s$ that dynamically aggregates the [CLS] token's hidden states across all layers. This creates a "semantic target" for the decoder to reconstruct, ensuring the model preserves the contextual richness found in the middle of the network without the overhead of full text generation.

Experimental Setup

We evaluated the framework using RoBERTa-Large as the backbone architecture. The model was fine-tuned on the SST-2 (Stanford Sentiment Treebank) dataset as the In-Distribution (ID) source. To test OOD detection capabilities, we utilized the 20NG (20 Newsgroups) dataset. Performance was measured primarily via the Area Under the Receiver Operating Characteristic curve (AUROC).

Key Insights and Results

  • Layer Superiority: Pilot studies confirmed that using hidden states from intermediate layers (9–13) yielded significantly higher AUROC for OOD detection compared to the final layer (Layer 23).
  • Representation Enhancement: VI-OOD is not merely a standalone detector; it improves the underlying latent representations. This makes it compatible with various post-hoc scoring functions, including Maximum Softmax Probability (MSP), Mahalanobis distance, Cosine similarity, and Energy-based scores. In all cases, VI-OOD consistently boosted the performance of these existing methods.
  • Efficiency: By utilizing the Transformer backbone as a shared encoder for both classification and reconstruction, the framework adds minimal computational overhead during inference while providing a more robust "sense of self."

Conclusion and Future Work

VI-OOD demonstrates that incorporating generative objectives into discriminative training allows Transformers to retain the information necessary for robust OOD detection. While the current framework focuses on reconstructing semantic representations, future research will explore scaling this approach to massive decoder-only Large Language Models (LLMs) and investigating more diverse OOD scenarios that reflect real-world distributional shifts. By moving toward $p(x, y)$ optimization, we can build models that are not only accurate but also aware of their own limitations.

Summary updated

BibTeX
@inproceedings{zhan2024vi,
  author = {Li-Ming Zhan and Bo Liu and Xiao-Ming Wu},
  title = {{VI-OOD: A Unified Representation Learning Framework for Textual Out-of-distribution Detection}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2024},
}
Figure