BERT-Defense: A Probabilistic Model Based on BERT to Combat Cognitively Inspired Orthographic Adversarial Attacks

Yannik Keller, Jan Mackensen, Steffen Eger

Introduction

Deep learning models for Natural Language Processing (NLP) are notoriously fragile when faced with adversarial attacks. While "high-level" attacks involve finding clever synonyms to flip a model's prediction, "low-level" orthographic attacks target the characters themselves. Most existing defenses are designed for simple typos (swaps, deletions, or keyboard misses). However, recent benchmarks like Zéroe introduce more sophisticated attacks inspired by human cognition, such as visual perturbations (e.g., replacing 'a' with 'a̰'), phonetic attacks (e.g., "is" to "izz"), and segmentation errors (e.g., "Aman isdriving").

Standard spell-checkers and specialized neural models like ScRNN often fail to generalize to these complex attacks because they are typically trained on a narrow set of simple noise. BERT-Defense is proposed as a novel, untrained adversarial defense pipeline that restores sentences corrupted by these cognitively inspired character-level attacks without requiring task-specific training.

Methodology

BERT-Defense utilizes a three-stage iterative pipeline that leverages the general-purpose linguistic knowledge of large pre-trained models.

1. Candidate Phase: Context-Independent Probability

The model uses a Modified Levenshtein Distance to compare attacked tokens against BERT’s word-piece vocabulary. This version tracks start and end indices to handle segmentation errors, allowing the model to "re-glue" words that were incorrectly split or joined. It generates a set of initial hypotheses for the original tokens.

2. Refinement Phase: Context-Dependent Probability

The model employs BERT’s Masked Language Modeling (MLM) to refine these candidates. Instead of a single word, BERT receives a weighted average embedding of likely candidates for a given position. This process is iterative: the model masks one token, predicts the most likely word based on the surrounding "cleaned" context, and repeats this for all tokens in the sentence to gradually clear adversarial noise.

3. Selection Phase: Hypothesis Selection

After BERT has polished several versions of the sentence, GPT calculates a perplexity score for each. The sentence with the lowest perplexity—representing the most natural English—is selected as the final output.

The three-stage BERT-Defense pipeline: from corrupted input to restored sentence.

Experimental Setup

The model was evaluated using the Zéroe Benchmark, which includes 10 cognitively inspired character-level attackers. Test cases were generated from 400 sentences sampled from the GLUE STS-B dataset. To establish a performance ceiling, a human baseline was created using 27 crowd-workers from Amazon Mechanical Turk (AMT), who were provided with "3-shot" training to restore the same attacked sentences.

Results

The performance of BERT-Defense was measured using Percent Perfectly Restored (PPR), Edit Distance, and MoverScore (semantic similarity). BERT-Defense significantly outperformed baselines such as pyspellchecker and the state-of-the-art ScRNN Defense across nearly all attack vectors.

Performance comparison of BERT-Defense against baselines across various cognitively inspired attacks.

Notably, on visual attacks, BERT-Defense achieved a MoverScore of approximately 0.83, whereas ScRNN scored around 0.25. In several scenarios, such as disemvoweling or natural typos, the domain-specific version of BERT-Defense matched or slightly exceeded human performance. Ablation studies confirmed that while the modified Levenshtein distance is critical for finding candidates, BERT and GPT are essential for reaching human-level fluency.

Discussion and Conclusion

A key insight from this work is that leveraging the massive linguistic knowledge of BERT and GPT allows for superior generalization compared to models trained on specific typo sets. Being "attack-agnostic," BERT-Defense handles novel perturbations without retraining. Furthermore, its ability to address segmentation errors through a modified Wagner-Fischer algorithm provides a significant advantage over standard defenses.

However, the model faces challenges regarding computational latency, taking roughly 8 seconds per sentence compared to 0.1 seconds for ScRNN. It also relies heavily on context; extremely severe attacks or very short sentences can lead to "hallucinations" where the output is grammatically correct but semantically inaccurate. Future work may involve optimizing the pipeline for real-time applications and adapting it for other noisy text tasks like OCR correction or social media text normalization.

Summary updated

BibTeX
@inproceedings{keller2021bert,
  author = {Yannik Keller and Jan Mackensen and Steffen Eger},
  title = {{BERT-Defense: A Probabilistic Model Based on BERT to Combat Cognitively Inspired Orthographic Adversarial Attacks}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2021},
  url = {https://openreview.net/forum?id=ZqtRbsPvin},
}
Figure