A Prompting-based Approach for Adversarial Example Generation and Robustness Enhancement

Yuting Yang, Pei Huang, Juan Cao, Jintao Li, Yun Lin, Feifei Ma, Jian ZhangarXiv 2022 · DOI

Abstract

Deep learning models for Natural Language Processing (NLP) are often vulnerable to adversarial attacks—small, intentional perturbations that cause incorrect predictions. Traditional attack methods frequently produce ungrammatical or nonsensical text and require extensive queries to the target model. This paper presents PAT (Prompt-based Adversarial Attack), a framework that leverages the inherent linguistic knowledge of Pre-trained Language Models (PLMs) to generate highly natural adversarial examples. By using malicious "trigger" prompts, PAT steers PLMs to generate contextually appropriate but deceptive text. Furthermore, we propose a robustness enhancement method that aligns the embeddings of original and adversarial inputs, providing an efficient defense mechanism. Experimental results across three benchmarks demonstrate that PAT achieves superior fluency and significant attack success while effectively hardening models against such threats.

Introduction

The brittleness of NLP models poses significant security and reliability risks. While adversarial example generation is a common technique to probe these vulnerabilities, existing search-based methods often result in "word salad"—sentences that are grammatically incorrect or semantically disjointed. These unnatural examples are easily detected by simple filters or human inspection. Moreover, the high query complexity of these methods makes them impractical for black-box scenarios.

To address these challenges, we introduce PAT, which shifts the paradigm from discrete word-swapping to continuous prompt-based generation. By "whispering" malicious intents to a PLM, we can generate adversarial text that maintains high fluency and semantic coherence.

Methodology

The PAT framework operates in three primary stages: Prompt Construction, Mask-and-Filling, and Learning to Attack.

The PAT framework for adversarial example generation and learning.

Adversarial Example Generation

Instead of exhaustive searching, we construct a prompt template by masking specific tokens in an input sentence and appending a "trigger" phrase. This trigger carries the target malicious intent (e.g., using "It is a bad movie" to influence the generation of a positive sentiment review). The PLM then fills these masks, producing a sentence that is contextually relevant but biased toward the trigger's sentiment.

Learning to Attack

To further refine the attack, we treat the PLM as a "talented hacker." By fine-tuning the model on a small set (typically fewer than 500) of known successful adversarial examples, the PLM learns to generate more effective perturbations. This "PAT*" version significantly improves attack success rates by capturing the specific vulnerabilities of the target domain.

Robustness Enhancement

For defense, we propose an implicit adversarial training method. Rather than the slow process of generating and retraining on millions of adversarial sentences, we fine-tune the model's embedding space. We minimize the distance between the representations of a clean sentence and its maliciously prompted counterpart. This ensures the model's internal logic remains robust to the semantic shifts introduced by adversarial prompts.

Experimental Results

We evaluated PAT on the MR, IMDB, and SNLI datasets against BiLSTM and BERT classifiers. We compared our approach to TextFooler, a state-of-the-art search-based attack.

Performance comparison between TextFooler and PAT across multiple datasets.

Superior Fluency and Naturalness

A key finding is the significant improvement in fluency. As shown in the results table, PAT consistently achieves much lower Perplexity (PPL) scores than TextFooler. For instance, on the MR dataset with a BiLSTM model, TextFooler's PPL is 694.67, whereas PAT's sentence-level attack achieves a PPL of 90.51. This indicates that PAT-generated examples are far more human-like and harder to detect.

Attack Success and Transferability

While TextFooler sometimes achieves higher raw success rates due to its brute-force nature, PAT* (the learned version) shows competitive performance, often improving success rates by over 10% compared to the base PAT. Furthermore, attacks generated via PAT show high transferability across different model architectures, suggesting they target fundamental linguistic flaws in PLMs.

Defense Gains

Our robustness enhancement method improved model accuracy under attack by approximately 10% compared to traditional adversarial training. Crucially, this was achieved with less than a 2% drop in "clean" accuracy, demonstrating that models can be hardened without sacrificing general performance.

Conclusion

PAT demonstrates that the prompting paradigm is a powerful tool for both attacking and defending NLP models. By leveraging the internal knowledge of PLMs, we can generate adversarial examples that are not only effective but also remarkably natural. The proposed embedding-alignment defense offers a computationally efficient path toward building more robust language technologies. Future work will explore the application of these techniques to other safety domains, such as preventing privacy leaks in large-scale models.

Summary updated

BibTeX
@misc{yang2022prompting,
  author = {Yuting Yang and Pei Huang and Juan Cao and Jintao Li and Yun Lin and Jin Song Dong and Feifei Ma and Jian Zhang},
  title = {{A Prompting-based Approach for Adversarial Example Generation and Robustness Enhancement}},
  year = {2022},
  eprint = {2203.10714},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2203.10714},
}
Figure