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 for Natural Language Processing (NLP) have achieved remarkable success, yet they remain surprisingly brittle. While they can achieve human-level performance on standard benchmarks, they are often easily fooled by "adversarial examples"—sentences where a few words are replaced with synonyms. To a human, the meaning is unchanged; to the model, the classification can flip entirely (e.g., from "Positive" to "Negative").
Existing defenses generally fall into two categories: Adversarial Training (AT), which is computationally expensive and hard to generalize, and Certified Robustness (e.g., Interval Bound Propagation), which is mathematically complex and difficult to scale to large architectures like BERT. This paper introduces the Synonym Encoding Method (SEM), a scalable preprocessing defense that clusters synonyms together and maps them to a single unique "code" before they reach the neural network.
The Synonym Encoding Method (SEM)
The core innovation of SEM is the insertion of a "Synonym Encoder" before the input layer of the target model. By collapsing the "synonym space" into a single point, the attacker loses their primary weapon: if "happy," "cheerful," and "joyful" all look like the same token to the model, swapping one for the other has zero effect on the model's internal state.
Conceptual Comparison of Defenses
Comparison of defense strategies: (a) standard training, (b) adversarial training, (c) certified robustness, and (d) the proposed Synonym Encoding Method (SEM). Source, p. 4
As illustrated in the figure above, SEM (d) differs fundamentally from other approaches. While standard training (a) leaves the model vulnerable to unseen synonym data ($x'$), and adversarial training (b) requires explicit training on those perturbations, SEM maps all unseen synonym data directly to the labeled representative ($x$). This is distinct from certified robustness (c), which attempts to share labels across a continuous region.
Implementation
- Clustering: The method uses word embeddings (specifically GloVe vectors counter-fitted to ensure synonyms are close and antonyms are distant) to group words.
- Encoding: Using a greedy algorithm based on word frequency, the system iterates through the dictionary. For each word, it finds synonyms within a specific distance ($\delta$) and maps the entire cluster to a single "code"—typically the most frequent word in that group.
- Training & Inference: During both phases, any word in a synonym cluster is replaced by its cluster code. This ensures the model is trained on the most natural-sounding representative of a meaning, maintaining accuracy on benign data.
Experimental Results
The authors evaluated SEM on three benchmark datasets: IMDB (sentiment analysis), AG’s News (topic classification), and Yahoo! Answers (topic classification), across multiple architectures including CNNs, LSTMs, and BERT.
- Robustness Gains: On the IMDB dataset using a Bi-LSTM, a standard model's accuracy dropped from 88.2% to 1.8% under a PWWS (synonym) attack. With SEM, the model maintained 76.1% accuracy under the same attack.
- BERT Defense: SEM proved highly effective for BERT, maintaining ~89% accuracy under attacks on IMDB, whereas the undefended BERT dropped to 40.7%.
- Transferability: Adversarial examples generated to fool one model were significantly less likely to fool a different model if the latter employed SEM.
Conclusion
SEM offers a simple yet powerful defense against synonym-substitution attacks. By exploiting the discrete nature of text and collapsing synonym clusters into single representatives, it provides a "plug-and-play" solution that scales to massive models like BERT without requiring architectural changes or additional training data. While it may result in a minor loss of linguistic nuance, the trade-off for security and robustness is substantial.
Summary updated