Abstract
Anomaly Detection (AD) in Natural Language Processing (NLP) presents unique challenges due to the discrete and high-dimensional nature of text data. This paper introduces DATE (Detecting Anomalies in Text using ELECTRA), the first end-to-end deep learning framework for text anomaly detection that leverages self-supervision. By training a model to solve complex linguistic "puzzles"—specifically identifying corrupted tokens and predicting masking patterns—the system learns the intricate structure of "normal" text without relying on external pre-trained embeddings. Our evaluations on standard benchmarks like 20Newsgroups and AG News show that DATE achieves state-of-the-art performance, outperforming existing methods by significant margins and demonstrating high robustness to training data contamination.
Introduction
Finding the "needle in a haystack" is a classic problem in machine learning, known as Anomaly Detection. While AD has seen significant success in computer vision and signal processing, its application to text has lagged. Text is inherently difficult for AD because it is discrete and high-dimensional. Previous approaches often relied on pre-trained word embeddings (e.g., GloVe), which can be problematic as they may already contain information about the anomalies or fail to capture the specific global context of a target dataset. DATE addresses these issues by training from scratch to understand the unique "texture" of a dataset, allowing it to recognize when new text deviates from the established norm.
Methodology: The DATE Framework
The core innovation of DATE lies in its use of the ELECTRA architecture combined with two simultaneous self-supervised "pretext tasks" designed to build a deep understanding of the training data.
Pretext Tasks
- Replaced Token Detection (RTD): A generator model replaces specific words in a sentence with plausible alternatives. The discriminator must then predict, for every token, whether it is an original word or a replacement.
- Replaced Mask Detection (RMD): This novel sequence-level task requires the model to identify which of $K$ predefined "mask patterns" was used to hide original words in a corrupted sentence.
By forcing the model to solve these tasks, it develops a granular understanding of linguistic structure and semantics. At inference time, the model generates a Pseudo Label (PL) score. If the model is "uncertain" or "confused" by a sentence (assigning low probability to tokens being original), that sentence is flagged as an anomaly.
Experimental Setup and Results
The model was evaluated on three key datasets: 20Newsgroups, AG News, and PAN 2020 (for authorship detection). In these experiments, one class is treated as "normal" while others are considered anomalies.
Performance Benchmarks
DATE established new state-of-the-art results, showing an average improvement of 4.7% AUROC on 20Newsgroups and 6.9% on AG News over existing semi-supervised methods.
Key Insights
- The "Random" Advantage: Surprisingly, the authors found that a random generator (sampling words uniformly from the vocabulary) outperformed "smart" generators like BERT. A smart generator creates replacements so plausible they appear as inliers, which hinders the discriminator's ability to define a clear boundary for anomalies.
- Multi-Scale Supervision: Combining token-level (RTD) and sequence-level (RMD) tasks stabilizes training and forces the model to consider both local word choice and global sentence structure.
- Zero Prior Knowledge: By avoiding pre-training on external data, DATE ensures it does not have unwanted prior knowledge about potential anomalies, making it more robust to truly novel outliers.
- Robustness to Contamination: In unsupervised settings where the training data contains up to 10% anomalies, DATE remained highly effective, outperforming other methods trained on perfectly clean data.
Interpretability and Future Work
DATE provides inherent interpretability; because it produces a score for every token, researchers can visualize which specific words triggered an anomaly alert. For example, in a sports-themed dataset, the presence of financial or political terms would be highlighted as anomalous.
Future work will explore "informed masking" strategies—targeting specific parts of speech like verbs or nouns—to further improve semantic understanding. Additionally, the authors suggest exploring contrastive learning and reformulating the system as an Energy-Based Model (EBM) to provide a more mathematically grounded evaluation of text abnormality.
Summary updated