Introduction
Machine learning models for text classification, while powerful, are notoriously brittle. Small, intentional changes to input text—known as adversarial examples—can easily flip a model's prediction. Traditional NLP attacks often rely on rule-based synonym replacement or character swaps, which frequently produce ungrammatical or contextually inappropriate sentences that are easily detected by humans.
This paper introduces BAE (BERT-based Adversarial Examples), a black-box attack that leverages a pre-trained BERT Masked Language Model (MLM) to generate stealthy adversarial text. By replacing or inserting words that fit the local context, BAE maintains high grammatical correctness and semantic similarity, making attacks significantly harder to detect.
Methodology: The Mask and Fill Pipeline
The core of BAE is a "Mask and Fill" strategy that uses BERT's contextual understanding to generate perturbations. The process involves four main steps:
- Importance Identification: The algorithm determines which words are most critical to the classifier's prediction by iteratively deleting words and measuring the resulting drop in confidence.
- Perturbation Generation:
- BAE-R (Replace): Masks an important word and uses BERT-MLM to predict the most likely substitutes.
- BAE-I (Insert): Inserts a mask token adjacent to an important word for BERT-MLM to fill.
- Quality Filtering: To preserve naturalness, a Part-of-Speech (POS) filter ensures replacements match the original tag. Additionally, a Universal Sentence Encoder (USE) ensures the adversarial sentence maintains a cosine similarity of > 0.8 with the original.
- Candidate Selection: The best candidate is chosen based on its ability to flip the label or maximize the confidence drop.
Experimental Setup
BAE was evaluated across seven diverse text classification benchmarks, including sentiment analysis (Amazon, Yelp, IMDB, MR), opinion polarity (MPQA), subjectivity (Subj), and question classification (TREC). These datasets represent a wide range of linguistic challenges for adversarial robustness.
Results and Discussion
BAE consistently outperforms previous state-of-the-art attacks like TextFooler in both attack strength and semantic coherence.
Attack Strength and the Robustness Gap
Even advanced architectures like BERT are highly vulnerable. For instance, on the Movie Reviews (MR) dataset, BAE-R+I reduced a BERT classifier's accuracy from 85.28% to just 19.21%. The "robustness gap" remains significant; while BERT is a stronger classifier than LSTMs or CNNs, it is still susceptible to context-aware perturbations.
Naturalness and Human Evaluation
A key advantage of BAE is its ability to handle homonyms and maintain local context. In blind human evaluations, BAE-generated sentences were rated as significantly more natural than those from previous methods. BAE-I (Insertion) proved particularly effective, showing that adding contextually appropriate words can be just as disruptive as replacing them.
Conclusion and Future Work
While BAE is highly effective, it faces challenges such as "semantic drift," where BERT might suggest a grammatically correct word that flips the intended meaning (e.g., "good" to "bad"). Future work involves using BAE for adversarial training—incorporating these "smart" perturbations into the training set to build more robust models.
Summary updated