Contrastive Out-of-Distribution Detection for Pretrained Transformers (2021)

Abstract: Pretrained Transformers achieve remarkable performance when training and test data are from the same distribution. However, in real-world scenarios, the model often faces out-of-distribution (OOD) instances that can cause severe semantic shift problems at inference time. Therefore, in practice, a reliable model should identify such instances, and then either reject them during inference or pass them over to models that handle another distribution. In this paper, we develop an unsupervised OOD detection method, in which only the in-distribution (ID) data are used in training. We propose to fine-tune the Transformers with a contrastive loss, which improves the compactness of representations, such that OOD instances can be better differentiated from ID ones. These OOD instances can then be accurately detected using the Mahalanobis distance in the model’s penultimate layer. We experiment with comprehensive settings and achieve near-perfect OOD detection performance, outperforming baselines drastically. We further investigate the rationales behind the improvement, finding that more compact representations through margin-based contrastive learning bring the improvement. We release our code to the community for future research.

## introduction many natural language classifiers are developed based on a closed-world assumption, i.e., the training and test data are sampled from the same distribution. however, training data can rarely capture the entire distribution. in real-world scenarios, outof-distribution (ood) instances, which come from categories that are not known to the model, can often be present in inference phases. these instances could be misclassified by the model into known categories with high confidence, causing the semantic shift problem @xcite . as a practical solution to this problem in real-world applications, the model should detect such instances, and signal exceptions or transmit to models handling other categories or tasks. although pretrained transformers @xcite achieve remarkable results when intrinsically evaluated on in-distribution (id) data, recent work @xcite shows that many of these models fall short of detecting ood instances. despite the importance, few attempts have been made for the problem of detecting ood in nlp tasks. one proposed method is to train a model on both the id and ood data and regularize the model to produce lower confidence on ood instances than id ones @xcite . however, as the ood instances reside in an unbounded feature space, their distribution during inference is usually unknown. hence, it is hard to decide which ood instances to use in training, let alone that they may not be available in lots of scenarios. another practiced method for ood detection is to use the maximum class probability as an indicator @xcite , such that lower values indicate more probable ood instances. though easy to implement, its ood detection performance is far from perfection, as prior studies @xcite show that ood inputs can often get high probabilities as well. in this paper, we aim at improving the ood detection ability of natural language classifiers, in particular, the pretrained transformers, which have been the backbones of many sota nlp systems. for practical purposes, we adopt the setting where only id data are available during task-specific training. moreover, we require that the model should maintain classification performance on the id task data. to this end, we propose a contrastive learning framework for unsupervised ood detection, which is composed of a contrastive loss and an ood scoring function. our contrastive loss aims at increasing the discrepancy of the representations of instances from different classes in the task. during training, instances belonging to the same class are regarded as pseudo-id data while those of different classes are considered mutually pseudo-ood data. we hypothesize that increasing inter-class discrepancies can help the model learn discriminative features for id/ood distinctions, and therefore help detect true ood data at inference. we study two versions of the contrastive loss: a similaritybased contrastive loss @xcite @xcite and a margin-based contrastive loss. the ood scoring function maps the representations of instances to ood detection scores, indicating the likelihood of an instance being ood. we examine different combinations of contrastive losses and ood scoring functions, including maximum softmax probability, energy score, mahalanobis distance, and maximum cosine similarity. particularly, we observe that ood scoring based on the mahalanobis distance @xcite , when incorporated with the margin-based contrastive loss, generally leads to the best ood detection performance. the mahalanobis distance is computed from the penultimate layer 2 of transformers by fitting a class-conditional multivariate gaussian distribution. the main contributions of this work are threefold. first, we propose a contrastive learning framework for unsupervised ood detection, where we comprehensively study combinations of different contrastive learning losses and ood scoring functions. second, extensive experiments on various tasks and datasets demonstrate the significant improvement our method has made to ood detection for transformers. third, we provide a detailed analysis to reveal the importance of different incorporated techniques, which also identifies further challenges for this emerging research topic. ## method in this section, we first formally define the ood detection problem (sec. 3.1), then introduce the overall framework (sec. 3.2), and finally present the contrastive representation learning and scoring functions (sec. 3.3 and sec. 3.4). ## experiments this section presents experimental evaluations of the proposed ood detection framework. we start by describing experimental datasets and settings (sec.4.1 and 4.2), followed by detailed results analysis and case studies (sec.4.3 to 4.5). ## conclusion this work presents an unsupervised ood detection framework for pretrained transformers requiring only id data. we systematically investigate the combination of contrastive losses and scoring functions, the two key components in our framework. in particular, we propose a margin-based contrastive objective for learning compact representations, which, in combination with the mahalanobis distance, achieves the best performance: near-perfect ood detection on various tasks and datasets. we further propose novel class detection as the future challenge for ood detection.