Detecting Textual Adversarial Examples Based on Distributional Characteristics of Data Representations

Na Liu, Mark Dras, Wei Emma Zhang

Abstract

Deep neural networks in Natural Language Processing (NLP) are vulnerable to adversarial examples—inputs with subtle perturbations that trigger incorrect predictions. This paper introduces two "reactive" defense methods, Adapted Local Intrinsic Dimensionality (LID) and MultiDistance Representation Ensemble (MDRE), which detect these attacks by analyzing the distributional characteristics of data representations. Our results demonstrate state-of-the-art detection accuracy across character, word, and phrase-level attacks on major NLP benchmarks.

Introduction

The "brittleness" of deep learning models is a significant security concern. In NLP, adversarial attacks can take many forms, from character-level typos to word-level synonym substitutions and phrase-level rephrasing. While "proactive" defenses like adversarial training exist, they are often computationally expensive and struggle to generalize to unseen attack types. This research proposes a "reactive" approach: a security layer that identifies and flags adversarial inputs before they are processed by the target model.

The Nature of Textual Adversarial Attacks

Adversarial examples are designed to be imperceptible to humans but disruptive to models. Figure 1 provides examples of how these perturbations manifest across different levels of granularity, often flipping a model's prediction from positive to negative sentiment with minimal changes.

Examples of adversarial attacks at character, word, phrase, and sentence levels.

The core insight of this work is that while an adversarial sentence may appear linguistically sound, its mathematical representation within a transformer model often resides "off the manifold" of natural language data.

Methodology

The authors propose two distinct detection frameworks based on representation space analysis:

1. Adapted Local Intrinsic Dimensionality (LID)

LID measures the "clutter" or expansion rate of a data point's neighborhood in high-dimensional space. The authors adapt this for NLP by extracting hidden states from the [CLS] token across all 12 layers of a BERT model. These states form a 12-dimensional feature vector used by a classifier to distinguish clean text from adversarial perturbations.

2. MultiDistance Representation Ensemble (MDRE)

MDRE treats adversarial examples as out-of-distribution (OOD) samples. For any input, it calculates the Euclidean distance to its nearest neighbor in the training set with the same predicted label. To ensure robustness, MDRE ensembles these distances across four diverse architectures: BERT, RoBERTa, XLNet, and BART. This multi-model perspective makes it difficult for an attacker to find a universal blind spot.

Experimental Evaluation

The proposed methods were tested on two standard benchmarks: IMDB (sentiment analysis) and MultiNLI (natural language inference). The evaluation covered three primary attack vectors: character-level (typos), word-level (synonym swaps), and phrase-level (back-translation).

Results and Discussion

The proposed methods significantly outperformed existing baselines, such as DISP (a perturbation discriminator) and FGWS (a frequency-guided substitution check).

Detection accuracy of proposed methods (LID, MDRE) compared to baselines across different attack types.

As shown in Figure 6, MDRE achieved a remarkable 95.1% detection accuracy for phrase-level attacks on the IMDB dataset. The adapted LID method also proved highly effective, particularly for word-level attacks where it reached 84.1% accuracy, surpassing both FGWS and DISP.

While performance was slightly lower on the MultiNLI dataset—likely due to the "mismatched" nature of its test sets—the proposed methods maintained a clear lead over traditional rule-based detectors. This suggests that analyzing learned representations is a more robust path forward than relying on vocabulary-based heuristics like word frequency.

Conclusion

This work demonstrates that the distributional characteristics of data representations provide a powerful signal for detecting textual adversarial examples. By moving beyond simple rule-based checks to sophisticated geometric analysis of the representation space, we can significantly harden NLP models against a wide array of adversarial threats. Future research will investigate the resilience of these detectors against optimization-based attacks specifically designed to evade detection.

Summary updated

BibTeX
@inproceedings{liu2022detecting,
  author = {Na Liu and Mark Dras and Wei Emma Zhang},
  title = {{Detecting Textual Adversarial Examples Based on Distributional Characteristics of Data Representations}},
  booktitle = {RepL4NLP @ ACL},
  year = {2022},
  doi = {10.18653/v1/2022.repl4nlp-1.9},
}
Figure