Abstract
Recent studies on adversarial images have shown that they tend to leave the underlying low-dimensional data manifold, making them significantly more challenging for current models to make correct predictions. This so-called off-manifold conjecture has inspired a novel line of defenses against adversarial attacks on images. In this study, we find a similar phenomenon occurs in the contextualized embedding space induced by pretrained language models, in which adversarial texts tend to have their embeddings diverge from the manifold of natural ones. Based on this finding, we propose Textual Manifold-based Defense (TMD), a defense mechanism that projects text embeddings onto an approximated embedding manifold before classification. It reduces the complexity of potential adversarial examples, which ultimately enhances the robustness of the protected model. Through extensive experiments, our method consistently and significantly outperforms previous defenses under various attack settings without trading off clean accuracy. To the best of our knowledge, this is the first NLP defense that leverages the manifold structure against adversarial attacks.
Introduction
Pretrained Language Models (PLMs) like BERT and RoBERTa have become the backbone of modern NLP, yet they remain remarkably fragile. Small, semantic-preserving changes—such as replacing a single word with a synonym—can completely flip a model's prediction. Existing defenses generally fall into two categories: adversarial training, which is computationally expensive, and certified defenses, which often rely on rigid assumptions that fail against adaptive attackers.
This research introduces a third approach based on the off-manifold conjecture. By observing that adversarial perturbations push text representations away from the natural data distribution, the authors developed Textual Manifold-based Defense (TMD) to project these "deviant" inputs back onto a learned manifold of natural language.
The Off-Manifold Phenomenon in NLP
In computer vision, it is well-established that adversarial images look "unnatural" to a model's internal logic because they exit the low-dimensional manifold where real images reside. This study confirms that a similar phenomenon exists in NLP. While random synonym swaps keep embeddings within the natural manifold, adversarial swaps specifically designed to fool a classifier force the embeddings to "jump" off this surface. TMD exploits this gap to identify and neutralize adversarial noise.
Methodology: Textual Manifold-based Defense (TMD)
TMD operates in two primary phases: manifold approximation and on-manifold projection.
1. Manifold Approximation (Training)
To define what "natural" text looks like, the authors use an InfoGAN (Information-maximizing Generative Adversarial Network) to learn the distribution of natural text embeddings. Recognizing that language is naturally categorical (e.g., divided by topics or sentiment), the authors model the manifold as disconnected. By using a discrete latent code and a learnable prior, the GAN captures these separate "islands" of natural language more accurately than standard continuous models.
2. On-manifold Projection (Inference)
When a sentence is input during inference, the model extracts its embedding. Before this embedding reaches the classifier, TMD performs a reconstruction step. The system samples $k$ points from the learned manifold and selects the one closest to the input embedding. This process effectively "washes away" adversarial perturbations by forcing the representation back onto the surface of natural language.
Notably, the authors found that simple sampling is more robust than gradient-based optimization for this projection, as it prevents attackers from manipulating the defense itself through gradient-based techniques.
Experimental Evaluation
The authors evaluated TMD across three standard benchmarks—AGNEWS, IMDB, and YELP—against three potent word-substitution attacks: PWWS, TextFooler, and BERT-Attack.
Key Results
- Significant Robustness Gains: TMD consistently outperformed state-of-the-art defenses like ASCC, DNE, and SAFER. On the AGNEWS dataset using BERT, TMD achieved an accuracy under attack (AUA) of 50-70%, whereas the vanilla model's performance dropped to 28-39%.
- No Clean Accuracy Tax: A common drawback of adversarial defenses is a decrease in accuracy on normal, non-attacked text. TMD maintains clean accuracy levels very close to the original vanilla models.
- Generalization: The defense proved effective across different model architectures (BERT, RoBERTa, XLNet), suggesting it captures fundamental properties of the data manifold rather than specific model artifacts.
Conclusion
TMD is the first NLP defense to successfully leverage the manifold structure of contextualized embeddings to thwart adversarial attacks. By projecting suspicious inputs back to a learned natural distribution, it provides a robust and efficient shield for PLMs. Future research may extend this work by exploring manifold projection at intermediate layers or utilizing more sophisticated generative models to further bridge the gap between clean and adversarial performance.
Summary updated