Abstract
Adversarial attacks on Natural Language Processing (NLP) models often produce ungrammatical or contextually inappropriate text, making them easily detectable by humans. We present CLARE (Contextualized Adversarial Example generation), a novel framework that produces highly fluent and effective adversarial examples. By employing a "mask-then-infill" strategy powered by pre-trained language models, CLARE applies contextualized replacements, insertions, and merges. This approach allows for more flexible edits that maintain high textual similarity and naturalness while significantly outperforming existing state-of-the-art attack methods.
Introduction
Machine learning models for Natural Language Processing (NLP) are notoriously brittle. Researchers use "adversarial attacks"—small, intentional perturbations to input text—to identify and mitigate these weaknesses. However, the utility of these attacks is often limited by the quality of the generated text. Most existing methods rely on context-agnostic heuristic rules, such as swapping a word for a synonym found in a static dictionary.
These traditional changes frequently result in text that sounds bizarre or ungrammatical to a human reader. Furthermore, many current methods are restricted to "one-for-one" word replacements, requiring the adversarial sentence to maintain the exact same length as the original. This rigid constraint limits the search space for potential vulnerabilities and often leads to less effective attacks that are easily spotted by human observers.
Methodology: The CLARE Framework
CLARE moves away from rigid, rule-based substitutions in favor of a probabilistic "mask-then-infill" approach. This method leverages the power of pre-trained Masked Language Models (MLMs) to ensure that every modification is contextually appropriate.
The Engine
CLARE utilizes RoBERTa as its core engine. Because the MLM is trained to predict missing tokens based on their surrounding context, its suggestions are naturally fluent. This allows the framework to generate perturbations that respect the semantic and syntactic nuances of the original sentence.
Three Perturbation Actions
Unlike models limited to simple word replacement, CLARE introduces three distinct actions to manipulate text:
- Replace: Swaps a word for a contextually fit alternative suggested by the MLM.
- Insert: Adds a new word into the sentence at a vulnerable position.
- Merge: Combines two adjacent words (a bigram) into a single new word that captures the combined meaning.
Attack Strategy
For a given input, CLARE identifies the most "vulnerable" positions—locations where a change is most likely to confuse the "victim" model. It then ranks potential changes based on a dual objective: maximizing the decrease in the victim model's confidence in the correct label while maintaining high semantic similarity to the original text.
Experimental Setup
The researchers evaluated CLARE across several diverse NLP tasks using standard benchmarks, including Yelp Reviews for sentiment analysis, AG News for topic classification, and MNLI/QNLI for Natural Language Inference (NLI). The framework was tested against established baselines such as TextFooler and BERTAttack.
Key Insights and Results
Performance and Quality
CLARE consistently outperformed state-of-the-art baselines across all metrics. On the MNLI dataset, CLARE achieved an 88.1% attack success rate, significantly higher than BERTAttack (82.7%) and TextFooler (59.8%). Beyond raw success rates, the quality of the output was markedly superior. CLARE’s adversarial examples maintained higher textual similarity scores (often exceeding 0.80) and exhibited far fewer grammatical errors.
Context and Flexibility
By using an MLM to "fill in the blanks," CLARE's changes respect the nuances of the sentence. This results in much lower "perplexity" (a measure of how unnatural text is) compared to synonym-swapping baselines. Furthermore, the ability to add or merge words (changing the sentence length) provides a wider search space, allowing CLARE to achieve higher success rates with fewer total modifications.
Human Evaluation
In blind tests, human judges preferred CLARE’s adversarial examples over those generated by TextFooler by a wide margin. The judges cited better fluency and superior preservation of the original meaning as the primary reasons for their preference.
Discussion and Future Work
Analysis showed that CLARE naturally gravitates toward perturbing nouns and noun phrases, which often carry the most predictive weight in tasks like topic classification. Additionally, the authors found that CLARE-generated examples can be used for adversarial training. In low-data scenarios, this acted as high-quality data augmentation, improving the model's standard accuracy.
Future work will focus on expanding the "Merge" action beyond noun phrases and optimizing the computational cost. While using a distilled version of RoBERTa improves speed, the process remains more resource-intensive than simple heuristic lookups. Additionally, reducing the number of queries required to the victim model in black-box settings remains a key area for optimization.
Summary updated