Universal Adversarial Perturbation for Text Classification

Hang Gao, Tim OatesarXiv 2019

Abstract

Given a state-of-the-art deep neural network text classifier, we show the existence of a universal and very small perturbation vector (in the embedding space) that causes natural text to be misclassified with high probability. Unlike images on which a single fixed-size adversarial perturbation can be found, text is of variable length, so we define the "universality" as "token-agnostic", where a single perturbation is applied to each token, resulting in different perturbations of flexible sizes at the sequence level. We propose an algorithm to compute universal adversarial perturbations, and show that the state-of-the-art deep neural networks are highly vulnerable to them, even though they keep the neighborhood of tokens mostly preserved. We also show how to use these adversarial perturbations to generate adversarial text samples. The surprising existence of universal "token-agnostic" adversarial perturbations may reveal important properties of a text classifier.

Introduction: The Challenge of Universal Text Attacks

Adversarial attacks—inputs specifically designed to deceive AI models—represent a significant security risk in machine learning. In computer vision, "universal" attacks are well-documented: a single static noise pattern can be added to any image to trigger misclassification. However, extending this concept to natural language processing (NLP) is difficult because text sequences vary in length, unlike fixed-dimension images.

Most existing text attacks are sample-dependent, requiring a unique modification for every sentence. Previous attempts at universal text attacks often involved prepending nonsensical "trigger" sequences (e.g., "zoning tapping fends") to inputs. While effective, these are easily detectable by humans and disrupt the natural flow of the text. This research introduces a token-agnostic universal perturbation: a single, tiny vector in the high-dimensional embedding space that can flip model predictions regardless of the input word or sequence length.

Methodology: Token-Agnostic Perturbations

The core innovation is targeting the embedding layer rather than the discrete text itself. The authors define a single perturbation vector $\delta$ that is added to the embedding of every token in an input sequence. This approach ensures universality across both different samples and individual tokens.

To find the optimal $\delta$, the researchers developed a gradient-based optimization algorithm. By iteratively processing training data, the algorithm calculates the gradient of the loss function with respect to the perturbation and updates $\delta$ to maximize classification error. To ensure the attack remains "imperceptible," the size of $\delta$ is constrained within an $\ell_p$ ball of radius $\epsilon$.

To quantify "imperceptibility" in a mathematical sense, the authors introduced the Neighborhood Intersection (NI) metric. NI measures whether a word, after being shifted by $\delta$, still retains its original nearest neighbors in the vocabulary. High NI values indicate that the word's semantic "meaning" is mathematically preserved.

Experimental Setup

The attack was evaluated against the BERT-base model on four benchmarks from the GLUE suite:

  • MRPC: Paraphrase detection (News domain).
  • CoLA: Linguistic acceptability (Grammar).
  • SST-2: Sentiment analysis (Movie reviews).
  • QNLI: Question-answering/Natural Language Inference (Wikipedia).

Results and Discussion

The experiments revealed that state-of-the-art text classifiers are remarkably vulnerable to these global directions in embedding space.

Quantitative Impact

As the perturbation size ($\epsilon$) increases, model performance drops precipitously across all tasks. For instance, on the QNLI task, accuracy fell from 91.8% to 49.9% with an $\epsilon$ of only 0.2.

Performance of the proposed universal attack across different tasks and perturbation sizes.

The researchers also found that the attack is highly data-efficient; using just 10% of the training data was sufficient to generate a potent universal perturbation. Surprisingly, this single universal vector often outperformed baselines that use unique perturbations for every word in the vocabulary, suggesting it exploits a fundamental geometric weakness in the model's decision boundaries.

Qualitative Examples

By mapping the perturbed embeddings back to their nearest neighbors in the vocabulary, the authors generated adversarial text samples. These samples often look very similar to the original text but successfully flip the model's prediction.

Examples of original vs. adversarial text and the resulting model predictions.

As shown in the examples, subtle changes like replacing "the" with "a" or changing a conjunction to a comma can lead to misclassification, even when the semantic meaning remains largely clear to a human reader.

Conclusion and Future Work

The existence of universal, token-agnostic perturbations suggests that deep learning text classifiers rely on specific, globally vulnerable geometric properties of the embedding space. While the resulting text is not always perfectly fluent, the mathematical preservation of word neighborhoods is striking. Future research could explore "black-box" scenarios where the attacker does not have access to model gradients, or investigate adversarial training as a defense mechanism.

Summary updated

BibTeX
@misc{gao2019universal,
  author = {Hang Gao and Tim Oates},
  title = {{Universal Adversarial Perturbation for Text Classification}},
  year = {2019},
  eprint = {1910.04618},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1910.04618},
}
Figure