TextAttack Basic Functions¶
Welcome to the documentation for TextAttack!
What is TextAttack?¶
TextAttack is a Python framework for adversarial attacks, adversarial training, and data augmentation in NLP.
TextAttack makes experimenting with the robustness of NLP models seamless, fast, and easy. It’s also useful for NLP model training, adversarial training, and data augmentation.
TextAttack provides components for common NLP tasks like sentence encoding, grammar-checking, and word replacement that can be used on their own.
Where should I start?¶
This is a great question, and one we get a lot. First of all, almost everything in TextAttack can be done in two ways: via the command-line or via the Python API. If you’re looking to integrate TextAttack into an existing project, the Python API is likely for you. If you’d prefer to use built-in functionality end-to-end (training a model, running an adversarial attack, augmenting a CSV) then you can just use the command-line API.
TextAttack does three things very well:
- Adversarial attacks (Python:
- Data augmentation (Python:
- Model training (Python:
Adversarial training can be achieved as a combination of  and/or  with  (via
textattack train --attack). To see all this in action, see the TextAttack End-to-End tutorial.
All of the other components: datasets, models & model wrappers, loggers, transformations, constraints, search methods, goal functions, etc., are developed to support one or more of these three functions. Feel free though to install textattack to include just one of those components! (For example, TextAttack provides a really easy Python interface for accessing and using word embeddings that will automatically download and save them on the first use.)
TextAttack provides a framework for constructing and thinking about generating inputs in NLP via perturbation attacks.
TextAttack builds attacks from four components:
- Goal Functions stipulate the goal of the attack, like to change the prediction score of a classification model, or to change all of the words in a translation output.
- Constraints determine if a potential perturbation is valid with respect to the original input.
- Transformations take a text input and transform it by inserting and deleting characters, words, and/or phrases.
- Search Methods explore the space of possible transformations within the defined constraints and attempt to find a successful perturbation which satisfies the goal function.
TextAttack provides a set of Attack Recipes that assemble attacks from the literature from these four components. Take a look at these recipes (or our paper on ArXiv) to get a feel for how the four components work together to create an adversarial attack.
Data augmentation is easy and extremely common in computer vision but harder and less common in NLP. We provide a Data Augmentation module using transformations and constraints.
TextAttack has some other features that make it a pleasure to use:
- Pre-trained Models for testing attacks and evaluating constraints
- Visualization options like Weights & Biases and Visdom
- AttackedText, a utility class for strings that includes tools for tokenizing and editing text
- Tutorial 0: TextAttack End-To-End (Train, Eval, Attack)
- Tutorial 1: Transformations
- Tutorial 2: Constraints
- Tutorial 3: Attacking TensorFlow models
- Tutorial 4: Attacking scikit-learn models
- Tutorial 5: Attacking AllenNLP models
- Tutorial 6: Attacking multilingual models
- How can I contribute to TextAttack?
- Lessons learned in designing TextAttack
- Selected API References
- Attack Recipes
- Attack Result
- Attacked Text
- Augmenter Recipes
- Goal Function
- Goal Function Result
- Misc Checkpoints
- Misc Loggers
- Misc Validators
- Search Methods
- Composite Transformation
- Word Swap
- Word Swap by Embedding
- Word Swap by WordNet Word Replacement
- Word Swap by Gradient
- Word Swap by Homoglyph
- Word Swap by Neighboring Character Swap
- Word Swap by Random Character Deletion
- Word Swap by Random Character Insertion
- Word Swap by Random Character Substitution
- Word Swap by Changing Location
- Word Swap by Changing Number
- Word Swap by Changing Name
- Word Swap by Contraction
- Word Swap by Extension