Adversarial Text Purification: A Large Language Model Approach for Defense

Raha Moraffah, Shubh Khandelwal, Amrita Bhattacharjee, Huan Liu

TL;DR

Researchers have developed a novel defense called Adversarial Text Purification that uses Large Language Models (LLMs) like GPT-3.5 to "clean" text inputs before they reach a classifier. By leveraging the generative power of LLMs to rewrite adversarially perturbed sentences, this method restores classifier accuracy by over 65% on average without requiring any retraining of the original models.

What Problem Does This Paper Address?

Deep learning models for text classification are surprisingly fragile. An attacker can craft "adversarial examples"—sentences with tiny, human-unnoticeable changes (like swapping a word for a synonym)—that completely flip a model's prediction. While this has been extensively studied in images, defending text is much harder.

In the image domain, noise is continuous, meaning you can use math (gradients) to "smooth" it out. Text, however, is discrete. Swapping "good" for "great" is a jump, not a slide. Identifying and reversing these combinatorial perturbations is a massive challenge. Existing defenses often require expensive adversarial training (retraining the model on attacked data) or are tied to specific types of attacks. The authors tackle the need for a "plug-and-play" defense that works against unseen attacks without touching the underlying classifier.

Methods

The authors propose an LLM-guided Adversarial Text Purification framework. Instead of trying to mathematically characterize the "noise" in a sentence, they treat the problem as a translation task: translating "attacked text" into "clean text."

![Fig.

  1. Our Proposed LLM-guided Adversarial Text Purification Framework.](/figures/art_cc7502fda84d407d8a5adb688541a348)

The core of the method is a carefully designed prompt that "primes" an instruction-tuned LLM (GPT-3.5) to act as a knowledgeable teacher. The prompt provides:

  1. The altered sentence (the adversarial example).
  2. The misclassified label (what the model currently thinks).
  3. The correct label (the ground truth).
  4. A list of possible categories.

The LLM is then tasked with generating a new sentence that is semantically similar to the original but designed to be classified correctly. This bypasses the need to explicitly find which words were changed by the attacker; the LLM simply uses its vast contextual knowledge to produce a fluent, "purified" version of the input.

Datasets

The researchers evaluated their framework on two standard NLP benchmarks:

  • IMDb: A dataset for binary sentiment classification (positive vs. negative) of movie reviews.
  • AG News: A four-way topic classification dataset (Science, Business, World, Sports).

To test the defense, they used TextFooler, one of the strongest word-level substitution attacks available, which systematically replaces words with synonyms to induce misclassification.

Key Insights and Ideas

  • LLMs as Semantic Denoisers: The most striking insight is that LLMs possess a deep enough understanding of language to recognize when a sentence has been "tampered" with semantically. They can effectively "hallucinate" the original intent back into the text.
  • The "Teacher" Prompt is Crucial: The authors performed an ablation study comparing their full prompt (P0) against simpler versions (P1, which removes the correction instruction, and P2, which just asks for a paraphrase).
  • Insight: Simply paraphrasing (P2) is not enough; the model needs the context of the misclassification to effectively "target" the purification.
  • Zero-Shot Robustness: Unlike adversarial training, this method requires no updates to the classifier's weights. It acts as a protective "firewall" that can be dropped in front of any pre-existing BERT or RoBERTa model.

Key Results

The results demonstrate that LLM-based purification significantly outperforms traditional defense methods.

Comparison of our LLM-guided purification methods with baselines.
  • Massive Accuracy Recovery: On the IMDb dataset under a heavy attack ($K=50$), a standard BERT model's accuracy plummeted to just 2.8%. The LLM-guided purification restored it to 73.52%.
  • Outperforming Baselines: The proposed method outperformed state-of-the-art adversarial training methods (like FreeLB) by over 30% and previous purification attempts (using BERT-based masking) by over 25% in most scenarios.
  • Fluency: Qualitative analysis showed that the purified sentences were not only correctly classified but also remained fluent and grammatically correct, unlike the often-clunky adversarial versions.

Limitations and Future Work

While highly effective, the authors and the nature of the work suggest a few hurdles:

  • Latency and Cost: Querying a massive LLM like GPT-3.5 for every single classification task is computationally expensive and introduces significant delay compared to a standalone classifier.
  • Prompt Sensitivity: The defense relies heavily on the quality of the prompt and the generative capabilities of the underlying LLM. A weaker LLM might not purify as effectively.
  • Future Directions: The authors suggest exploring how this approach scales with different LLMs and whether the purification process can be optimized to reduce the "inference overhead," perhaps by distilling the LLM's purification ability into a smaller, faster model.

Summary updated

BibTeX
@inproceedings{moraffah2024adversarial,
  author = {Raha Moraffah and Shubh Khandelwal and Amrita Bhattacharjee and Huan Liu},
  title = {{Adversarial Text Purification: A Large Language Model Approach for Defense}},
  booktitle = {Pakdd},
  year = {2024},
  doi = {10.1007/978-981-97-2262-4\_6},
}
Figure