Masked Language Model Based Textual Adversarial Example Detection

Xiaomei Zhang, Zhaoxi Zhang, Qi Zhong, Xufei Zheng, Yanjun Zhang, Shengshan Hu, Leo Yu Zhang · DOI

Introduction

Natural Language Processing (NLP) models, despite their impressive performance, remain highly vulnerable to adversarial attacks. These attacks involve small, often imperceptible perturbations—such as word substitutions or character-level typos—that can drastically alter a model's output. Defending against such threats typically involves computationally expensive retraining or architectural modifications. This paper proposes Masked Language Model-based Detection (MLMD), a model-agnostic, "plug-and-play" framework designed to identify adversarial examples by leveraging the inherent properties of pre-trained Masked Language Models (MLMs).

Methodology: MLMD as a Manifold Projector

The core philosophy of MLMD rests on the off-manifold conjecture: while natural language resides on a low-dimensional "normal" data manifold, adversarial examples are intentionally pushed off this manifold to exploit model weaknesses. MLMD utilizes an MLM (such as RoBERTa) to act as a manifold projector, pulling suspicious inputs back toward the distribution of natural language.

Overview of the MLMD detection framework for normal and adversarial inputs.

As illustrated in the framework overview above, the detection process follows three primary stages:

  1. Masking: A portion of the input tokens is replaced with a special [MASK] token.
  2. Unmasking: The pre-trained MLM reconstructs the masked tokens. Because the MLM is trained on vast corpora of natural text, its reconstructions naturally align with the "normal" data manifold, effectively "purifying" adversarial perturbations.
  3. Detection: Both the original and reconstructed texts are fed into the victim model. If the input was normal, the prediction remains stable. If it was adversarial, the reconstruction process strips the perturbation, leading to a significant shift in the victim model's prediction.

Experimental Setup

The authors evaluated MLMD across three standard benchmarks: AG-NEWS (topic classification), IMDB (long-form sentiment analysis), and SST-2 (short-form sentiment analysis). The framework was tested against four diverse victim architectures (CNN, LSTM, BERT, and ALBERT) and four state-of-the-art attack methods: word-level (PWWS, TextFooler) and character-level (TextBugger, DeepWordBug).

Results and Discussion

MLMD demonstrates exceptional performance, achieving detection accuracies as high as 98.4% on AG-NEWS and 90.1% on the challenging SST-2 dataset. It consistently outperforms existing frequency-based baselines like FGWS, particularly on short texts where word frequency is an unreliable metric.

Distribution of detection scores across different attack methods, showing clear separation between normal and adversarial examples.

The empirical strength of this approach is visible in the distribution of detection scores. As shown in the histograms above, normal examples consistently cluster near a score of 1.0, indicating high prediction stability after reconstruction. In contrast, adversarial examples exhibit a much broader and lower distribution of scores, confirming that the MLM successfully disrupts the adversarial signal.

Key insights from the study include:

  • Model Agnosticism: MLMD works effectively regardless of the victim model's architecture.
  • RoBERTa's Superiority: RoBERTa proved to be the most effective unmasking engine, likely due to its dynamic masking and large-scale pre-training.
  • Simplicity: A simple threshold-based classifier on label changes is often sufficient for high-accuracy detection.

Conclusion and Future Work

MLMD provides a robust, architecture-independent defense against textual adversarial attacks. By repurposing MLMs as manifold projectors, it offers a scalable alternative to adversarial training. Future research will focus on reducing inference latency—currently the method's primary bottleneck—and investigating adaptive attacks where adversaries attempt to bypass the masking-reconstruction pipeline.

Summary updated

BibTeX
@inproceedings{zhang2023masked,
  author = {Xiaomei Zhang and Zhaoxi Zhang and Qi Zhong and Xufei Zheng and Yanjun Zhang and Shengshan Hu and Leo Yu Zhang},
  title = {{Masked Language Model Based Textual Adversarial Example Detection}},
  booktitle = {AsiaCCS},
  year = {2023},
  doi = {10.1145/3579856.3590339},
}
Figure