Anomaly Transformer
- The paper introduces a novel attention mechanism that uses association discrepancy to differentiate normal and anomalous time series patterns.
- It employs a dual-branch architecture combining series-association and prior-association to capture both global and local context in sequential data.
- Extensive benchmarks demonstrate the model’s superior performance through improved precision, recall, and F1 scores over multiple deep learning baselines.
Anomaly Transformer refers to a class of unsupervised, transformer-based models that leverage association discrepancy for detecting anomalies in sequential data, most notably time series. These models are characterized by novel attention mechanisms that contrast local and global attention patterns and introduce precise, theoretically grounded anomaly scoring functions. The principal representative is the "Anomaly Transformer: Time Series Anomaly Detection with Association Discrepancy" model (Xu et al., 2021), which established a foundation for subsequent advances.
1. Architectural Foundation and Attention Mechanism
Anomaly Transformer employs a stack of encoder-only transformer layers, each operating over embedded and optionally position-encoded time series data. For a time series , where , each input is linearly projected to a -dimensional embedding. Positional encoding, typically sinusoidal, may be added.
The critical innovation is the two-branch "Anomaly-Attention" mechanism, formally comprising:
- Series-association branch: Computes conventional scaled dot-product self-attention,
where , , and are standard query, key, and value matrices, and is the current layer.
- Prior-association branch: Imposes an inductive bias toward temporal adjacency by generating a learnable, unimodal Gaussian over indices, parameterized by learnable per-index widths :
In each encoder block, these two attention distributions, 0 and 1, are computed in parallel. The value outputs are propagated to subsequent layers via standard residual connections and a pointwise MLP.
2. Association Discrepancy and Anomaly Scoring
Central to the Anomaly Transformer is measuring the divergence between the series-association and prior-association distributions for each position 2. The association discrepancy metric quantifies this:
3
where 4 is the number of Transformer layers.
Empirically, normal points develop support for non-adjacent associations, leading to a high discrepancy (large AssDis), while anomalies—unable to relate to typical long-range patterns—exhibit concentrated, local attention and thus lower AssDis. This property enables a theoretically motivated, point-wise discrimination criterion.
At test time, the model computes for each time step:
- 5: softmax-normalized association discrepancy (higher when AssDis is low)
- 6: per-point reconstruction error, 7
- Final anomaly score: 8
3. Minimax Training Objective
The optimization objective jointly balances two terms:
- Reconstruction loss: 9 enforces faithful representation of normal data.
- Discrepancy regularizer: Weighted sum of per-step association discrepancy, 0.
The overall loss function is
1
A minimax strategy is used to avoid collapse (2):
- Minimize-phase: Update prior branch parameters (3) and decoder, minimizing 4, with gradients stopped on 5.
- Maximize-phase: Update 6 (series-attention branch) and decoder, maximizing 7 (i.e., increasing AssDis) while minimizing 8, with gradients stopped on 9.
This alternation amplifies the separation between normal and anomalous attention patterns.
4. Experimental Validation and Benchmarks
Anomaly Transformer has been rigorously benchmarked against deep learning baselines such as InterFusion, OmniAnomaly, and THOC. Evaluation spans multiple domains:
- Datasets: SMD (Server Machine Data), PSM (Pooled Server Metrics), MSL (Mars Science Laboratory), SMAP (Soil Moisture Satellite), SWaT (Water Treatment), and NeurIPS-TS (synthetic).
- Metrics: Precision, Recall, F1 score, ROC-AUC.
Representative F1 results extracted directly from the data:
| Dataset | InterFusion | OmniAnomaly | THOC | Anomaly Transformer |
|---|---|---|---|---|
| SMD | 86.22 | 85.22 | 84.99 | 92.33 |
| MSL | 86.62 | 87.67 | 89.69 | 93.59 |
| SMAP | 89.14 | 86.92 | 90.68 | 96.69 |
| SWaT | 83.01 | 82.83 | 85.13 | 94.07 |
| PSM | 83.52 | 80.83 | 89.54 | 97.89 |
The Anomaly Transformer achieves state-of-the-art performance on all listed benchmarks (Xu et al., 2021).
5. Implementation and Inference Details
Salient implementation specifications include:
- Architecture: 3 encoder layers, 0, 8 attention heads
- Windowing: Non-overlapping input windows of length 100
- Optimization: Adam optimizer, learning rate 1, batch size 32, early stopping within 10 epochs
- Thresholding: Calibrated on validation data; top 2% of anomaly scores selected (e.g., 3 for SWaT, 4 for SMD)
During inference, the anomaly score for each point is computed using the hybrid criterion described above, allowing robust temporal anomaly delineation.
6. Related Directions and Extensions
Subsequent advances incorporate the core principles of the Anomaly Transformer, including:
- ADTR: Applies transformers for feature (rather than pixel) reconstruction, exploiting semantic embeddings from pretrained backbones and leveraging transformer attention to enhance anomaly–normal separation (You et al., 2022).
- AnomalyBERT: Introduces a self-supervised transformer trained using synthetic data degradations, with a relative position bias to capture flexible temporal dependencies in multivariate time series (Jeong et al., 2023).
- ISSTAD: Synergizes masked autoencoding with pixelwise self-supervision for image anomaly detection and localization, fusing per-pixel reconstruction errors with classifier outputs for precise detection (Jin et al., 2023).
- TiSAT: Proposes a sparse-attention transformer with a novel sequence-level evaluation criterion, further validating the efficacy of transformer architectures for timely anomaly detection in sequential data (Doshi et al., 2022).
7. Position in the Literature and Significance
The Anomaly Transformer established a conceptual and practical framework in which association discrepancy—reflecting the contrast between global and local attentional focus—serves as a robust indicator of anomalous structure in sequential data. The minimax training framework creates a sharp distinguishability criterion between normal and abnormal regimes, with empirical superiority across heterogeneous benchmarks. The mechanistic insights—particularly the use of attention distributions as intrinsic anomaly markers—have fundamentally shaped subsequent transformer-based anomaly detection research in both time series and high-dimensional vision settings (Xu et al., 2021, You et al., 2022, Jeong et al., 2023, Jin et al., 2023, Doshi et al., 2022).