CED: Comparing Embedding Differences for Detecting Out-of-Distribution and Hallucinated Text

Hakyung Lee, Keon-Hee Park, Hoyoon Byun, Jeyoon Yeom, Jihee Kim, Gyeong-Moon Park, Kyungwoo SongEMNLP 2024

Abstract

Detecting out-of-distribution (OOD) samples is crucial for ensuring the safety and robustness of models deployed in real-world scenarios. While most studies on OOD detection focus on fine-tuned models trained on in-distribution (ID) data, detecting OOD in pre-trained models is also important due to computational limitations and the widespread use of open-source pre-trained models. However, in the same domain shift setting, the OOD detection performance of pre-trained models is insufficient because both ID and OOD samples originate from the same domain, leading to a high overlap in their embeddings. To address this issue, we introduce a new method called CED, a training-free OOD detection technique designed to enhance the distinction between ID and OOD datasets. We theoretically validate that specific auxiliary and oracle samples that satisfy certain conditions improve this distinction. Motivated by our theoretical analysis, CED enhances the differentiation by utilizing these specially designed auxiliary and oracle samples. As a result, CED significantly improves the ability of pre-trained models to distinguish between ID and OOD samples in text classification and hallucination detection tasks. Furthermore, we verify that CED is a plug-and-play method compatible with various backbone networks, such as RoBERTa, Llama, and OpenAI Embedding.

Introduction

As large language models (LLMs) are increasingly deployed in critical sectors like healthcare and customer service, the ability to identify out-of-distribution (OOD) inputs—instances where the model should ideally refrain from responding—is vital for safety and reliability. Traditional OOD detection often relies on fine-tuning models on specific tasks, which is computationally expensive and difficult to scale as new data distributions emerge.

While "frozen" pre-trained language models (PLMs) offer a more efficient alternative, they frequently struggle with semantic shifts. In these scenarios, OOD data can appear very similar to in-distribution (ID) data within the model's embedding space, leading to high overlap and poor detection performance. CED (Comparing Embedding Differences) addresses this by amplifying relational differences in the latent space without requiring any additional training.

Methodology: Comparing Embedding Differences (CED)

CED is a "plug-and-play" method that leverages the contextual sensitivity of PLMs to distinguish between ID and OOD samples. The process consists of two primary steps:

  1. Strategic Sample Selection: The method identifies "Oracle Samples" from the ID training data that are closest to the target input. It then selects "Auxiliary Samples" oriented in the opposite direction of the oracle samples in the embedding space to act as contrastive anchors.
  2. Sample Combination and Scoring: The target input is concatenated with these selected samples and re-processed by the model. The CED Score is calculated by comparing how the embeddings change when the target is paired with an auxiliary sample versus when the oracle is paired with that same auxiliary sample.
Overview of the CED methodology, illustrating the two-step process of sample selection and scoring through concatenation.

The core intuition is that for ID data, the model's internal context keeps the embeddings stable and similar to the oracle. For OOD data, the concatenation forces a divergence in the embedding space that a simple distance check would miss.

Experimental Setup

The researchers evaluated CED across two primary domains:

  • Text Classification (OOD Detection): Evaluated using six benchmark datasets: Banking77, CLINC150, ROSTD, SNIPS, AGNews, and News Categories (NC).
  • Hallucination Detection: Utilized the HELM benchmark, which includes text generated by models like GPT-J and Llama-2, manually labeled for factual consistency against WikiText-103 articles.

Results and Insights

CED consistently outperformed standard baselines such as Mahalanobis distance and K-Nearest Neighbors (KNN) across various backbone architectures, including RoBERTa, Llama 3, and OpenAI’s proprietary embedding models.

  • Reliability Gains: On the SNIPS dataset using RoBERTa, CED reduced the False Positive Rate (FPR95) from 0.6203 to 0.3743—a significant improvement in reliability.
  • Versatility: The method demonstrated gains even on black-box models like OpenAI’s text-embedding-3-small, confirming it does not require access to internal model weights.
  • Hallucination as OOD: By treating hallucinations as "out-of-distribution facts," CED achieved superior AUC scores in hallucination detection compared to traditional logit-based methods.
Performance comparison showing the improvement in AUROC and reduction in FPR95 when using CED compared to a standard KNN baseline across different parameter settings.

Theoretical analysis confirms that selecting auxiliary samples in the opposite direction of the oracle is the optimal strategy for identifying "hard" OOD cases—those samples that sit right on the edge of the ID distribution.

Limitations and Future Work

While CED offers a robust, training-free solution, some limitations remain. The study was primarily validated on English-language datasets; further analysis is needed to confirm these relational patterns hold across diverse languages. Additionally, while the method is training-free, the requirement for multiple passes through the model to process concatenated sequences introduces computational overhead. Future work will focus on optimizing this for real-time, low-latency applications and testing on the latest proprietary models as embedding access becomes available.

Summary updated

BibTeX
@inproceedings{lee2024ced,
  author = {Hakyung Lee and Keon-Hee Park and Hoyoon Byun and Jeyoon Yeom and Jihee Kim and Gyeong-Moon Park and Kyungwoo Song},
  title = {{CED: Comparing Embedding Differences for Detecting Out-of-Distribution and Hallucinated Text}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  url = {https://openreview.net/forum?id=8KK8oVZ085},
}
Figure