What Learned Representations and Influence Functions Can Tell Us About Adversarial Examples

Shakila Mahjabin Tonni, Mark Dras

Abstract

Adversarial examples—inputs specifically crafted to deceive machine learning models—present a significant challenge to the reliability of Natural Language Processing (NLP) systems. While many defenses focus on proactive training or input-level preprocessing, this study explores reactive detection by analyzing the internal learned representations of models. We adapt two high-performing techniques from the computer vision domain: Nearest Neighbor Influence Functions (NNIF) and Mahalanobis distance-based detectors. Our findings indicate that NNIF provides a powerful mechanism for identifying adversarial text, achieving up to 100% detection accuracy on character-level attacks. By leveraging the discrepancy between a model's spatial neighbors and its influential training points, we demonstrate that adversarial examples leave a detectable "internal fingerprint" distinct from benign data.

1. Introduction

Deep Learning models, despite their state-of-the-art performance across various NLP tasks, remain notoriously fragile. Small, often human-imperceptible changes to an input—such as a single character swap or the substitution of a word with its synonym—can lead a model to produce highly confident but incorrect predictions. These adversarial examples expose the misalignment between human perception and the decision boundaries of neural networks.

In the NLP community, defenses have traditionally been categorized as "proactive," such as adversarial training, or "input-based," such as synonym-restoration filters. However, proactive methods are computationally expensive, and input-based methods often fail against sophisticated attacks. This paper investigates a third category: "reactive" defenses. These methods attempt to detect attacks during inference by examining the model's internal "learned representations," seeking to determine if adversarial inputs inhabit a fundamentally different subspace than natural language.

2. Methodology

The researchers adapted two primary detection frameworks originally developed for image classification to the BERT-base architecture.

2.1 Nearest Neighbor Influence Functions (NNIF)

NNIF represents the core contribution of the study. It utilizes Influence Functions, a statistical technique that quantifies the impact of a specific training data point on the model’s parameters. The underlying hypothesis is that for a "normal" input, the training examples that most influenced its prediction should also be its "nearest neighbors" in the model's internal vector space.

In an adversarial scenario, this relationship breaks down. An adversarial example is often a point in space that has been "pushed" across a decision boundary. While its spatial neighbors might belong to the target (incorrect) class, the training points that actually influenced the model's logic for that region may belong to the original (correct) class. When these two sets—influential points and nearest neighbors—diverge, the input is flagged as adversarial.

2.2 Mahalanobis (MAHAL) Detector

The Mahalanobis detector treats the internal representations of each class (e.g., "Positive" vs. "Negative" sentiment) as following a Gaussian distribution. It calculates the Mahalanobis distance—a measure of how many standard deviations a point is from the mean of a distribution—to determine if a new input is an outlier. If the representation of a test input is too far from the "center" of its predicted class, it is considered suspicious.

3. Experimental Setup

The study utilized the BERT-base model and evaluated performance on two benchmark datasets:

  • IMDB: A binary sentiment analysis dataset.
  • MultiNLI (Multi-Genre Natural Language Inference): A complex task requiring the model to identify entailment, contradiction, or neutrality between sentence pairs.

Two types of adversarial attacks were employed:

  • CHARATT: Character-level perturbations (e.g., typos, misspellings).
  • WORDATT: Word-level substitutions using context-preserving synonyms.

The detectors were compared against several baselines, including Local Intrinsic Dimensionality (LID), Multi-Distance Ensemble (MDRE), and DISP (a method that attempts to "repair" the input text).

4. Key Results and Insights

The experimental results demonstrate that internal representation analysis is a highly effective tool for NLP security.

4.1 Detection Performance

NNIF emerged as the state-of-the-art (SOTA) method, achieving 100% accuracy in detecting character-level attacks on the IMDB dataset. Even on the more challenging word-level attacks—which are semantically similar to benign text—NNIF maintained approximately 90% accuracy, significantly outperforming existing baselines like RSV.

For the Mahalanobis detector, the authors discovered that "feature ensembling"—analyzing representations across all layers of the BERT model rather than just the final layer—was crucial. On the IMDB dataset, this approach improved detection accuracy from a mediocre 60% to over 91%.

4.2 The "Two-View" Advantage

The success of NNIF suggests that influence functions and nearest neighbors provide complementary perspectives. Nearest neighbors capture spatial proximity in the latent space, while influence functions capture the "learning history" of the model. The disagreement between these two views serves as a robust indicator of adversarial manipulation.

4.3 Domain-Specific Observations

The researchers noted a distinct difference between NLP and computer vision. In vision tasks, using a small number of influential training points (e.g., $M=30$) is typically optimal. However, in NLP, detection accuracy improved as the number of influential points increased (up to $M=500$). This suggests that text representations are more distributed or complex than image representations. Furthermore, MultiNLI proved to be a much harder task for detection than IMDB, likely due to the inherent noise in the adversarial subspace of multi-sentence inference tasks.

5. Limitations and Future Work

Despite its high accuracy, NNIF faces a significant hurdle: computational cost. Calculating influence functions requires computing the Inverse Hessian-Vector Product, a mathematically intensive operation. This complexity currently limits the scalability of the method to real-time applications.

Future research will likely focus on "FastIF" techniques to reduce this overhead. Additionally, while this study focused on BERT-base, further investigation is needed to determine if these adversarial subspaces behave similarly in massive Large Language Models (LLMs) like GPT-4 or Llama-3.

6. Conclusion

This paper demonstrates that adversarial examples in NLP are not just "tricks" of the input text but are reflected in the internal logic of the model. By looking beyond the output and analyzing how a model relates new inputs to its training history, we can build highly effective reactive defenses that safeguard AI systems against subtle and sophisticated attacks.

Summary updated

BibTeX
@inproceedings{tonni2023what,
  author = {Shakila Mahjabin Tonni and Mark Dras},
  title = {{What Learned Representations and Influence Functions Can Tell Us About Adversarial Examples}},
  booktitle = {Ijcnlp (findings)},
  year = {2023},
  doi = {10.18653/v1/2023.findings-ijcnlp.35},
}
Figure