Anomaly detection with deep learning
Anomaly detection is the task of identifying examples that differ significantly from the patterns seen in normal data. It is fundamentally different from s...
Framing Anomaly Detection as a Learning Problem
Anomaly detection is the task of identifying examples that differ significantly from the patterns seen in normal data. It is fundamentally different from standard classification because anomalies are rare, diverse, and often unknown in advance, making it impractical to collect labeled examples of every anomaly type. Deep learning approaches to anomaly detection typically learn a compact representation of normal data and flag examples that deviate from that representation at test time.
In industrial quality control, a manufacturer may have millions of images of correctly produced parts but very few images of defective ones, and defect types may change as manufacturing processes evolve. Training a classifier with a defect class would be impractical. Instead, a deep anomaly detection system learns the distribution of normal parts and raises an alert when a new image falls outside that distribution.
Exam tip: Distinguish between anomaly detection and out-of-distribution detection. Anomaly detection typically refers to finding unusual examples within a single domain, such as fraudulent transactions in a stream of normal ones. Out-of-distribution detection identifies inputs from a completely different domain than the training data. Both problems use similar techniques but differ in the severity and type of distributional shift involved.
Autoencoder-Based Anomaly Detection
Autoencoders are a natural fit for anomaly detection. The model is trained to compress inputs into a low-dimensional latent code and then reconstruct the original input from that code, minimizing reconstruction error on normal training examples. At inference time, normal examples are reconstructed accurately while anomalies, which the model has not learned to represent, produce high reconstruction errors. The reconstruction error serves as an anomaly score.
Variational autoencoders extend this approach by imposing a structured probability distribution on the latent space, making it easier to identify when a test example falls outside the typical region of the latent space. The evidence lower bound used to train a VAE explicitly models the probability of the input, allowing anomaly scoring based on estimated density rather than just reconstruction error.
One practical challenge is that powerful autoencoders can sometimes reconstruct anomalies too accurately by generalizing beyond the training distribution in unhelpful ways. Constraining the model's capacity, using dropout, or incorporating contrastive training objectives can prevent this over-generalization.
Contrastive and Transformer-Based Methods
Recent anomaly detection methods use contrastive learning to train representations that cluster normal data tightly while spreading anomalies away from the normal cluster. PatchCore, a strong baseline for industrial anomaly detection, extracts patch-level features from a pretrained backbone and builds a memory bank of normal feature vectors. Test-time anomaly scores are computed as the distance from each patch's feature to its nearest neighbor in the memory bank.
Transformer-based methods have proven effective for time-series anomaly detection. Anomaly Transformer uses an association discrepancy measure based on attention patterns: normal data points tend to attend to nearby time steps, while anomalies exhibit unusual long-range attention patterns. The discrepancy between these patterns serves as an anomaly score.
Evaluation of anomaly detection systems must account for the strong class imbalance between normal and anomalous examples. Standard accuracy is uninformative because a classifier that always predicts normal achieves very high accuracy when anomalies are rare. Area under the ROC curve and average precision are more informative metrics, and threshold selection should be calibrated based on the acceptable false positive rate for the specific application.