Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Word-level Adversarial Examples in Convolutional Neural Networks for Sentence Classification

This repository holds the Word-level Adversarial Examples codes and models for the papers HotFlip: White-Box Adversarial Examples for Text Classification

[Arxiv Preprint] [ACL 2018]

This repository includes robustness improvements using adversarial training improvement.

Some examples generated from CNN are able to trick both CNN and BLSTM. please have a look at examples.txt, with the first column as its label, the second as its confidence, the third as the sentence. examples_turker.txt are examples given to Amazon Mechanical Turk and they annoted the adversarial and original examples share the same meanings.


Code is written in Python (2.7) and requires Theano (0.9), NLTK.

Using the pre-trained word2vec vectors will also require downloading the binary file from

Data Preprocessing

To process the raw data, please refer to

set word2vec path points to the word2vec binary file (i.e. GoogleNews-vectors-negative300.bin file).

Different Versions of Codes Only one word flip Allow two word flip Only one word flip using gradient subtraction. Only one word flip with labels change Use pretrained model to generate adversarial test set and test accuracy (with confidence) on it. As same as Kim's CNN

  • lstm The adversarial examples generated from CNN are able to attack a BLSTM. Train a BLSTM model Use adversarial examples from CNN (e.g. sst2_0.4_two_examples.txt) to attack pretrained BLSTM model.

Running the models (CPU)

Example commands:

THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python -nonstatic -rand
THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python -static -word2vec
THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python -nonstatic -word2vec

This will run the CNN-rand, CNN-static, and CNN-nonstatic models respectively.

Using the GPU

GPU will result in a good 10x to 20x speed-up, so it is highly recommended. To use the GPU, simply change device=cpu to device=gpu (or whichever gpu you are using). For example:

THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python -nonstatic -word2vec


If you use this repository in your researh, please cite:

  title={HotFlip: White-Box Adversarial Examples for Text Classification},
  author={Ebrahimi, Javid and Rao, Anyi and Lowd, Daniel and Dou, Dejing},
  booktitle={Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers)},


Code for ACL2018 HotFlip: White-Box Adversarial Examples for Text Classification, Word-level Adversarial Examples



No releases published


No packages published