NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

BERT-ATTACK: Adversarial Attack Against BERT Using BERT

Linyang Li, Ruotian Ma, Qipeng Guo, Xiangyang Xue, Xipeng Qiu

Introduction

Adversarial attacks—small, intentional perturbations designed to deceive machine learning models—have been extensively studied in the context of computer vision. However, applying these techniques to Natural Language Processing (NLP) presents a unique challenge: language is discrete. Unlike image pixels, which can be subtly adjusted, words must be swapped entirely.

Previous text-based attacks relied on heuristic rules, such as character swapping or fixed synonym dictionaries. These methods frequently suffer from two major drawbacks: poor grammatical fluency and a lack of semantic consistency. Often, the resulting adversarial samples are unintelligible to humans or change the sentence's meaning so drastically that the attack is no longer valid.

Methodology: Turning BERT Against Itself

The researchers introduce BERT-Attack, a high-quality adversarial method that leverages a pre-trained Masked Language Model (MLM)—specifically BERT—to generate deceptive text. The algorithm operates in two distinct stages:

  1. Identifying Vulnerable Words: The system determines which words are most critical to the target model's prediction. By iteratively replacing each word with a [MASK] token and measuring the resulting drop in the target model's confidence score, the algorithm ranks words by their "vulnerability."
  2. Contextual Word Replacement: Instead of a static thesaurus, BERT-Attack uses the original sentence context to predict the top-$K$ most likely replacements for vulnerable positions. By not masking the target word during generation, the model ensures that replacements are both semantically and grammatically natural.
Overview of the BERT-Attack framework, illustrating the two-stage process of identifying vulnerable words and generating replacements using a pre-trained BERT model.

The method also incorporates sub-word awareness, allowing it to attack Byte-Pair Encoding (BPE) tokens, and applies filters to prevent semantic "drift," such as accidentally replacing a word with its antonym.

Experimental Results

BERT-Attack was evaluated against fine-tuned BERT models and LSTMs across several benchmarks, including sentiment analysis (Yelp, IMDB), topic classification (AG’s News), and Natural Language Inference (SNLI, MNLI).

Performance comparison of BERT-Attack against baseline methods (TextFooler and GA) across various datasets, highlighting its superior success rate and lower perturbation percentage.

As shown in the results above, BERT-Attack consistently outperforms previous state-of-the-art methods like TextFooler:

  • Attack Success: It reduced a fine-tuned BERT's accuracy on the IMDB dataset from 90.9% to a mere 11.4%.
  • Efficiency and Perturbation: On the Yelp dataset, BERT-Attack achieved success by changing only 4.1% of the words, whereas TextFooler required a 12.8% change.
  • Fluency: Human evaluations confirmed that BERT-Attack produces more fluent and semantically similar samples than rule-based alternatives.

Discussion and Future Work

The core innovation of BERT-Attack is treating a pre-trained BERT as a "contextual thesaurus" that understands the natural language manifold. This approach is not only more effective but also significantly faster—roughly 3x faster than TextFooler—because it generates all potential candidates in a single forward pass.

While highly effective, the method still faces challenges with very short sequences and occasional semantic drift (the "antonym problem"). Future research will likely focus on using these high-quality adversarial samples for adversarial training, thereby hardening NLP models against increasingly sophisticated deceptive inputs.

Summary updated

BibTeX
@inproceedings{li2020bert,
  author = {Linyang Li and Ruotian Ma and Qipeng Guo and Xiangyang Xue and Xipeng Qiu},
  title = {{BERT-ATTACK: Adversarial Attack Against BERT Using BERT}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2020},
  doi = {10.18653/v1/2020.emnlp-main.500},
}
Figure