Skip to content

Add DCdetector anomaly detection model#813

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/add-dcdetector-model
Draft

Add DCdetector anomaly detection model#813
Copilot wants to merge 2 commits into
mainfrom
copilot/add-dcdetector-model

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 6, 2026

Adds DCdetector (KDD 2023) — a dual-attention contrastive model for time series anomaly detection — as a new pypots.anomaly_detection model.

Architecture

  • Multi-scale patching: splits input into two complementary views per patch size — patch-wise (inter-patch) and in-patch (intra-patch) — then embeds each with a Conv1D + positional encoding
  • DAC attention (DACStructure): computes separate softmax attention for each view, upsamples both back to full window size [B, H, L, L], averages over the channel dimension
  • RevIN normalization: uses existing pypots.nn.modules.revin.RevIN
  • Minimax contrastive loss: prior_loss − series_loss (symmetric KL divergence between the two attention views)

Anomaly scoring

Unlike reconstruction-based detectors, DCdetector has no reconstruction head. predict() is fully overridden to compute per-time-step anomaly scores as softmax(−KL_series − KL_prior) × temperature (temperature=50 per paper), with threshold derived from the training set distribution.

New files

File Purpose
pypots/nn/modules/dcdetector/layers.py BackboneDCdetector + all sub-modules
pypots/anomaly_detection/dcdetector/core.py _DCdetector (contrastive loss, ModelCore)
pypots/anomaly_detection/dcdetector/model.py User-facing DCdetector (BaseNNDetector)
tests/anomaly_detection/dcdetector.py Full test suite

Usage

from pypots.anomaly_detection import DCdetector

model = DCdetector(
    n_steps=100,       # must be divisible by every patch size
    n_features=55,
    anomaly_rate=0.01,
    patch_sizes=[5, 10, 20],
    d_model=256,
    n_heads=4,
    e_layers=3,
)
model.fit(train_set)
results = model.predict(test_set)  # {"anomaly_detection": np.ndarray}

n_steps must be divisible by every value in patch_sizes; the constructor asserts this upfront.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add DCdetector</issue_title>
<issue_description>### 1. Model description

DCdetector should be included in PyPOTS as an anomaly detection model

@inproceedings{yang2023dcdetector,
title={DCdetector: Dual Attention Contrastive Representation Learning for Time Series Anomaly Detection},
author={Yiyuan Yang and Chaoli Zhang and Tian Zhou and Qingsong Wen and Liang Sun},
booktitle={Proc. 29th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining (KDD 2023)},
location = {Long Beach, CA},
pages={3033–3045},
year={2023}
}

2. Check open-source status

  • The model implementation is publicly available

3. Provide useful information for the implementation

Check out the official code repository here https://github.com/DAMO-DI-ML/KDD2023-DCdetector

4. Your contribution

Will make a PR</issue_description>

<agent_instructions>DCdetector uses RevIN as a submodule. RevIN is in PyPOTS already. You can find it in pypots/nn/modules/revin/layers.py</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: WenjieDu <17807970+WenjieDu@users.noreply.github.com>
Copilot AI changed the title [WIP] Add DCdetector anomaly detection model to PyPOTS Add DCdetector anomaly detection model Mar 6, 2026
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Mar 6, 2026

@WenjieDu
Copy link
Copy Markdown
Owner

@yyysjz1997 Hi Yiyuan, as the 1st author of DCdetector, could you help review this PR please

@WenjieDu WenjieDu added the keep Keep this issue away from being stale. label Mar 16, 2026
@yyysjz1997
Copy link
Copy Markdown
Contributor

yyysjz1997 commented Mar 18, 2026

@yyysjz1997 Hi Yiyuan, as the 1st author of DCdetector, could you help review this PR please

Hi Wenjie, I just noticed that the hyperparameter patch_size is usually a small prime number list, e.g., [3,5,7]. And the window size hyperparameter n_steps should be divisible by every patch size (e.g., 105 for patch_size = [3,5,7]).

@WenjieDu
Copy link
Copy Markdown
Owner

@yyysjz1997 Got it. Do you have any further comments on this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep Keep this issue away from being stale.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add DCdetector

3 participants