Abstract
Standard pretrained Transformers often exhibit overconfidence when encountering out-of-distribution (OOD) data, a phenomenon known as the semantic shift problem. This paper proposes a contrastive learning framework that fine-tunes models using a margin-based contrastive loss and employs Mahalanobis distance for OOD scoring. This approach achieves near-perfect OOD detection on several benchmarks without requiring OOD examples during training.
Introduction
Most NLP classifiers operate under a "closed-world assumption," assuming all inference-time inputs belong to the training classes. However, real-world deployment often involves "alien" inputs—such as snippets of a different language or unrelated domains—that the model was never trained to handle. Standard models like BERT or RoBERTa tend to classify these OOD samples into in-distribution (ID) categories with high confidence. To ensure reliability, models must be capable of unsupervised OOD detection, identifying these samples using only ID training data.
Methodology
The proposed framework consists of two primary components: contrastive representation learning and Mahalanobis-based scoring.
Contrastive Representation Learning
Beyond standard Cross-Entropy loss ($L_{ce}$), the authors introduce a Margin-based Contrastive Loss ($L_{margin}$). The objective is to force instances of the same class to cluster tightly in the feature space while maintaining a minimum distance (the margin) between different classes.
By shrinking the territory occupied by ID classes, the model creates a more defined feature manifold. OOD samples, which do not share these learned semantic features, naturally fall into the "empty space" outside these dense clusters.
OOD Scoring with Mahalanobis Distance
At inference, the model calculates a "suspicion score" using the Mahalanobis distance. Unlike the Maximum Softmax Probability (MSP), which is often overconfident, the Mahalanobis distance measures how many standard deviations an input is from the mean of ID clusters, accounting for the covariance (shape) of those clusters.
Experimental Setup
The method was evaluated using several ID tasks, including sentiment analysis (SST2, IMDB), question classification (TREC-10), and topic classification (20 Newsgroups). OOD benchmarks included NLI datasets (RTE, MNLI), machine translation data (Multi30K, WMT16), and cross-task datasets.
Results and Discussion
The framework achieved near-perfect results, with AUROC scores reaching 99.3% to 100% on SST2, IMDB, and TREC-10. The False Alarm Rate at 95% Recall (FAR95) was reduced from a baseline of ~35% to 1.7%.
As shown in the t-SNE visualizations, $L_{margin}$ creates significantly denser and more isolated ID clusters compared to standard training or similarity-based contrastive loss ($L_{scl}$). This compactness is the key to effective OOD detection, as it leaves more space for OOD data to be identified as outliers. Importantly, this enhanced detection capability does not compromise the model's primary task accuracy.
Conclusion
While the proposed method excels at detecting OOD samples from different domains, "Novel Class Detection" (detecting new categories within the same domain) remains a challenge. Future research will focus on these "near-OOD" scenarios to further improve the robustness of pretrained Transformers in the wild.
Summary updated