Detecting Textual Adversarial Examples through Randomized Substitution and Vote

Xiaosen Wang, Yifeng Xiong, Kun He

Abstract

A line of work has shown that natural text processing models are vulnerable to adversarial examples. Correspondingly, various defense methods are proposed to mitigate the threat of textual adversarial examples, e.g., adversarial training, input transformations, detection, etc. In this work, we treat the optimization process for synonym substitution based textual adversarial attacks as a specific sequence of word replacement, in which each word mutually influences other words. We identify that we could destroy such mutual interaction and eliminate the adversarial perturbation by randomly substituting a word with its synonyms. Based on this observation, we propose a novel textual adversarial example detection method, termed Randomized Substitution and Vote (RS&V), which votes the prediction label by accumulating the logits of $k$ samples generated by randomly substituting the words in the input text with synonyms. The proposed RS&V is generally applicable to any existing neural networks without modification on the architecture or extra training, and it is orthogonal to prior work on making the classification network itself more robust. Empirical evaluations on three benchmark datasets demonstrate that our RS&V could detect the textual adversarial examples more successfully than the existing detection methods while maintaining the high classification accuracy on benign samples.

Introduction

Deep Neural Networks (DNNs) in Natural Language Processing (NLP) are notoriously fragile. An attacker can flip a model's prediction—for instance, changing a sentiment classifier's output from "Positive" to "Negative"—by replacing just a few words with synonyms. These synonym substitution-based adversarial attacks are particularly challenging because they are often human-imperceptible, maintaining the original meaning while baffling the model.

Existing defenses typically fall into two categories: adversarial training, which is computationally expensive and often degrades performance on clean data, and input pre-processing, which frequently struggles to distinguish between malicious attacks and natural linguistic diversity. This paper introduces Randomized Substitution and Vote (RS&V), a model-agnostic detection method that requires no extra training and maintains high accuracy on benign samples.

Methodology

The core philosophy of RS&V is based on the "Mutual Interaction" hypothesis. Adversarial attacks rely on a fragile, coordinated sequence of word replacements that work together to push the model toward an incorrect prediction. By introducing randomness through synonym substitution, RS&V disrupts this specific coordination, causing the attack to collapse.

Overview of the RS&V detection framework, illustrating the pipeline from input text to final detection and label voting.

As shown in the framework overview, the RS&V algorithm follows three main steps:

  1. Stopword Filtering: High-frequency words (e.g., "the", "is") are identified using a TF-IDF-inspired approach and excluded from substitution to preserve the core semantic context.
  2. Randomized Substitution: For a given input text $x$, the system generates $k$ variants. In each variant, a portion of words is randomly replaced with synonyms sourced from WordNet and GloVe embeddings.
  3. Vote & Detection: All $k$ variants are fed into the target model. The system accumulates the logits (raw prediction scores) to produce a "voted" label. If this voted label differs from the model's original prediction for the input, the input is flagged as adversarial.

Experimental Evaluation

The authors evaluated RS&V across three benchmarks: AG’s News, IMDB, and Yahoo! Answers, testing against five state-of-the-art attacks (GA, PWWS, PSO, Textfooler, and HLA) on Word-CNN, BERT, and RoBERTa architectures.

Key Results

  • Superior Detection: RS&V consistently outperformed baselines like DISP and FGWS. On the AG's News dataset using a CNN, RS&V achieved a detection accuracy of 86.0%, significantly higher than DISP (76.0%) and FGWS (77.1%).
  • Minimal Clean Accuracy Impact: RS&V maintained benign accuracy within ~1% of undefended models. In some instances, such as RoBERTa on Yahoo! Answers, it even slightly improved clean accuracy.
  • Robustness: In "white-box" scenarios where the attacker is aware of the defense, RS&V remained effective, maintaining 85.0% accuracy against Textfooler on AG's News, where an undefended model dropped to 34.8%.

Ablation Studies

The effectiveness of RS&V is influenced by three primary hyperparameters: the substitution rate $p$, the number of votes $k$, and the stopword selection portion $s$. As illustrated in the ablation studies, increasing the substitution rate $p$ and the number of votes $k$ generally improves adversarial detection accuracy, while the stopword portion $s$ must be carefully balanced to avoid losing semantic information.

Ablation studies showing the impact of substitution rate (p), number of votes (k), and stopword selection portion (s) on classification accuracy for benign and adversarial samples.

Conclusion

RS&V offers a robust, "plug-and-play" defense against textual adversarial attacks by leveraging the fragility of adversarial perturbations. While the method introduces inference overhead—requiring $k$ passes through the model—it provides a powerful detection mechanism without the need for model retraining. Future work may focus on optimizing computational efficiency and extending the defense to character-level or paraphrasing attacks.

Summary updated

BibTeX
@inproceedings{wang2022detecting,
  author = {Xiaosen Wang and Yifeng Xiong and Kun He},
  title = {{Detecting Textual Adversarial Examples through Randomized Substitution and Vote}},
  booktitle = {Conference on Uncertainty in Artificial Intelligence},
  year = {2022},
  url = {https://openreview.net/forum?id=Hu_4s88iqgc},
}
Figure