Introduction
Deep Learning models for Natural Language Processing (NLP) are notoriously vulnerable to adversarial attacks—small, intentional changes to input text that cause a model to make a mistake. While these attacks are well-studied in computer vision, they are significantly more challenging in NLP due to the discrete nature of text.
Existing "black-box" attacks typically identify important words and replace them with synonyms from static dictionaries like WordNet. However, this approach often ignores the surrounding context, leading to replacements that are semantically incorrect or grammatically awkward. This lack of fluency makes adversarial examples easy for humans to detect. This paper addresses these limitations by introducing a context-aware framework that leverages pre-trained language models to generate more natural and effective attacks.
Methodology
The proposed attack follows a two-step framework designed to maximize both success rates and linguistic fluency.
1. Word Ranking
The algorithm first identifies the most influential words in the input text ($X$) relative to the target model's prediction. By systematically removing words and measuring the resulting drop in classification scores, the system ranks words by their importance to the model's decision-making process.
2. Context-Aware Word Substitution
For the highest-ranked words, the system generates replacements using BERT in a novel way:
- Candidate Generation: The target word is masked, and both the original sentence and the masked sentence are fed into BERT, separated by a
[SEP]token. This utilizes BERT’s Next Sentence Prediction (NSP) and Masked Language Modeling (MLM) to suggest words that are semantically consistent with the original intent and grammatically appropriate for the new context. - Filtering and Selection: Candidates are filtered using counter-fitting techniques and the Universal Sentence Encoder (USE) to ensure the sentence's core meaning is preserved. The final selection is the candidate that most effectively reduces the target model's confidence or flips its prediction.
Experimental Results
The approach was evaluated on three major benchmarks: IMDB (sentiment classification), MR (movie reviews), and SNLI (natural language inference). The proposed method was compared against state-of-the-art baselines, including TextFooler (TF) and Genetic Attack (GA).
Key Findings
- Superior Success Rates: On the IMDB dataset with a BERT target model, the attack reduced accuracy from 90.9% to just 9.5%, outperforming TextFooler's 13.6%.
- Enhanced Stealthiness: The "Perturbation Rate" (percentage of words changed) was consistently lower. For the IMDB/BERT configuration, the attack required changing only 4.2% of words, compared to 6.1% for TextFooler.
- Improved Fluency: The grammatical error increase (I%) was significantly lower than baselines. Notably, on the SNLI dataset, the proposed attack showed an error increase of only 1.9%, whereas TextFooler resulted in a 9.7% increase.
Conclusion
By using BERT's NSP as a semantic anchor, this method moves beyond simple synonym replacement to generate "contextual synonyms" that fit the flow of a sentence naturally. While the approach is computationally more expensive than dictionary-based methods due to the overhead of querying a large language model, it represents a significant step forward in creating stealthy and effective adversarial text. Future work will focus on optimizing the generation speed and further refining semantic constraints.
Summary updated