Certified Robustness to Text Adversarial Attacks by Randomized [MASK]

Jiehang Zeng, Xiaoqing Zheng, Jianhan Xu, Linyang Li, Liping Yuan, Xuanjing Huang

This paper introduces RanMASK, a novel defense mechanism designed to make Natural Language Processing (NLP) models provably robust against adversarial attacks. By leveraging the power of random masking and the pre-existing capabilities of models like BERT, the authors provide a scalable way to guarantee that a classifier's prediction won't flip, even if an attacker strategically alters several words or characters.

TL;DR

RanMASK achieves certified robustness against text adversarial attacks by randomly masking a large portion of input words and using a majority vote across these masked versions to make a final prediction. Unlike previous certified defenses, it is "attack-agnostic," meaning it provides provable guarantees without needing to know the specific synonym lists or strategies an attacker might use.

What Problem Does This Paper Address?

Deep neural networks in NLP are notoriously fragile. Small, human-imperceptible changes—like replacing a word with a synonym ("happy" to "glad") or introducing a typo ("football" to "fo0tba1l")—can easily trick a model into changing its prediction.

Defenses generally fall into two categories:

  1. Empirical Defenses: These work well in practice against known attacks but offer no guarantees against a new, more sophisticated attack.
  2. Certified Defenses: These provide a mathematical guarantee (a "certificate") that the model's prediction will remain constant within a certain range of perturbations.

Existing certified defenses for NLP have two major flaws: they struggle to scale to large architectures like BERT, and they often assume the defender knows exactly which synonyms an attacker might use. RanMASK aims to provide a scalable, attack-agnostic certified defense that handles both word-level and character-level perturbations.

Methods

RanMASK is based on a technique called randomized smoothing.

  1. Random Masking: Given an input text, the system generates many "masked" copies. In each copy, a significant percentage (the masking rate, $\rho$) of words are randomly replaced with the [MASK] token.
  2. Base Classifier: These masked copies are fed into a base classifier (like BERT or RoBERTa). Because these models are pre-trained on masked language modeling, they are naturally adept at "recovering" context even when many words are missing.
  3. Majority Vote: The final prediction is determined by a majority vote across all the masked samples.
The RanMASK workflow: an adversarial input is masked multiple times, classified, and the final result is determined by majority vote.

The core intuition is that if only a few words are maliciously changed, it is highly unlikely that all those changed words will appear in any single masked version. If enough original, unperturbed context remains, the base classifier will likely still predict the correct class.

The authors also provide a robustness certificate (Theorem 1). By estimating a value $\beta$ (the probability that the classifier is correct given that at least one perturbed word was not masked), they can mathematically define the maximum number of words an attacker can change before the majority vote is guaranteed to flip.

Datasets

The authors evaluated RanMASK on four standard NLP benchmarks:

  • AGNEWS: Topic classification (4 classes).
  • SST2: Sentiment analysis (binary).
  • IMDB: Long-form movie reviews (binary sentiment).
  • SNLI: Natural Language Inference (predicting if a hypothesis entails, contradicts, or is neutral toward a premise).

Key Insights and Ideas

  • Leveraging Pre-training: RanMASK is elegant because it uses the [MASK] token—a fundamental part of how models like BERT are trained—as a defense mechanism rather than just a training objective.
  • Attack Agnosticism: Because the defense relies on removing information (masking) rather than anticipating specific changes (synonym substitution), it doesn't need to know the attacker's synonym dictionary. This makes it much more robust in real-world scenarios.
  • Majority Vote vs. Logits: The authors found that "majority vote" ensemble methods are significantly more robust than "logit-summed" methods. Majority voting creates a "sharper" decision boundary that is harder for greedy, score-based attack algorithms to navigate.
  • Tighter Bounds: By introducing the $\beta$ parameter and estimating it via Monte Carlo methods, the authors achieved tighter certified bounds than previous "ablation" methods used in the image domain.
Analysis showing that the estimated value of $\beta$ closely approximates the class probability $p_c(x)$, allowing for a tighter and more practical robustness certificate.

Key Results

  • Certified Robustness: On AGNEWS, RanMASK can certify that over 50% of texts are robust to any perturbation of up to 5 words. On SST2, it certifies robustness for over 50% of texts against 2-word perturbations.
  • Empirical Performance: RanMASK consistently outperformed strong empirical defenses like FreeLB and PGD-K. For example, on AGNEWS under the TextFooler attack, RanMASK achieved a robust accuracy of 55.1%, compared to 24.6% for FreeLB.
  • Character-Level Defense: Unlike many word-substitution defenses, RanMASK proved highly effective against character-level attacks (like DeepWordBug), significantly increasing robust accuracy compared to the baseline.

Limitations and Future Work

  • Clean Accuracy Trade-off: There is an inherent trade-off. A high masking rate ($\rho$) increases robustness but decreases accuracy on "clean" (unattacked) data. Finding the optimal $\rho$ is dataset-dependent.
  • Computational Cost: Because the method requires running multiple masked versions of an input through a large model like BERT, inference is slower than a standard classifier.
  • Future Directions: The authors suggest a two-stage framework: use a fast "adversarial detector" first. If a text is flagged as suspicious, use the robust RanMASK classifier; otherwise, use a standard, faster model to maintain high clean accuracy. They also suggest combining RanMASK with other orthogonal defenses like adversarial training.

Summary updated

BibTeX
@misc{zeng2021certified,
  author = {Jiehang Zeng and Xiaoqing Zheng and Jianhan Xu and Linyang Li and Liping Yuan and Xuanjing Huang},
  title = {{Certified Robustness to Text Adversarial Attacks by Randomized [MASK]}},
  year = {2021},
  eprint = {2105.03743},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2105.03743},
}
Figure