Embedding and Gradient Say Wrong: A White-Box Method for Hallucination Detection

Xiaomeng Hu, Yiming Zhang, Ru Peng, Haozhe Zhang, Chenwei Wu, Gang Chen, Junbo ZhaoEMNLP 2024

Abstract

In recent years, large language models (LLMs) have achieved remarkable success in natural language generation, capable of producing fluent output based on provided prompts. However, the "hallucination" problem—where LLMs generate text unrelated to input facts—remains a critical challenge. This study introduces EGH (Embedding and Gradient-based Hallucination detection), a white-box method that models the distributional distance between regular conditional output and unconditional output (generated without input text). By applying a Taylor Expansion to this distance in the output probability space, our approach leverages internal embedding and first-order gradient information. EGH is a plug-and-play method adaptable to any autoregressive LLM, achieving state-of-the-art performance on benchmarks like HADES and HaluEval.

Introduction

Despite their fluency, LLMs frequently hallucinate, generating factually incorrect or irrelevant text. Current detection methods are largely "black-box," relying on final text outputs or external models for verification. These approaches often miss subtle internal signals indicating a model's failure to ground its answer in the source text.

The authors argue that "white-box" methods—utilizing internal probability matrices and hidden layers—are under-studied. The core challenge lies in identifying which internal parameters are most discriminative for spotting hallucinations amidst the billions of parameters in modern LLMs.

Methodology: The EGH Framework

The EGH method is built on a simple intuition: a hallucination occurs when a model's answer ($A$) does not sufficiently depend on the question ($Q$). To quantify this, the researchers compare two scenarios: a Conditional Query (where the model receives both $Q$ and $A$) and an Unconditional Query (where the question space is zero-padded).

Overview of conditional vs. unconditional query processing to identify hallucination signals.

To make this comparison computationally tractable, the team applies a first-order Taylor Expansion to the difference between these two states. This process extracts two primary features:

  1. Embedding Information ($E$): The difference between hidden layer representations of answer tokens in conditional vs. unconditional settings.
  2. Gradient Information ($G$): The first-order gradient of the KL-divergence between the two probability distributions, capturing how sensitive the model's certainty is to the presence of context.
The three-step EGH pipeline: extracting embeddings and gradients to feed a lightweight MLP hallucination detector.

These features are processed through a three-layer Multi-Layer Perceptron (MLP) to produce a final classification label.

Key Results and Insights

EGH was evaluated across major benchmarks, including HaluEval (35,000 examples), SelfCheckGPT, and HADES.

  • State-of-the-Art Performance: On the HaluEval QA task, EGH achieved a remarkable 97.19% accuracy, outperforming existing black-box baselines by over 30% and previous white-box methods by 2-3%.
  • Generalization: A model trained on only 10% of HaluEval performed exceptionally well on SelfCheckGPT, suggesting that the "internal signature" of a hallucination is consistent across different tasks.
  • Ablation Success: Combining both embeddings and gradients yielded superior results compared to using either feature alone, proving that both internal "state" and "sensitivity" are necessary for accurate detection.

Limitations and Future Work

The primary drawback of EGH is its computational overhead. Because the method requires calculating gradients, it necessitates a backward pass through the model, increasing time and memory complexity compared to simple generation. Future research will focus on identifying more lightweight internal signals that can provide similar accuracy without the need for full gradient backpropagation. Additionally, while EGH excels at detecting input-conflicting hallucinations, further refinement is needed for "fact-conflicting" scenarios where a model is confidently but incorrectly asserting world facts.

Summary updated

BibTeX
@inproceedings{hu2024embedding,
  author = {Xiaomeng Hu and Yiming Zhang and Ru Peng and Haozhe Zhang and Chenwei Wu and Gang Chen and Junbo Zhao},
  title = {{Embedding and Gradient Say Wrong: A White-Box Method for Hallucination Detection}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  url = {https://openreview.net/forum?id=LvDzBWA1vw},
}
Figure