TextAttack: A Framework for Adversarial Attacks, Data Augmentation, and Adversarial Training in NLP

John X. Morris, Eli Lifland, Jin Yong Yoo, Jake Grigsby, Di Jin, Yanjun Qi

Abstract

TextAttack is an open-source Python framework designed to unify the fragmented landscape of NLP adversarial attacks into a single, modular library. By decomposing attacks into four plug-and-play components—goal functions, constraints, transformations, and search methods—it enables researchers to benchmark existing methods, develop novel attacks, and utilize these techniques for data augmentation and adversarial training.

Introduction

In computer vision, adversarial attacks are well-established, yet Natural Language Processing (NLP) has historically lacked a standardized infrastructure for robustness testing. Prior to TextAttack, new attacks were typically released in isolated repositories, making fair comparisons across different models and datasets nearly impossible. TextAttack addresses this "Wild West" scenario by providing a unified "language" and infrastructure for NLP robustness.

Methodology

The core innovation of TextAttack is its modular design. The framework decomposes almost every NLP attack into four distinct components:

  1. Goal Function: Defines the criteria for a successful attack (e.g., changing a classification or reducing a BLEU score).
  2. Constraints: Rules to ensure the perturbed text remains human-readable and semantically similar to the original.
  3. Transformation: The mechanism for altering text, such as synonym swapping or character insertion.
  4. Search Method: The algorithm used to find the most effective transformation, such as greedy search or genetic algorithms.
TextAttack framework overview illustrating the modular components for developing and utilizing attacks.

This separation allows researchers to create novel attacks by combining components from disparate sources. TextAttack includes "recipes" for 16 influential attacks and integrates with the Hugging Face ecosystem, providing access to the GLUE benchmark and over 82 pre-trained models.

Applications and Key Results

TextAttack demonstrates that adversarial techniques are valuable not only for breaking models but also for improving them through data augmentation and adversarial training.

Model Fragility and Adversarial Training

Benchmarking reveals that standard models are highly vulnerable. For instance, a BERT-based sentiment classifier's accuracy can drop from near-perfect to nearly zero under the TextFooler attack. However, TextAttack facilitates adversarial training to mitigate these vulnerabilities. Experimental results show that training an LSTM on adversarial examples generated by deepwordbug improved its accuracy under that specific attack from 23.46% to 35.07%.

Data Augmentation

The framework’s transformations can be used to generate synthetic training data. Results highlight that TextAttack’s data augmentation recipes significantly boost model performance, particularly in low-data regimes with fewer than 1,000 samples.

Evaluation accuracy of a WordCNN model using different augmentation methods (EDA and Embedding) across varying training sample sizes.

Conclusion

TextAttack provides a standardized catalyst for NLP robustness research, moving the field toward fair head-to-head comparisons. While the library must be manually updated as new methods emerge, it establishes a foundation for making robustness testing a standard component of the NLP development pipeline.

Summary updated

BibTeX
@inproceedings{morris2020textattack,
  author = {John X. Morris and Eli Lifland and Jin Yong Yoo and Jake Grigsby and Di Jin and Yanjun Qi},
  title = {{TextAttack: A Framework for Adversarial Attacks, Data Augmentation, and Adversarial Training in NLP}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2020},
  doi = {10.18653/v1/2020.emnlp-demos.16},
}
Figure