Holistic Sentence Embeddings for Better Out-of-Distribution Detection (2022)

Abstract: Detecting out-of-distribution (OOD) instances is significant for the safe deployment of NLP models. Among recent textual OOD detection works based on pretrained language models (PLMs), distance-based methods have shown superior performance. However, they estimate sample distance scores in the last-layer CLS embedding space and thus do not make full use of linguistic information underlying in PLMs. To address the issue, we propose to boost OOD detection by deriving more holistic sentence embeddings. On the basis of the observations that token averaging and layer combination contribute to improving OOD detection, we propose a simple embedding approach named Avg-Avg, which averages all token representations from each intermediate layer as the sentence embedding and significantly surpasses the state-of-the-art on a comprehensive suite of benchmarks by a 9.33% FAR95 margin. Furthermore, our analysis demonstrates that it indeed helps preserve general linguistic knowledge in fine-tuned PLMs and substantially benefits detecting background shifts. The simple yet effective embedding method can be applied to fine-tuned PLMs with negligible extra costs, providing a free gain in OOD detection. Our code is available at https://github.com/lancopku/Avg-Avg.

## introduction pretrained language models have achieved remarkable performance on various nlp tasks under the assumption that the train and test samples are drawn from the same distribution @xcite . however, in real-life applications such as dialogue systems and clinical text processing, it is inevitable for models to make predictions on out-ofdistribution (ood) samples, which may result in fatally unreasonable predictions @xcite . therefore, it is crucial for fine-tuned plms to automatically detect ood inputs. among recent works on textual ood detection, distance-based methods have received much attention due to their superior performance @xcite . they calculate the sample distance to the training-data distribution as the uncertainty measure for ood detection. in these approaches, the distance scores are usually calculated in the space of the last-layer cls vectors (i.e., the inputs to the classification head) produced by fine-tuned plms. as known, the cls embedding space is optimized for the indistribution classification task during fine-tuning, thus not necessarily optimal for ood detection. in this paper, we investigate how to derive sentence embeddings suitable for ood detection from fine-tuned plms. motivated by the token averaging and layer combination techniques proposed to enhance unsupervised sentence embeddings @xcite , we apply them to ood detection and make two intriguing empirical findings: (1) averaging all token representations outperforms the standard practice only using the cls vector; (2) combining token representations from all intermediate layers brings further improvements. these observations lead to an extremely simple yet effective pooling technique: averaging all token representations in each intermediate layer as the sentence embedding for ood detection. we name the all-layer-all-token pooling technique avg-avg and demonstrate that it consistently uplifts the ood detection performance of bert @xcite and roberta @xcite models on a comprehensive suite of textual ood detection benchmarks. further investigations into the rationales behind the improvement show that avg-avg effectively helps reserve general linguistic information in the feature space and benefits detecting background shifts. in summary, our proposal serves as a plug-and-play post-processing technique to improve the capability of fine-tuned plms to detect ood instances and reveals that it is a promising direction to boost textual ood detection via deriving more holistic representations. ## conclusion in this work, we focus on how to derive sentence embeddings suitable for ood detection from finetuned plms. specifically, we introduce token averaging and layer combination to derive more holistic representations and substantially improve the capability of plms to detect ood inputs. moreover, our analysis shows that our approach helps preserve general linguistic information and benefits detecting background shifts. overall, our work points out a new perspective that textual ood detection can be enhanced by obtaining high-quality sentence embeddings, and we hope to extend this idea to training-time methods in future work.