The Best Defense is Attack: Repairing Semantics in Textual Adversarial Examples
Heng Yang, Ke Li
TL;DR
This work targets textual adversarial robustness by identifying the semantic drift caused by adversaries and proposes Rapid, a two-phase defense that couples an in-model adversarial detector with a reactive perturbation-based repair mechanism. Phase 1 jointly trains a victim classifier and a detector using multi-attack adversaries to enable targeted defense, formalized by a composite loss that balances classification, detection, and adversarial training. Phase 2 applies detection, then repairs adversaries through perturbation defocusing and pseudo-semantic similarity filtering to preserve natural semantics while minimizing edits. Experiments on SST2, Amazon, AGNews, and Yahoo! demonstrate high detection accuracy, substantial repair rates (up to 99%), and notable efficiency gains over prior methods, with robustness to unseen attacks and different victim-model backbones like BERT and DeBERTa. The work contributes a reproducible benchmarking platform and highlights the practical potential of semantics-focused defenses in real-world NLP tasks.
Abstract
Recent studies have revealed the vulnerability of pre-trained language models to adversarial attacks. Existing adversarial defense techniques attempt to reconstruct adversarial examples within feature or text spaces. However, these methods struggle to effectively repair the semantics in adversarial examples, resulting in unsatisfactory performance and limiting their practical utility. To repair the semantics in adversarial examples, we introduce a novel approach named Reactive Perturbation Defocusing (Rapid). Rapid employs an adversarial detector to identify fake labels of adversarial examples and leverage adversarial attackers to repair the semantics in adversarial examples. Our extensive experimental results conducted on four public datasets, convincingly demonstrate the effectiveness of Rapid in various adversarial attack scenarios. To address the problem of defense performance validation in previous works, we provide a demonstration of adversarial detection and repair based on our work, which can be easily evaluated at https://tinyurl.com/22ercuf8.
