Block-Sparse Adversarial Attack to Fool Transformer-Based Text Classifiers

Sahar Sadrizadeh, Ljiljana Dolamic, Pascal Frossard

Adversarial attacks—small, intentional perturbations designed to trick machine learning models—have long been a headache for computer vision researchers. However, translating these attacks to Natural Language Processing (NLP) is notoriously difficult because text is discrete; you can’t simply add 0.01 to a "word" the way you can to a pixel.

In their paper, "Block-Sparse Adversarial Attack to Fool Transformer-Based Text Classifiers," researchers from EPFL and Armasuisse S+T present a new gradient-based white-box attack that successfully topples state-of-the-art Transformer models by changing only a handful of words.

TL;DR

The authors introduce a gradient-based attack that uses a block-sparsity constraint to find the minimum number of word substitutions needed to fool a classifier. By projecting continuous gradients back into discrete word embeddings, they reduced GPT-2’s accuracy from over 90% to less than 5% while maintaining high semantic similarity to the original text.

What Problem Does This Paper Address?

Most adversarial attacks in NLP fall into two camps: black-box (which use heuristics like synonym swapping and don't need model internals) and white-box (which use the model's gradients). White-box attacks are generally more powerful but struggle with two things:

  1. The Discreteness Gap: Gradients tell you how to change an input in continuous space, but text consists of discrete tokens.
  2. Imperceptibility: In images, "imperceptible" means low $L_p$ noise. In text, it means the sentence must still make sense and mean roughly the same thing to a human, which usually requires changing as few words as possible.

Existing white-box attacks often perturb too many words, making the attack obvious, or they use overly complex, over-parameterized distributions that are hard to optimize.

Methods

The authors propose an Iterative Gradient Projection method. The core "secret sauce" is the optimization objective:

$$\min_{e_{x'} \in \mathcal{E}_V} \mathcal{L}_{Adv} + \alpha \mathcal{L}_{BSparse}$$

  • $\mathcal{L}_{Adv}$: The adversarial loss (cross-entropy), which pushes the model toward the wrong prediction.
  • $\mathcal{L}_{BSparse}$: A block-sparsity constraint (specifically an $\ell_{2,1}$ norm). This treats the perturbation of each word as a "block." By minimizing this, the algorithm is forced to keep the perturbation vector for most words at zero, effectively selecting only the most "influential" words to change.
  • $\alpha$: A hyperparameter that balances attack success vs. how many words are changed.

The Algorithm Loop: 1. Gradient Step: Calculate the gradient in the continuous embedding space to see how to best "fool" the model. 2. Projection Step: Since the gradient move results in a point that doesn't correspond to a real word, the algorithm projects that point back to the nearest actual word embedding in the vocabulary using cosine similarity. 3. Refinement: If the attack fails, they decrease $\alpha$ (making the attack more aggressive) and try again. They also use a "buffer" to keep track of generated sentences and avoid getting stuck in infinite loops.

Datasets

The attack was evaluated against a fine-tuned GPT-2 model on three standard NLP benchmarks:

  • AG News: News article topic classification (4 classes).
  • MNLI (Multi-Genre Natural Language Inference): Determining if a premise entails or contradicts a hypothesis.
  • Yelp Reviews: Sentiment analysis (Positive/Negative).

Key Insights and Ideas

  • Sparsity is the Key to "Stealth": By using block-sparsity, the attack naturally identifies the "weak points" in a sentence. Instead of adding a little bit of noise to every word, it makes a decisive change to one or two words that carry the most weight for the classifier.
  • The Continuous-to-Discrete Bridge: The paper demonstrates that even though text is discrete, the embedding space is "smooth" enough that gradient descent can effectively guide us toward adversarial synonyms.
  • Semantic Preservation: By using cosine similarity during the projection step, the algorithm tends to pick replacement words that are semantically related to the original, helping the adversarial sentence remain coherent to human readers.

Key Results

The results were devastating for the target models. Across all datasets, the attack was able to drop the accuracy of a fine-tuned GPT-2 model to near zero:

Dataset Clean Accuracy Accuracy After Attack Semantic Similarity
AG News 94.8% 0.4% 0.87
MNLI 81.7% 3.0% 0.85
Yelp Reviews 97.8% 1.8% 0.87

The attack outperformed the previous state-of-the-art white-box attack (GBDA) in almost every metric, achieving higher success rates with fewer changes. For example, on AG News, GBDA only brought accuracy down to 6.6%, while this block-sparse method reached 0.4%.

Limitations and Future Work

  • White-Box Requirement: The attack requires full access to the model's gradients and parameters. In many real-world scenarios (like attacking a proprietary API), this information isn't available.
  • Computational Cost: The iterative nature of the projection and the need to search the vocabulary for the nearest neighbor can be computationally expensive for very large vocabularies.
  • Defense Research: The authors note that while this attack is powerful, it provides a new benchmark for researchers to develop more robust Transformer architectures that can withstand such targeted perturbations. Future work could involve using this attack during adversarial training to "harden" models.

Summary updated

BibTeX
@inproceedings{sadrizadeh2022block,
  author = {Sahar Sadrizadeh and Ljiljana Dolamic and Pascal Frossard},
  title = {{Block-Sparse Adversarial Attack to Fool Transformer-Based Text Classifiers}},
  booktitle = {IEEE International Conference on Acoustics, Speech and Signal Processing},
  year = {2022},
  url = {https://openreview.net/forum?id=4TdLco9mZl},
}

Loading related content...

Figure