Generating Fluent Adversarial Examples for Natural Languages

Huangzhao Zhang, Hao Zhou, Ning Miao, Lei Li

Abstract

Efficiently building an adversarial attacker for natural language processing (NLP) tasks is a real challenge. Firstly, as the sentence space is discrete, it is difficult to make small perturbations along the direction of gradients. Secondly, the fluency of the generated examples cannot be guaranteed. In this paper, we propose MHA, which addresses both problems by performing Metropolis-Hastings sampling, whose proposal is designed with the guidance of gradients. Experiments on IMDB and SNLI show that our proposed MHA outperforms the baseline model on attacking capability. Adversarial training with MHA also leads to better robustness and performance.

Introduction

Adversarial attacks—perturbing input data to trick a machine learning model—are well-established in computer vision but notoriously difficult in Natural Language Processing (NLP). The primary challenge is that text is discrete; unlike pixels, words cannot be "nudged" by tiny fractions of a gradient. Existing NLP attackers often produce ungrammatical or nonsensical sentences, which are easily ignored during adversarial training, failing to force the model toward a truly robust decision boundary.

The Metropolis-Hastings Attack (MHA) framework addresses these issues by treating adversarial generation as a sampling problem. By incorporating gradient guidance, MHA generates adversarial text that is both highly effective at fooling models and remarkably fluent.

Methods

MHA frames the search for an adversarial example as a Metropolis-Hastings (M-H) sampling process. The algorithm aims to sample from a stationary distribution that balances two goals: the sentence must be likely according to a pre-trained language model (ensuring fluency) and it must maximize the probability of the model making an error (ensuring attack success).

Example of word-level adversarial perturbations leading to a sentiment classification flip.

The algorithm moves through the sentence space using three operations: replacement, insertion, and deletion of words. It is implemented in two variants:

  • b-MHA (Black-box): Requires only model output labels and uses a language model to pre-select candidate words for replacement.
  • w-MHA (White-box): Leverages model gradients. It uses the cosine similarity between the gradient direction and the word embedding change to guide the sampler toward more effective perturbations, significantly accelerating the process.

Experimental Setup

The researchers evaluated MHA on two standard NLP benchmarks:

  • IMDB: A sentiment analysis dataset of 50,000 movie reviews.
  • SNLI (Stanford Natural Language Inference): A dataset for determining relationships between sentence pairs (Entailment, Contradiction, or Neutral).

The victim models included a bi-LSTM for IMDB and the BiDAF model for SNLI.

Key Results

MHA demonstrated superior performance in both efficiency and attack success. On the SNLI dataset, w-MHA achieved an 88.6% success rate using only 525 model invocations on average. In contrast, the baseline genetic attacker reached only 76.8% success and required nearly double the invocations (971.9).

Comparison of attack success rates vs. model invocations for IMDB and SNLI datasets.

The "ladder-shaped" success curve seen in genetic algorithms (due to their population-based nature) is replaced by a steeper, smoother curve in MHA, particularly the white-box version. Furthermore, MHA examples exhibited significantly lower Perplexity (PPL), indicating higher fluency. Human evaluators ranked b-MHA as the most fluent among tested methods.

Discussion and Conclusion

A key insight of this work is that fluency is a critical feature for adversarial training. "Off-manifold" (gibberish) attacks allow models to learn simple shortcuts to ignore specific weird inputs. In contrast, "on-manifold" (fluent) attacks force the model to improve its fundamental logic. Models trained with MHA-generated examples became more robust against various attacks and even showed slight improvements in standard accuracy on smaller datasets.

While highly effective, MHA occasionally produces incomplete sentences or results in semantic drift (e.g., changing "car" to "home" in NLI tasks). Future work will focus on adding stricter semantic similarity constraints and exploring inverse embedding distances to further refine word selection.

Summary updated

BibTeX
@inproceedings{zhang2019generating,
  author = {Huangzhao Zhang and Hao Zhou and Ning Miao and Lei Li},
  title = {{Generating Fluent Adversarial Examples for Natural Languages}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2019},
  doi = {10.18653/v1/p19-1559},
}
Figure