Contrastive Out-of-Distribution Detection for Pretrained Transformers

Wenxuan Zhou, Fangyu Liu, Muhao Chen
4/18/2021
cs.CL
transformer-attentioncontrastive-ssl

Official code: wzhouad/Contra-OOD (41 stars, Python, MIT, last commit 2021-11-29).

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.

Show full abstract

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.

View on arXivView PDF

Code Implementations(1)

wzhouad/Contra-OODOfficial100%
MIT

Source code for paper "Contrastive Out-of-Distribution Detection for Pretrained Transformers", EMNLP 2021

419PythonSep 6, 20215 years agoMIT

Similar Papers

Cite this paper

@article{zhou2021contrastive,
  title  = {Contrastive Out-of-Distribution Detection for Pretrained Transformers},
  author = {Wenxuan Zhou and Fangyu Liu and Muhao Chen},
  year   = {2021},
  eprint = {2104.08812},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2104.08812}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2104.08812.svg)](https://codeofpaper.com/paper/2104.08812)

Discussion