Holistic Sentence Embeddings for Better Out-of-Distribution Detection

Sishuo Chen, Xiaohan Bi, Rundong Gao, Xu Sun

Abstract

Reliable out-of-distribution (OOD) detection is critical for the safe deployment of NLP models. Traditional methods often rely on the final-layer [CLS] token, which is heavily biased toward the specific task it was trained on, potentially discarding broader linguistic information. This paper proposes Avg-Avg, a simple yet effective pooling strategy that averages all token representations across all transformer layers to create a holistic sentence embedding. This training-free, plug-and-play approach significantly improves OOD detection performance, reducing the False Alarm Rate (FAR95) by 9.33% on average across multiple benchmarks.

Introduction

When NLP models encounter inputs that differ significantly from their training data—known as Out-of-Distribution (OOD) samples—they often produce overconfident but incorrect predictions. For safety-critical applications like medical diagnosis or automated customer support, a model must be able to identify its own uncertainty. Current state-of-the-art OOD detection typically utilizes distance-based methods, measuring the Mahalanobis distance between a new input's embedding and the training distribution. However, these methods usually extract embeddings from the [CLS] token of the final layer of a fine-tuned Transformer.

The Challenge of Task-Specific Bias

The authors identify a fundamental flaw in using only the final-layer [CLS] token: fine-tuning. During the fine-tuning process, the model's final layers are optimized to extract features specific to the "In-Distribution" (ID) task (e.g., sentiment analysis). This optimization "squashes" the representation space, prioritizing task-relevant features while discarding general linguistic information. This loss of information makes it harder to distinguish between ID samples and OOD samples that might share some surface-level similarities but differ in broader semantic or syntactic ways.

The Avg-Avg Method

To overcome this bias, the researchers introduce Avg-Avg, a "holistic" embedding approach that leverages the entire model hierarchy. The method consists of two straightforward pooling steps:

  1. Intra-Layer Token Averaging: Instead of using only the [CLS] token, the hidden vectors of all tokens in a sentence are averaged for each layer.
  2. Inter-Layer Combination: These layer-wise representations are then averaged across all intermediate layers of the Transformer.
Ablation study of intra-layer and inter-layer pooling strategies showing the superiority of the Avg-Avg approach.

This approach captures a rich hierarchy of information—from surface-level features in early layers to complex semantics in deeper layers—providing a more comprehensive "fingerprint" of the input sentence.

Experimental Setup

The method was evaluated using several popular Transformer architectures, including BERT, RoBERTa, ALBERT, and DistilRoBERTa. The researchers used a diverse set of In-Distribution (ID) datasets:

  • Sentiment Analysis: SST-2 and IMDB.
  • Question Classification: TREC-10.
  • Topic Classification: 20 Newsgroups (20NG).

OOD performance was tested against various datasets representing different types of shifts, such as domain shifts (WMT-16, Multi30k) and semantic shifts (News Top-5 vs. News Rest).

Results and Discussion

The Avg-Avg strategy demonstrated substantial improvements over existing baselines. As shown in the results table below, the method consistently achieved higher AUROC and lower FAR95 scores across all tested datasets.

Performance comparison of Avg-Avg against state-of-the-art OOD detection methods across various benchmarks.

Key Insights

  • Linguistic Preservation: By including intermediate layers, Avg-Avg retains "task-agnostic" knowledge that is often lost in the final layer. This is particularly effective for detecting background shifts, where the topic remains the same but the style or domain changes.
  • Robustness and Simplicity: While it is theoretically possible to find an "Oracle" combination of layers that performs slightly better, simply averaging all layers is remarkably robust. This makes it a practical choice for real-world deployment where OOD data is not available for tuning.
  • Model Agnostic: The performance gains were consistent across all tested model architectures, proving the generalizability of the holistic embedding approach.

Conclusion

The Avg-Avg pooling strategy offers a simple, training-free way to significantly enhance OOD detection in NLP. By moving beyond the task-biased final-layer [CLS] token and embracing a holistic view of the Transformer's internal representations, models can better distinguish between familiar and unfamiliar inputs. Future work may explore more theoretically grounded methods for combining these layers to further bridge the gap to the theoretical maximum performance.

Reference: Chen, S., Bi, X., Gao, R., & Sun, X. (2022). Holistic Sentence Embeddings for Better Out-of-Distribution Detection. Findings of EMNLP 2022.

Summary updated

BibTeX
@inproceedings{chen2022holistic,
  author = {Sishuo Chen and Xiaohan Bi and Rundong Gao and Xu Sun},
  title = {{Holistic Sentence Embeddings for Better Out-of-Distribution Detection}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2022},
  url = {https://openreview.net/forum?id=nZLhWzyosDz},
}
Figure