Generating Fluent Adversarial Examples for Natural Languages

Huangzhao Zhang, Hao Zhou, Ning Miao, Lei Li

Abstract

Adversarial attacks in natural language processing (NLP) face unique challenges due to the discrete nature of text and the difficulty of maintaining linguistic fluency. We present the Metropolis-Hastings Attack (MHA), a novel framework that treats adversarial generation as a sampling problem. By balancing model-fooling effectiveness with language model-guided fluency, MHA generates adversarial examples that are both highly successful and remarkably natural. Our results on IMDB and SNLI benchmarks demonstrate that MHA outperforms traditional genetic algorithms in both speed and success rate, while providing superior data for robust adversarial training.

Introduction

Adversarial examples—inputs specifically crafted to deceive machine learning models—have been extensively studied in computer vision. In the continuous space of images, small gradient-based perturbations can easily flip a model's prediction. However, natural language is discrete; changing a single character or word can lead to "unfluent" gibberish that is easily detectable or irrelevant for robust training.

Existing methods, such as genetic algorithms, often produce sentences that lack grammatical coherence. This is problematic for adversarial training: if a model is trained on nonsense, it learns to ignore noise rather than understanding the subtle semantic boundaries of human language. To address this, we propose MHA, which leverages Markov Chain Monte Carlo (MCMC) sampling to explore the space of fluent, effective adversarial perturbations.

Methodology: The Metropolis-Hastings Attack

The core of MHA is the Metropolis-Hastings (M-H) sampling technique. We define a stationary distribution $\pi$ that incorporates two primary objectives:

  1. Fluency: Quantified by a pre-trained Language Model (LM).
  2. Effectiveness: Quantified by the victim classifier’s probability of producing an incorrect label.

The algorithm explores the sentence space through a series of proposed word replacements, insertions, or deletions. Each change is accepted or rejected based on a calculated rate that ensures the final samples align with our target distribution.

The Manifold Argument

A key motivation for MHA is the "manifold" hypothesis. Real-world sentences lie on a low-dimensional manifold within the high-dimensional space of all possible word combinations. Unfluent adversarial examples often fall off this manifold.

Conceptual illustration of the data manifold and decision boundaries.

As shown in Figure 1, a model can "defend" against off-manifold examples by simply adjusting its decision boundary in empty space. In contrast, fluent adversarial examples exist within the manifold, forcing the model to refine its logic where real data resides.

Black-Box vs. White-Box Attacks

We introduce two variants of the framework:

  • Black-Box (b-MHA): The attacker only has access to the model's output. It uses a language model to suggest contextually appropriate candidate words.
  • White-Box (w-MHA): The attacker utilizes the model's gradients. By calculating the gradient of the loss with respect to word embeddings, w-MHA identifies replacements that move the sentence most efficiently toward a misclassification.

Experimental Results

We evaluated MHA on the IMDB sentiment analysis dataset and the Stanford Natural Language Inference (SNLI) dataset.

Efficiency and Success Rate

MHA demonstrates significant improvements in efficiency over population-based genetic attacks. Because MHA is a single-chain sampler, it requires far fewer queries (invocations) to the victim model.

Success rate vs. number of model invocations for IMDB and SNLI datasets.

On the SNLI dataset, w-MHA achieved an 88.6% success rate with an average of only 525 model invocations. In comparison, the baseline genetic attacker reached a 76.8% success rate and required nearly double the invocations (971.9).

Fluency and Robustness

Beyond success rates, MHA produces significantly more fluent text. Measured by Perplexity (PPL), MHA examples on SNLI scored ~332, whereas genetic attacks scored ~834. Lower perplexity indicates that the generated text is more natural and less "surprising" to a language model.

Furthermore, models trained using MHA-generated examples showed enhanced robustness. Adversarial training with MHA examples created models that were harder for both MHA and genetic attackers to fool, whereas models trained on genetic attacks remained vulnerable to the more fluent MHA perturbations.

Conclusion and Future Work

The Metropolis-Hastings Attack provides a powerful framework for generating high-quality adversarial text. By prioritizing fluency, MHA ensures that adversarial examples remain relevant to the underlying data distribution, making them more effective for both attacking and hardening NLP models. Future work will focus on mitigating "semantic drift"—where word changes inadvertently alter the ground-truth label—and refining grammatical constraints to ensure complete, well-formed sentences.

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