Understanding, Detecting, and Separating Out-of-Distribution Samples and Adversarial Samples in Text Classification

David Cheng-Han Chiang, Hung-Yi LeearXiv 2022

Abstract

In this paper, we study the differences and commonalities between statistically out-of-distribution (OOD) samples and adversarial (Adv) samples, both of which hurting a text classification model's performance. We conduct analyses to compare the two types of anomalies (OOD and Adv samples) with the in-distribution (ID) ones from three aspects: the input features, the hidden representations in each layer of the model, and the output probability distributions of the classifier. We find that OOD samples expose their aberration starting from the first layer, while the abnormalities of Adv samples do not emerge until the deeper layers of the model. We also illustrate that the models' output probabilities for Adv samples tend to be more unconfident. Based on our observations, we propose a simple method to separate ID, OOD, and Adv samples using the hidden representations and output probabilities of the model. On multiple combinations of ID, OOD datasets, and Adv attacks, our proposed method shows exceptional results on distinguishing ID, OOD, and Adv samples.

Introduction

Deep learning models for text classification are notoriously brittle, failing primarily through out-of-distribution (OOD) samples—data statistically different from the training set—and adversarial (Adv) samples—maliciously crafted inputs designed to fool the model. While current anomaly detection systems often group these into a single "non-in-distribution" category, robust systems should treat them differently. An OOD sample should typically be ignored, whereas an adversarial sample might be recoverable through preprocessing. This paper investigates the fundamental differences between these anomalies and proposes a method to distinguish them by looking into the "black box" of Transformer models.

Methodology

The authors performed a layer-by-layer analysis of BERT and RoBERTa models across several benchmarks, including IMDB, SST-2, AG-News, and Yelp Polarity. They evaluated three key areas:

  1. Input Features: Analyzing vocabulary differences using Bag-of-Words and TF-IDF.
  2. Hidden Representations: Fitting class-conditional Gaussian distributions to hidden states and using log-likelihood to measure deviation from training data.
  3. Output Probabilities: Examining the maximum softmax probability (model confidence) for final predictions.

To generate adversarial samples, four synonym-substitution attacks were employed: Textfooler, PWWS, BAE, and TF-adj.

The "Early vs. Late" Layer Phenomenon

A central finding of this research is the distinct signature left by different anomalies within the model's layers. OOD samples deviate from the training distribution almost immediately, showing high detectability starting from Layer 1. Conversely, adversarial samples remain indistinguishable from clean data through the majority of the model; their abnormality only becomes apparent in the final two layers as the model is forced toward an incorrect prediction.

AUROC for OOD detection (left) and Adv detection (right) across model layers.

This observation led to a Two-Stage Detection Pipeline:

  • Stage 1: Filter OOD samples using hidden representations from an early layer (e.g., Layer 2).
  • Stage 2: Distinguish between clean ID samples and adversarial ones using the maximum output probability for the remaining data.

Adversarial Confidence and Separation

Even when an adversarial attack successfully misleads a model, the model typically exhibits lower confidence in the incorrect label compared to its confidence in correct labels for clean data. This difference in confidence scores allows for effective separation in the second stage of the pipeline.

Distribution of output scores for ID, Adv, and OOD samples, showing the lower confidence associated with adversarial and OOD inputs.

Key Results

  • OOD Detection: Using Layer 2 hidden features, the method achieved near-perfect separation (AUROC 0.95–1.00) between ID and OOD samples.
  • Adversarial Detection: Output confidence enabled high-accuracy detection (AUROC > 0.90), although the "BAE" attack was notably more difficult to identify.
  • Three-Way Separation: The two-stage pipeline successfully categorized inputs into ID, OOD, or Adv with significantly higher accuracy than unified detection methods.

Conclusion

This study demonstrates that OOD and adversarial samples, while both detrimental to performance, manifest at different stages of neural processing. By leveraging early-layer representations for OOD detection and output confidence for adversarial detection, models can more effectively manage diverse types of input anomalies. Future work may extend these findings to character-level attacks and other NLP tasks such as question answering or summarization.

Summary updated

Abstract

In this paper, we study the differences and commonalities between statistically out-of-distribution (OOD) samples and adversarial (Adv) samples, both of which hurting a text classification model's performance. We conduct analyses to compare the two types of anomalies (OOD and Adv samples) with the in-distribution (ID) ones from three aspects: the input features, the hidden representations in each layer of the model, and the output probability distributions of the classifier. We find that OOD samples expose their aberration starting from the first layer, while the abnormalities of Adv samples do not emerge until the deeper layers of the model. We also illustrate that the models' output probabilities for Adv samples tend to be more unconfident. Based on our observations, we propose a simple method to separate ID, OOD, and Adv samples using the hidden representations and output probabilities of the model. On multiple combinations of ID, OOD datasets, and Adv attacks, our proposed method shows exceptional results on distinguishing ID, OOD, and Adv samples.

Empirical ResultsBrowse all benchmarks →

BenchmarkMethodMetricScoreProtocol / Evidence
IMDB · OOD) vs. SST-2 (IDProposed two-stage separation method — stage 1 OOD detectorAUROC1.00backbone=RoBERTa · id-dataset=SST-2 · aggregation=averaged across adversarial-dataset variants · ood-dataset=IMDB · sample-size=500 per dataset
Source quote
L596: IMDB │ 1.00 │ 0.99
AG News · Three-way separation; ID=AG-News, adversary=TextFooler, Proposed two-stage separation method — cascaded stages 1 and 2Accuracy0.823split: test · backbone=BERT · adversary=TextFooler · id-dataset=AG-News · ood-dataset=SST-2 · threshold-selection=5th percentile on ID development set
Source quote
L692: TextFooler │ 0.747 │ 0.823 │ 0.751
BibTeX
@misc{chiang2022understanding,
  author = {David Cheng-Han Chiang and Hung-Yi Lee},
  title = {{Understanding, Detecting, and Separating Out-of-Distribution Samples and Adversarial Samples in Text Classification}},
  year = {2022},
  eprint = {2204.04458},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2204.04458},
}
Figure