Exploring Gradient-Guided Masked Language Model to Detect Textual Adversarial Attacks

Xiaomei Zhang, Zhaoxi Zhang, Yanjun Zhang, Xufei Zheng, Leo Yu Zhang, Shengshan Hu, Shirui PanIEEE Transactions on Audio Speech and Language Processing 2025

Introduction

Deep Neural Networks (DNNs) in Natural Language Processing (NLP) are remarkably effective yet notoriously fragile. Minor, often imperceptible perturbations—such as replacing a word with a synonym or introducing a typo—can drastically alter a model's prediction, resulting in an adversarial attack. While traditional defenses often involve adversarial training, these methods are computationally expensive and can degrade performance on clean data.

A more practical alternative is adversarial detection, which aims to identify and flag malicious inputs before they reach the victim model. This paper introduces GradMLMD, a novel detection framework that exploits the "off-manifold" nature of adversarial examples. By leveraging the reconstruction capabilities of pre-trained Masked Language Models (MLMs), GradMLMD identifies inputs that deviate from the distribution of natural language.

Methodology

The core of the proposed approach lies in using MLMs to project inputs back onto the natural language manifold. The authors propose two variants:

MLMD (Masked Language Model-based Detection)

MLMD systematically masks tokens in an input sequence and uses an MLM (e.g., RoBERTa) to reconstruct them. If the original input was adversarial, this "mask and unmask" process typically results in a significant shift in the victim model's output, as the MLM pulls the "off-manifold" adversarial token back toward a legitimate state.

GradMLMD (Gradient-guided MLMD)

To address the computational latency of masking every word, GradMLMD utilizes the gradient of the loss function to identify "keywords"—the specific tokens most likely targeted by an attacker. By focusing the mask/unmask operations only on these high-gradient tokens (typically the top 30%), GradMLMD achieves a 70% reduction in operations while maintaining high detection accuracy.

Overview of the GradMLMD detection process and masking strategies.

Experimental Setup

The framework was evaluated on three standard benchmarks: AG-NEWS (news classification), IMDB (sentiment analysis), and SST-2 (sentiment analysis). The researchers tested against four major attack types: PWWS, TextFooler, TextBugger, and DeepWordBug, targeting a variety of victim architectures including CNN, LSTM, BERT, and ALBERT.

Results and Analysis

The experimental results demonstrate that MLMD and GradMLMD consistently outperform existing state-of-the-art detectors, such as FGWS and GRADMASK, with F1 score improvements ranging from 1% to 20%.

Impact of Masking Rate

A key finding is the relationship between the masking rate ($r$) and detection performance. As shown in the figure below, detection accuracy (F1 score) generally increases with the masking rate but begins to plateau, suggesting that focusing on a subset of influential words is sufficient for robust detection.

Detection performance (F1 Score) across different masking rates for various attack types.

Key Insights

  • The Manifold as a Shield: Pre-trained MLMs act as natural denoisers. Their training on massive corpora allows them to effectively "repair" adversarial perturbations.
  • Gradients as a Shortcut: The high overlap (86%) between gradient-selected keywords and an ideal "oracle" selection confirms that gradients are a highly effective proxy for identifying critical tokens.
  • Model-Agnosticism: The detector operates as an independent module, allowing it to protect any existing NLP model without retraining.

Conclusion

GradMLMD provides a powerful and efficient solution for securing NLP models against adversarial attacks. By combining the manifold-capturing power of MLMs with gradient-guided efficiency, it offers a scalable defense that does not compromise the performance of the underlying victim model. Future work may explore scaling these techniques to larger generative models and investigating adaptive attacks designed to bypass manifold-based defenses.

Summary updated

BibTeX
@misc{zhang2025exploring,
  author = {Xiaomei Zhang and Zhaoxi Zhang and Yanjun Zhang and Xufei Zheng and Leo Yu Zhang and Shengshan Hu and Shirui Pan},
  title = {{Exploring Gradient-Guided Masked Language Model to Detect Textual Adversarial Attacks}},
  year = {2025},
  eprint = {2504.08798},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2504.08798},
}
Figure