On Adversarial Examples for Text Classification by Perturbing Latent Representations

Pablo Rivas, Korn Sooksatra, Bikram Khanal, Pablo Rivas 0001arXiv 2024 · DOI · arXiv

Abstract

This research introduces a novel framework for generating adversarial text examples by operating within a continuous latent space. Traditional adversarial attacks in Natural Language Processing (NLP) often rely on discrete word-swapping, which can lead to unnatural or ungrammatical results. By mapping text into a continuous embedding space using an encoder-decoder architecture, the authors enable the use of powerful white-box gradient-based attacks, such as the Fast Gradient Sign Method (FGSM). Evaluated on the AG News corpus, the framework demonstrates the ability to produce fluent adversarial sentences that effectively deceive state-of-the-art classifiers while maintaining semantic proximity to the original input.

Introduction

In Computer Vision (CV), adversarial attacks are well-established. Because images consist of continuous pixel values, researchers can utilize a model's internal gradients—a "mathematical compass"—to find the minimal perturbations needed to induce misclassification. This is known as a white-box attack.

In contrast, NLP presents a significant challenge because text is discrete. One cannot incrementally change a word like "dog" to make it slightly more like "cat" in the same way one can adjust a pixel. Consequently, most NLP attacks are black-box, involving heuristic word or character swaps that often result in gibberish. This paper seeks to bridge the gap between CV and NLP by treating text as a continuous representation in a latent space, allowing for more efficient and effective white-box attacks.

Methodology

The proposed framework shifts the "battlefield" of adversarial generation from discrete tokens to a continuous latent manifold. The architecture consists of three primary components: an Encoder ($E$), a Decoder ($D$), and an Adversary.

The five-step framework for generating adversarial text: 1) Encoding input into latent space $z$, 2) Reconstructing input $x'$, 3) Classifying $x'$, 4) Producing a perturbed latent space $z^*$ via an adversary, and 5) Constructing the final adversarial example $x^*$.

The Attack Pipeline

  1. Latent Mapping: The Encoder transforms the input text $x$ into a continuous embedding vector $z$.
  2. Joint Training: To ensure the latent space is semantically meaningful, the encoder and decoder are trained jointly. The loss function balances reconstruction accuracy with the embedding's ability to represent the target class.
  3. Gradient-Based Perturbation: With a continuous vector $z$, the system applies the Fast Gradient Sign Method (FGSM). This white-box technique uses the classifier's gradients to calculate the optimal "nudge" required to flip the model's prediction, resulting in a perturbed vector $z^*$.
  4. Reconstruction: The Decoder maps $z^*$ back into the discrete text space to produce the adversarial example $x^*$.

Experimental Evaluation

The framework was tested using the AG News corpus, a standard benchmark for text classification containing news titles and descriptions across four categories: World (W), Sports (S), Business (B), and Sci/Tech (S/T). The dataset includes 30,000 training samples per class. The target classifier utilized a pre-trained BERT preprocessor and an LSTM-based architecture, achieving a 90% validation accuracy on clean data.

Results and Analysis

The adversarial framework successfully induced misclassifications while maintaining text fluency. A key finding was the asymmetry in class vulnerability. As shown in the success rate table below, some categories are significantly easier to attack than others.

Success rates of adversarial attacks between different news categories. Rows represent the original (clean) class, and columns represent the target adversarial (Adv) class.

Key Findings

  • Vulnerability vs. Robustness: Sci/Tech news proved to be the most vulnerable category, with an 18.24% success rate when attempting to flip it to Business. Conversely, World news was the most robust, showing the lowest overall success rates when targeted for perturbation.
  • Class-Specific Resistance: It was exceptionally difficult to flip other classes into the "Sports" category (success rates often below 1%). This suggests the classifier relies on highly distinct features for sports that are difficult to mimic through latent space manipulation.
  • Qualitative Stealth: Reconstructed adversarial examples often appeared as natural summaries or slight rephrasings of the original text, making them stealthier than traditional word-substitution attacks.

Conclusion

By moving the generation of adversarial examples into a continuous latent space, this research proves that high-powered white-box attacks can be effectively ported to the domain of natural language. While the approach is computationally intensive due to the need for specialized encoder-decoder training, it offers a more principled and effective way to evaluate the robustness of NLP models. Future work will focus on optimizing the perturbation bound ($\epsilon$) to better balance attack success with semantic preservation across diverse linguistic domains.

Summary updated

Abstract

Recently, with the advancement of deep learning, several applications in text classification have advanced significantly. However, this improvement comes with a cost because deep learning is vulnerable to adversarial examples. This weakness indicates that deep learning is not very robust. Fortunately, the input of a text classifier is discrete. Hence, it can prevent the classifier from state-of-the-art attacks. Nonetheless, previous works have generated black-box attacks that successfully manipulate the discrete values of the input to find adversarial examples. Therefore, instead of changing the discrete values, we transform the input into its embedding vector containing real values to perform the state-of-the-art white-box attacks. Then, we convert the perturbed embedding vector back into a text and name it an adversarial example. In summary, we create a framework that measures the robustness of a text classifier by using the gradients of the classifier.
BibTeX
@misc{rivas2024adversarial,
  author = {Pablo Rivas and Korn Sooksatra and Bikram Khanal and Pablo Rivas},
  title = {{On Adversarial Examples for Text Classification by Perturbing Latent Representations}},
  year = {2024},
  eprint = {2405.03789},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.03789},
}
Figure