Abstract
In the area of natural language processing, deep learning models are recently known to be vulnerable to various types of adversarial perturbations, but relatively few works are done on the defense side. Especially, there exists few effective defense method against the successful synonym substitution based attacks that preserve the syntactic structure and semantic information of the original text while fooling the deep learning models. We contribute in this direction and propose a novel adversarial defense method called Synonym Encoding Method (SEM). Specifically, SEM inserts an encoder before the input layer of the target model to map each cluster of synonyms to a unique encoding and trains the model to eliminate possible adversarial perturbations without modifying the network architecture or adding extra data. Extensive experiments demonstrate that SEM can effectively defend the current synonym substitution based attacks and block the transferability of adversarial examples. SEM is also easy and efficient to scale to large models and big datasets.
Introduction
Deep Learning models in Natural Language Processing (NLP) are surprisingly fragile. An attacker can often flip a model's prediction (e.g., changing a "Positive" sentiment to "Negative") simply by replacing a few words with their synonyms. Because these "synonym substitution attacks" preserve the original meaning and grammar, they are nearly impossible for automated spell-checkers or even human readers to detect.
Existing defenses generally fall into two camps: Adversarial Training (AT) and Certified Robustness (e.g., Interval Bound Propagation). AT is computationally expensive and often fails to generalize across the vast space of word substitutions. Certified methods, while rigorous, are complex to implement for large-scale architectures like BERT and often lead to significant degradation in accuracy on clean data.
The Synonym Encoding Method (SEM)
The core innovation of this work is the Synonym Encoding Method (SEM). Instead of training the model to recognize every possible synonym, SEM inserts a specialized encoder before the model’s input layer to map entire clusters of synonyms to a single unique "code."
Clustering and Encoding Algorithm
SEM leverages word embeddings—specifically GloVe vectors counter-fitted to ensure synonyms are proximal while antonyms are distant—to identify groups of words with nearly identical meanings. The process involves:
- Clustering: Iterating through the dictionary by word frequency. For each word, synonyms are identified; if a synonym already belongs to a cluster, the current word joins it.
- Mapping: Every word in a cluster is mapped to a single representative, typically the most frequent word in that group.
- Training: The model is trained on the original dataset, but it only processes these unique codes. This makes the model naturally invariant to synonym-swapping attacks.
By "collapsing" the discrete adversarial neighborhood into a single point, SEM effectively neutralizes perturbations. This structural defense is independent of the specific attack method used, as any synonym within the predefined cluster will be mapped to the same input.
Experimental Evaluation
The authors evaluated SEM across three benchmark datasets: IMDB (sentiment classification), AG’s News (topic classification), and Yahoo! Answers (10-category topic classification). These were tested against four architectures: CNN, LSTM, Bi-LSTM, and BERT.
Key Results
- Robustness: On the IMDB dataset with an LSTM, standard model accuracy dropped from 87.3% to 2.2% under a PWWS attack. With SEM, accuracy remained at 77.3%.
- BERT Compatibility: SEM is highly effective with BERT. On AG’s News, BERT with SEM maintained 88.5% accuracy under attack, significantly outperforming the baseline.
- Efficiency: SEM requires no extra data and does not significantly increase training time. On Yahoo! Answers, SEM-enabled models converged faster due to the simplified input space.
- Benign Accuracy: The "tax" on clean data was minimal, typically within 1-3% of the original model's performance.
Conclusion
SEM provides a lightweight and scalable defense against synonym substitution attacks. By exploiting the discrete nature of NLP input spaces and using frequency-based encoding, it achieves state-of-the-art robustness without the heavy computational costs associated with adversarial training or the accuracy trade-offs of certified defenses. Future work may extend this approach to generative tasks where word choice nuance is more critical.
Summary updated