Gradient-based Adversarial Attacks against Text Transformers

Chuan Guo, Alexandre Sablayrolles, Hervé Jégou, Douwe Kiela

Abstract

Adversarial attacks in Natural Language Processing (NLP) have traditionally relied on discrete, black-box heuristics due to the non-differentiable nature of text. We present GBDA (Gradient-based Distributional Attack), a novel framework that bridges this gap by optimizing a distribution of adversarial examples using the Gumbel-softmax reparameterization trick. By incorporating differentiable constraints for fluency and semantic similarity, GBDA generates adversarial text that is both highly effective at compromising Transformer-based models and natural to human readers. Our results show that GBDA significantly outperforms existing state-of-the-art heuristic attacks in both white-box and black-box transfer settings.

Introduction

In computer vision, adversarial examples are typically generated using gradient-based optimization, leveraging the continuous nature of pixel data. In contrast, the discrete nature of text—where words cannot be modified by infinitesimal increments—has forced NLP researchers to rely on slow, query-intensive heuristic methods. These methods often produce "word salad" that lacks human-level fluency.

GBDA addresses these challenges by bringing the efficiency of gradient-based optimization to the discrete world of NLP. By optimizing in a continuous embedding space and mapping back to discrete tokens, the method identifies adversarial regions rather than single points, enhancing both attack success and transferability.

Methodology

The core of the GBDA framework is the optimization of a parameter matrix $\Theta$, which represents a probability distribution over the vocabulary for each token position in a sentence.

Overview of the GBDA framework using Gumbel-softmax sampling and differentiable constraints.

Gumbel-Softmax Optimization

To allow gradients to flow through the discrete sampling process, GBDA utilizes the Gumbel-softmax distribution. This trick enables the model to "evolve" a sentence toward an adversarial target using standard optimization techniques like Adam. Instead of a greedy search for a single word swap, the model optimizes the entire distributional space.

Differentiable Constraints

To maintain text quality, the loss function includes two critical "guardrails":

  1. Fluency Constraint: A Causal Language Model (e.g., GPT-2) calculates the perplexity of the generated distribution, penalizing nonsensical token combinations.
  2. Semantic Similarity Constraint: BERTScore is employed to ensure the adversarial sentence retains the core meaning of the original input, preventing trivial attacks that simply flip the sentiment or intent (e.g., changing "excellent" to "terrible").

Experimental Setup

GBDA was evaluated across several standard NLP benchmarks, including AG News and DBPedia (Categorization), Yelp and IMDB (Sentiment Analysis), and MNLI (Natural Language Inference). The attacks targeted three prominent Transformer architectures: GPT-2, XLM, and BERT.

Results and Analysis

White-box Performance

In white-box settings, where the attacker has full access to model gradients, GBDA proved devastating. It reduced the accuracy of BERT, GPT-2, and XLM from baseline levels (typically >90%) to below 10% across nearly all tested tasks.

Comparison with State-of-the-Art

GBDA demonstrates superior efficiency and effectiveness compared to leading heuristic-based attacks such as BERT-Attack, BAE, and TextFooler.

Comparison of GBDA against state-of-the-art text attack baselines across multiple datasets.

As shown in the table above, on the AG News dataset, GBDA reduced BERT's accuracy to 8.8% using only 107 queries on average. In comparison, the BAE baseline required 419 queries to reach only 13.0% accuracy. This highlights the efficiency gains provided by gradient-based optimization over greedy word-swapping heuristics.

Human Evaluation and Transferability

Human studies conducted via Amazon Mechanical Turk revealed that GBDA-modified text is often indistinguishable from other top-tier attacks in terms of naturalness, yet it is significantly more successful at fooling the target AI. Furthermore, the distributional nature of the attack allows for high transferability; adversarial samples generated on a source model (like GPT-2) effectively compromise different target architectures (like BERT) in black-box scenarios.

Conclusion

GBDA represents a significant step forward in NLP security, proving that gradient-based attacks are not only possible for discrete text but are more efficient and effective than traditional heuristics. While currently limited to token replacement, future iterations of this framework could expand to include insertions, deletions, and more complex structural edits. This research underscores the ongoing vulnerability of Transformer models and the need for more robust defensive training techniques.

Summary updated

BibTeX
@inproceedings{guo2021gradient,
  author = {Chuan Guo and Alexandre Sablayrolles and Hervé Jégou and Douwe Kiela},
  title = {{Gradient-based Adversarial Attacks against Text Transformers}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.464},
}
Figure