Is Fine-tuning Needed? Pre-trained Language Models Are Near Perfect for Out-of-Domain Detection (2023)

Abstract: Out-of-distribution (OOD) detection is a critical task for reliable predictions over text. Fine-tuning with pre-trained language models has been a de facto procedure to derive OOD detectors with respect to in-distribution (ID) data. Despite its common use, the understanding of the role of fine-tuning and its necessity for OOD detection is largely unexplored. In this paper, we raise the question: is fine-tuning necessary for OOD detection? We present a study investigating the efficacy of directly leveraging pre-trained language models for OOD detection, without any model fine-tuning on the ID data. We compare the approach with several competitive fine-tuning objectives, and offer new insights under various types of distributional shifts. Extensive experiments demonstrate near-perfect OOD detection performance (with 0% FPR95 in many cases), strongly outperforming the fine-tuned counterpart.

## introduction despite recent successes, high-performing pretrained language models are still fragile under distribution shifts, making their applications to the real world challenging @xcite . in most real-world settings, the train and test distributions are often not independent and identically distributed. furthermore, test distributions are often non-stationary and can change over time. the problem of out-of-distribution (ood) detection addresses the identification of anomalous data, enabling the model to abstain from prediction when it 1 https://github.com/uppaal/lm-ood is not supposed to. this is especially important for high-risk settings like financial and medical applications, where unreliable predictions could incur great costs @xcite . in literature, a de facto procedure is to fine-tune a pre-trained language model on the in-distribution (id) data foot_0 , and then derive the ood detector based on the adapted model @xcite @xcite . the fine-tuned model is hypothesized to produce embeddings that are customized to the id data. thus, prior work focuses on the design of fine-tuning and expects the adapted representations to be more useful for ood detection. despite its common use, the understanding of the role of fine-tuning and its necessity for ood detection is largely lacking in the field. motivated by this, we revisit the common procedure and raise the unexplored question: is finetuning necessary at all, for ood detection? to answer this question, we introduce a simple and effective procedure for ood detection, which does not require any model fine-tuning on the id data. specifically, we explore distance-based metrics for detection, which measure the relative distances of samples in the representation space of a pre-trained language model. the operating hypothesis is that embeddings of id samples are closer to each other than the ood sample embeddings. to the best of our knowledge, we are the first to explore distancebased ood detection methods directly on a pretrained language model, rather than the fine-tuned models adopted in previous works. we show that our method based on a pre-trained language model achieves near-perfect performance in detecting out-of-domain shifts, favorably outperforming its fine-tuned counterparts. for example, for 20newsgroups (id) vs. rte (ood), ood detection with the best fine-tuning loss @xcite ) yields an fpr95 of 24.8%, while a pre-trained language model can perfectly detect rte as ood with 0% fpr95. for comprehensive evaluations, we experiment on 8 diverse id-ood dataset pairs spanning semantic and background shifts, and show that the strong performance of using the pretrained model holds consistently. to better understand the strong performance, we further show that pre-trained models display strongly separated domain clusters, both qualitatively and quantitatively. the strong separation of domain clusters leads to the efficacy of distance-based ood detection. even further, we systematically compare different fine-tuning objectives, and interestingly observe that the performance of distance-based ood detection declines over the course of fine-tuning across all objectives, despite the increase in id classification accuracy. to this end, we provide new insights that early stopping @xcite can be a promising solution, if one desires a good tradeoff between ood detection and id classification performance. our contributions can be summarized as follows: ## methodology in section 3.1, we start by introducing ood detection with pre-trained language models, which does not require any model fine-tuning on the id dataset. we further consider ood detection with model fine-tuning in section 3.2. ## related work the problem of ood detection is different from domain adaptation @xcite , where a model is trained to generalize to a known target domain with the same label space. it is also different from selective prediction where a model abstains only when its confidence is low, irrespective of domain @xcite @xcite . ## conclusion in this paper, we explore the simple and effective setting of zero-shot ood detection with pre-trained langage models. our work departs from prior literature that typically requires fine-tuning on the id data. extensive evaluations demonstrate that pre-trained models are near-perfect for ood detection when the test data comes from a different domain. we additionally investigate the effect of fine-tuning on ood detection, and identify strategies to achieve both strong ood detection performance and id accuracy. we perform both qualitative and quantitative analysis on the embedding characteristics, explaining the strong performance of our method. we hope our work will inspire future work to the strong promise of using pre-trained models for ood detection.