Skip to main content

A library for generating text adversarial examples

Project description

TextAttack 🐙

Generating adversarial examples for NLP models

DocsAboutSetupUsageDesign

Coverage Status PyPI version

About

TextAttack is a Python framework for running adversarial attacks against NLP models. TextAttack builds attacks from four components: a search method, goal function, transformation, and set of constraints. TextAttack's modular design makes it easily extensible to new NLP tasks, models, and attack strategies. TextAttack currently supports attacks on models trained for classification, entailment, and translation.

Setup

Installation

You should be running Python 3.6+ to use this package. A CUDA-compatible GPU is optional but will greatly improve code speed. TextAttack is available through pip:

pip install textattack

Configuration

TextAttack downloads files to ~/.cache/textattack/ by default. This includes pretrained models, dataset samples, and the configuration file config.yaml. To change the cache path, set the environment variable TA_CACHE_DIR.

Usage

Running Attacks

The examples/ folder contains notebooks walking through examples of basic usage of TextAttack, including building a custom transformation and a custom constraint. These examples can also be viewed through the documentation website.

We also have a command-line interface for running attacks. See help info and list of arguments with python -m textattack --help.

Attacks and Papers Implemented ("Attack Recipes")

We include attack recipes which build an attack such that only one command line argument has to be passed. To run an attack recipes, run python -m textattack --recipe [recipe_name]

The first are for classification and entailment attacks:

The final is for translation attacks:

Augmenting Text

Many of the components of TextAttack are useful for data augmentation. The textattack.Augmenter class uses a transformation and a list of constraints to augment data. We also offer three built-in recipes for data augmentation:

  • textattack.WordNetAugmenter augments text by replacing words with WordNet synonyms
  • textattack.EmbeddingAugmenter augments text by replacing words with neighbors in the counter-fitted embedding space, with a constraint to ensure their cosine similarity is at least 0.8
  • textattack.CharSwapAugmenter augments text by substituting, deleting, inserting, and swapping adjacent characters

All Augmenter objects implement augment and augment_many to generate augmentations of a string or a list of strings. Here's an example of how to use the EmbeddingAugmenter:

>>> from textattack.augmentation import EmbeddingAugmenter
>>> augmenter = EmbeddingAugmenter()
>>> s = 'What I cannot create, I do not understand.'
>>> augmenter.augment(s)
['What I notable create, I do not understand.', 'What I significant create, I do not understand.', 'What I cannot engender, I do not understand.', 'What I cannot creating, I do not understand.', 'What I cannot creations, I do not understand.', 'What I cannot create, I do not comprehend.', 'What I cannot create, I do not fathom.', 'What I cannot create, I do not understanding.', 'What I cannot create, I do not understands.', 'What I cannot create, I do not understood.', 'What I cannot create, I do not realise.']

Design

TokenizedText

To allow for word replacement after a sequence has been tokenized, we include a TokenizedText object which maintains both a list of tokens and the original text, with punctuation. We use this object in favor of a list of words or just raw text.

Models and Datasets

TextAttack is model-agnostic! Anything that overrides __call__, takes in TokenizedText, and correctly formats output works. However, TextAttack provides pre-trained models and samples for the following datasets:

Classification:

  • AG News dataset topic classification
  • IMDB dataset sentiment classification
  • Movie Review dataset sentiment classification
  • Yelp dataset sentiment classification

Entailment:

  • SNLI datastet
  • MNLI dataset (matched & unmatched)

Translation:

  • newstest2013 English to German dataset

Attacks

The attack_one method in an Attack takes as input a TokenizedText, and outputs either a SuccessfulAttackResult if it succeeds or a FailedAttackResult if it fails. We formulate an attack as consisting of four components: a goal function which determines if the attack has succeeded, constraints defining which perturbations are valid, a transformation that generates potential modifications given an input, and a search method which traverses through the search space of possible perturbations.

Goal Functions

A GoalFunction takes as input a TokenizedText object and the ground truth output, and determines whether the attack has succeeded, returning a GoalFunctionResult.

Constraints

A Constraint takes as input a current TokenizedText, and a list of transformed TokenizedTexts. For each transformed option, it returns a boolean representing whether the constraint is met.

Transformations

A Transformation takes as input a TokenizedText and returns a list of possible transformed TokenizedTexts. For example, a transformation might return all possible synonym replacements.

Search Methods

A SearchMethod takes as input an initial GoalFunctionResult and returns a final GoalFunctionResult The search is given access to the get_transformations function, which takes as input a TokenizedText object and outputs a list of possible transformations filtered by meeting all of the attack’s constraints. A search consists of successive calls to get_transformations until the search succeeds (determined using get_goal_results) or is exhausted.

Contributing to TextAttack

We welcome suggestions and contributions! Submit an issue or pull request and we will do our best to respond in a timely manner. TextAttack is currently in an "alpha" stage in which we are working to improve its capabilities and design.

Citing TextAttack

If you use TextAttack for your research, please cite TextAttack: A Framework for Adversarial Attacks in Natural Language Processing.

@misc{Morris2020TextAttack,
    Author = {John X. Morris and Eli Lifland and Jin Yong Yoo and Yanjun Qi},
    Title = {TextAttack: A Framework for Adversarial Attacks in Natural Language Processing},
    Year = {2020},
    Eprint = {arXiv:2005.05909},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textattack-0.0.2.4.tar.gz (73.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textattack-0.0.2.4-py3-none-any.whl (130.1 kB view details)

Uploaded Python 3

File details

Details for the file textattack-0.0.2.4.tar.gz.

File metadata

  • Download URL: textattack-0.0.2.4.tar.gz
  • Upload date:
  • Size: 73.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for textattack-0.0.2.4.tar.gz
Algorithm Hash digest
SHA256 eb9c94e12b7ec558e543623c09be7bc32bb916aa584e3a9ca7b2864679c81c08
MD5 0747f5074a92a39533854e048b2e0705
BLAKE2b-256 71c8bb02fdd0491fd3d07917edcec9f606a9ec845e47e1b0dfce23f87cd6b7e3

See more details on using hashes here.

File details

Details for the file textattack-0.0.2.4-py3-none-any.whl.

File metadata

  • Download URL: textattack-0.0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 130.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for textattack-0.0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 500103f7127f689bdb167818e2cee14f73b738307c633f77afb20564cec8de55
MD5 c7f6c0a115228f4879f76f5c3c2606f8
BLAKE2b-256 f1c8637e92485e4b295894f13396e3b129a9a5deff78c07d53a00c0828fcabfa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page