Abstract
We study an important task of attacking natural language processing models in a black box setting. We propose an attack strategy that crafts semantically similar adversarial examples on text classification and entailment tasks. Our proposed attack finds candidate words by considering the information of both the original word and its surrounding context. It jointly leverages masked language modelling and next sentence prediction for context understanding. In comparison to attacks proposed in prior literature, we are able to generate high quality adversarial examples that do significantly better both in terms of success rate and word perturbation percentage.
Introduction
Deep learning models for natural language processing (NLP) have demonstrated remarkable performance across various tasks. However, these models are surprisingly fragile. Small, imperceptible changes to an input—such as replacing a single word with a synonym—can cause a model to flip its prediction, a phenomenon known as an adversarial attack.
While generating adversarial examples in computer vision is relatively straightforward due to the continuous nature of image data, NLP presents a unique challenge because text is discrete. Previous black-box attacks often relied on simple synonym lookups using resources like WordNet. These methods frequently fail because they ignore context (e.g., confusing different meanings of the word "bank") and often produce "word salad"—sentences that are grammatically incorrect or nonsensical to human readers. This paper introduces a context-aware attack strategy designed to generate high-quality, human-like adversarial examples.
Methodology
The proposed attack follows a two-stage process: word ranking and context-aware word substitution.
1. Word Ranking
To maximize efficiency, the algorithm first identifies which words in a sentence are most critical to the target model's prediction. By systematically removing words and observing the resulting drop in the model's confidence score, the attack prioritizes the most influential words for replacement.
2. Context-Aware Substitution
The core innovation of this work lies in using BERT to generate replacement candidates. Unlike traditional methods, this approach leverages two specific features of BERT:
- Contextual Masking: The target word is replaced with a
[MASK]token, allowing BERT to suggest words that fit the surrounding grammatical structure. - The NSP Trick: To ensure the replacement word preserves the original meaning, the authors utilize BERT’s Next Sentence Prediction (NSP) head. By feeding BERT a pair consisting of the original sentence and the modified sentence, the model is forced to suggest candidates that are consistent with the original intent.
- Refinement: Candidates are further filtered using counter-fitted word embeddings and the Universal Sequence Encoder (USE) to ensure high semantic similarity and overall sentence coherence.
Experimental Results
The attack was evaluated against state-of-the-art models (BERT and LSTM) on three benchmarks: IMDB and Movie Reviews (MR) for sentiment analysis, and the Stanford Natural Language Inference (SNLI) dataset.
As shown in the results table, the proposed method consistently outperforms baselines like TextFooler (TF) and PWWS. For a BERT model trained on IMDB, the attack reduced accuracy to 9.5%, compared to TextFooler's 13.6%. Crucially, it achieved this with a lower perturbation rate (4.2% vs. 6.1%) and a significantly lower grammatical error increase rate ($I\%$), indicating that the adversarial examples are both more effective and more stealthy.
Qualitative Analysis
The strength of the context-aware approach is most evident when examining the generated text. Previous methods often substituted words in ways that changed the sentiment or rendered the sentence awkward.
In the examples above, the proposed method selects replacements like "words" or "sketch" that maintain the flow and meaning of the original sentence, whereas the baseline (TF) often chooses less natural substitutes like "qualifier" or "spitting."
Conclusion
This research demonstrates that context is vital for generating effective and natural adversarial attacks in NLP. By leveraging BERT's deep understanding of language structure and intent, the proposed method generates adversarial examples that are harder for both machines and humans to detect. While the computational cost of querying BERT for every substitution remains a challenge, this work sets a new standard for fluency and semantic preservation in adversarial NLP.
Summary updated