Papers › Contrastive Out-of-Distribution Detection for Pretrained Transformers

Contrastive Out-of-Distribution Detection for Pretrained Transformers

18 Apr 2021EMNLP 2021 11arXiv:2104.08812archive 2025-07-28

Wenxuan Zhou, Fangyu Liu, Muhao Chen

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.

PaperPDFConference PDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2104.08812")

Code

Syntology Ran 6 of 7 code samples harvested from 1 repository linked to this paper; 1 has no recorded run. Of those that ran: 1 ran · honoured contract; 2 ran · our draft was wrong; 3 ran · fixture could not drive it.

By repository: official repository: 5 samples from 1 repository, 4 ran; 2 identical to code first harvested elsewhere. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

wzhouad/Contra-OOD officialmentioned in papermentioned on GitHubpytorch report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

7 samples harvested; 6 ran; 1 honoured the contract we drafted; 1 has no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

1ran · honoured contract
2ran · our draft was wrong
3ran · fixture could not drive it
1unverified

Licence: 2 of the 7 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from wzhouad/Contra-OOD. Some samples are identical code Syntology first harvested from another repository; for those, this paper's copy is not located and its licence is not recorded. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

collate_fn wzhouad/Contra-OOD/evaluation.py official repository ran · our draft was wrong MIT (permissive) · fba9a666b9277255 · report
fpr_and_fdr_at_recall wzhouad/Contra-OOD/evaluation.py official repository ran · fixture could not drive it fingerprinted MIT (permissive) · 541b471fca0ca15a · report
get_fpr_95 wzhouad/Contra-OOD/evaluation.py official repository ran · fixture could not drive it fingerprinted MIT (permissive) · b20471eaf133a65e · report
stable_cumsum wzhouad/Contra-OOD/evaluation.py official repository ran · honoured contract fingerprinted MIT (permissive) · e6809651fe876f1d · report
evaluate_ood wzhouad/Contra-OOD/evaluation.py official repository unverified MIT (permissive) · b1476bb643081c90 · report
get_auroc identical code first harvested elsewhere ran · fixture could not drive it fingerprinted licence of this copy not recorded · ace38fb975eb5cfa · report
merge_keys identical code first harvested elsewhere ran · our draft was wrong licence of this copy not recorded · 21a8e085a4502dad · report

Tasks

Contrastive LearningOut of Distribution (OOD) DetectionOut-of-Distribution Detection

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

Contrastive Learning

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections