Abstract
Deep Neural Networks (DNNs) in Natural Language Processing (NLP) are highly susceptible to adversarial attacks, where minor word substitutions can lead to incorrect classifications. While existing defenses focus on model retraining or simple detection, they often fail to maintain system utility under attack. This paper presents TextShield, a novel "detect-and-correct" framework that utilizes saliency information to identify and rectify adversarial perturbations. By leveraging an ensemble of saliency maps and a synonym-based purification process, TextShield restores model accuracy to near-benign levels across multiple benchmarks.
1. Introduction
The vulnerability of NLP models to adversarial examples poses a significant risk in safety-critical applications, such as content filtering and sentiment analysis. Word-level adversarial attacks, which swap original words for synonyms to flip model predictions, are particularly effective because they preserve human readability while exploiting the model's decision boundaries.
Existing defense strategies generally fall into two categories: model enhancement (e.g., adversarial training) and detection. Model enhancement is often computationally expensive and specific to certain attack types, while detection-only methods leave the system incapable of processing the flagged input. TextShield addresses this gap by introducing a correction mechanism that allows the model to "see through" the attack and provide the correct label.
2. Methodology
TextShield operates as a modular defense layer placed in front of a standard classifier. It consists of two primary components: a Saliency-Based Detector and a Saliency-Based Corrector.
2.1 Saliency-Based Detector
The core insight of TextShield is that adversarial perturbations leave a distinct "saliency footprint." Because attacks are designed to maximize the change in model output, the perturbed words often exhibit unnaturally high gradient-based importance (saliency) compared to words in natural text.
To capture this, the authors propose Adaptive Word Importance (AWI), which utilizes an ensemble of four saliency calculation methods:
- Vanilla Gradient (VG)
- Guided Backpropagation (GBP)
- Layerwise Relevance Propagation (LRP)
- Integrated Gradient (IG)
These four saliency maps are fed into a Long Short-Term Memory (LSTM) network followed by a linear layer, which classifies the input sentence as either benign or adversarial.
2.2 Saliency-Based Corrector
Once a sentence is flagged as adversarial, the corrector attempts to "purify" it. It identifies "suspect" words where the saliency exceeds a predefined threshold ($\beta$). These words are then replaced with their most frequent synonyms using the NLTK library. This process shifts the sentence back toward the natural language distribution that the model was originally trained on, effectively neutralizing the adversarial perturbation.
3. Experimental Evaluation
TextShield was evaluated using three major text classification benchmarks: IMDB (sentiment analysis), AG’s News (topic classification), and Yahoo! Answers (category classification). The researchers tested the defense against four prominent word-level attacks: TextFooler (TL), PWWS, Genetic Algorithm (GA), and IGA.
3.1 Robustness and Accuracy Recovery
The results demonstrate that TextShield significantly restores model performance. For instance, on the AGNews dataset, a standard BERT model's accuracy plummeted from 94.6% to 45.5% under an IGA attack. With TextShield enabled, the accuracy was restored to 90.2%.
3.2 Comparison with State-of-the-Art
TextShield consistently outperformed or matched existing defenses like ASCC and IBP. Notably, while defenses like IBP are difficult to apply to complex architectures like BERT, TextShield remains model-agnostic. As a standalone detector, TextShield's saliency-based approach achieved higher F1-scores than frequency-based (FGWS) and logit-based (WDR) detectors across nearly all tested attack scenarios.
4. Discussion and Limitations
While TextShield represents a significant advancement in adversarial defense, several limitations remain:
- Attack Specificity: The system is optimized for word-level attacks. While it shows some efficacy against character-level and sentence-level perturbations, its performance is lower in those domains.
- Synonym Dependency: The corrector's effectiveness is tied to the quality of the NLTK synonym dictionary. Obscure words or neologisms used in attacks may not be easily corrected.
- Computational Overhead: The defense adds approximately 60ms of latency per inference on high-end GPUs. While acceptable for many applications, it may be a constraint for real-time, high-throughput systems.
5. Conclusion
TextShield shifts the paradigm of adversarial defense from mere detection to active correction. By utilizing the inherent saliency footprints of adversarial attacks, it provides a robust, model-agnostic shield that preserves the utility of NLP classifiers even under heavy attack. Future work will focus on developing generalized correction schemes capable of handling multi-level perturbations simultaneously.
Summary updated