Unsupervised Layer-wise Score Aggregation for Textual OOD Detection

Maxime Darrin, Guillaume Staerman, Eduardo Dadalto Câmara Gomes, Jackie Ck Cheung, Pablo Piantanida, Pierre Colombo · OpenReview

Abstract

Out-of-distribution (OOD) detection is a rapidly growing field due to new robustness and security requirements driven by an increased number of AI-based systems. Existing OOD textual detectors often rely on an anomaly score (e.g., Mahalanobis distance) computed on the embedding output of the last layer of the encoder. In this work, we observe that OOD detection performance varies greatly depending on the task and layer output. More importantly, we show that the usual choice (the last layer) is rarely the best one for OOD detection and that far better results could be achieved if the best layer were picked. To leverage this observation, we propose a data-driven, unsupervised method to combine layer-wise anomaly scores. In addition, we extend classical textual OOD benchmarks by including classification tasks with a greater number of classes (up to 77), which reflects more realistic settings. On this augmented benchmark, we show that the proposed post-aggregation methods achieve robust and consistent results while removing manual feature selection altogether. Their performance achieves near oracle's best layer performance.

Introduction

In Natural Language Processing (NLP), Out-of-Distribution (OOD) detection is a critical safety mechanism. It allows models to identify inputs that differ significantly from their training data, preventing confident but incorrect predictions on irrelevant or nonsensical text. Traditionally, OOD detection has focused on the final layer of a neural network, assuming it contains the most refined semantic information. However, recent research suggests that this "last layer" approach is often suboptimal for textual data.

The "Last Layer" Fallacy

In Computer Vision, the final layer is often the most informative for anomaly detection. In contrast, for NLP tasks, the most useful signals for OOD detection are frequently scattered across various layers of the transformer encoder. Some layers may capture syntactic anomalies, while others are more sensitive to semantic shifts.

AUROC performance across layers for various datasets and scoring methods.

As shown in Figure 1, the AUROC performance for different OOD scoring methods—such as Cosine Similarity ($S_C$), Integrated Rank-Weighted depth ($S_{IRW}$), and Mahalanobis distance ($S_M$)—varies significantly across layers. Crucially, the final layer (layer 12) is rarely the peak performer. This variability makes manual layer selection impractical for diverse real-world tasks.

Proposed Method: Unsupervised Layer-wise Aggregation

To address this, we propose a data-driven, unsupervised framework that aggregates anomaly scores from all layers of the encoder. This approach eliminates the need for manual feature selection and leverages the synergistic information present across the entire model "trace."

The four-step pipeline for unsupervised layer-wise score aggregation.

The framework follows a four-step process:

  1. Compute Per-layer Embeddings: Extract hidden states for each layer of the transformer.
  2. Compute Scores Per Class and Layer: Calculate standard OOD scores at every layer, creating a "score trajectory" vector.
  3. Perform Score Aggregation: Use an unsupervised second-stage anomaly detector, such as Isolation Forest (IF) or Local Outlier Factor (LOF), to analyze these trajectories.
  4. Apply Threshold: Flag inputs as OOD if the aggregated score exceeds a learned threshold.

This method is entirely unsupervised, requiring only "normal" in-distribution data during the aggregation training phase to learn typical score patterns.

Experimental Setup and Datasets

To evaluate the robustness of this approach, we introduced MILTOOD-C, a more complex and realistic benchmark than existing datasets. MILTOOD-C is multilingual (English, French, Spanish, German) and includes tasks with a high number of classes (up to 150), such as Banking77. The benchmark covers 27 datasets and 180 distinct In-Distribution vs. Out-of-Distribution scenarios.

Key Results

Our aggregation methods consistently outperformed single-layer baselines and even surpassed "oracle" benchmarks where the single best layer was perfectly selected for each task.

  • FPR Reduction: On the MILTOOD-C benchmark, the proposed methods achieved an average False Positive Rate (FPR) of 0.02 at a 95% True Positive Rate, a significant improvement over the 0.20+ FPR typical of standard baselines.
  • AUROC Performance: The best configurations reached an average AUROC of 0.99, representing a 6% improvement over previous state-of-the-art aggregation methods like "Power Mean."
  • Aggregator Efficacy: Isolation Forest emerged as the most versatile aggregator, providing stable performance gains regardless of the underlying scoring metric used.

Conclusion

By moving beyond the last layer and adopting an unsupervised aggregation strategy, we can significantly enhance the reliability of textual OOD detection. This approach provides a robust, task-agnostic solution that captures the diverse anomaly signals distributed throughout deep transformer architectures, achieving near-optimal performance across a wide range of languages and classification complexities.

Summary updated

BibTeX
@inproceedings{darrin2024unsupervised,
  author = {Maxime Darrin and Guillaume Staerman and Eduardo Dadalto Câmara Gomes and Jackie CK Cheung and Pablo Piantanida and Pierre Colombo},
  title = {{Unsupervised Layer-wise Score Aggregation for Textual OOD Detection}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2024},
  doi = {10.1609/aaai.v38i16.29742},
}
Figure