### Initialize and Use RevIN Normalization Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Demonstrates how to initialize the RevIN layer, normalize input data, and denormalize it back to the original scale. ```python import torch from model.RevIN import RevIN # Initialize RevIN layer revin = RevIN( num_features=55, # Number of channels/features eps=1e-5, # Numerical stability constant affine=True # Learnable affine parameters ) # Move to GPU if torch.cuda.is_available(): revin = revin.cuda() # Input time series: (batch_size, window_size, num_features) x = torch.randn(32, 100, 55).cuda() # Normalize the input x_normalized = revin(x, mode='norm') print(f"Normalized mean: {x_normalized.mean():.6f}") print(f"Normalized std: {x_normalized.std():.6f}") # Process with model... # Then denormalize for output x_denormalized = revin(x_normalized, mode='denorm') print(f"Reconstruction error: {(x - x_denormalized).abs().max():.6f}") ``` -------------------------------- ### Execute Solver Training and Testing Pipeline Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Configure and run the Solver class to manage the training, validation, and testing workflow. The configuration dictionary must be fully populated before initializing the solver. ```python from solver import Solver # Configuration dictionary for the solver config = { 'win_size': 100, # Sliding window size 'patch_size': [5], # Patch sizes for multi-scale attention 'lr': 1e-4, # Learning rate 'loss_fuc': 'MSE', # Loss function ('MSE' or 'MAE') 'n_heads': 1, # Number of attention heads 'e_layers': 3, # Number of encoder layers 'd_model': 256, # Model dimension 'input_c': 38, # Number of input channels 'output_c': 38, # Number of output channels 'num_epochs': 10, # Maximum training epochs 'batch_size': 256, # Batch size 'dataset': 'SMD', # Dataset name 'data_path': 'SMD', # Path to dataset 'index': 0, # Dataset index (for UCR/SMD_Ori) 'model_save_path': 'checkpoints', # Model checkpoint directory 'anormly_ratio': 0.6, # Anomaly ratio for threshold calculation 'mode': 'train', # Mode: 'train' or 'test' 'use_gpu': True, # Use GPU acceleration 'gpu': 0, # GPU device ID 'use_multi_gpu': False, # Multi-GPU training 'devices': '0,1,2,3' # Device IDs for multi-GPU } # Initialize solver (loads data, builds model) solver = Solver(config) # Train the model solver.train() # Output: Epoch progress, validation losses, checkpoint saving # Test the model and get evaluation metrics accuracy, precision, recall, f_score = solver.test() # Output: # Threshold : 0.0234 # pa_accuracy : 0.9812 # pa_precision : 0.8934 # pa_recall : 0.9156 # pa_f_score : 0.9044 # MCC_score : 0.8567 # Affiliation precision : 0.9234 # Affiliation recall : 0.8901 ``` -------------------------------- ### Run CLI Training and Testing Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Executes training and testing routines via the command line interface for various datasets. ```bash # Train on SMD dataset python main.py \ --mode train \ --dataset SMD \ --data_path SMD \ --input_c 38 \ --output_c 38 \ --win_size 105 \ --patch_size 57 \ --batch_size 256 \ --num_epochs 10 \ --lr 1e-4 \ --anormly_ratio 0.6 \ --loss_fuc MSE \ --n_heads 1 \ --e_layers 3 \ --d_model 256 # Test the trained model python main.py \ --mode test \ --dataset SMD \ --data_path SMD \ --input_c 38 \ --output_c 38 \ --win_size 105 \ --patch_size 57 \ --batch_size 256 \ --anormly_ratio 0.6 # Train on MSL dataset python main.py \ --mode train \ --dataset MSL \ --data_path MSL \ --input_c 55 \ --output_c 55 \ --win_size 90 \ --patch_size 35 \ --batch_size 64 \ --num_epochs 3 \ --anormly_ratio 1 # Train on univariate UCR dataset (with index) python main.py \ --mode train \ --dataset UCR \ --data_path UCR \ --index 137 \ --input_c 1 \ --output_c 1 \ --win_size 100 \ --patch_size 5 # Use shell scripts for reproducible experiments bash ./scripts/SMD.sh bash ./scripts/MSL.sh bash ./scripts/SMAP.sh bash ./scripts/PSM.sh bash ./scripts/SWAT.sh bash ./scripts/UCR.sh ``` -------------------------------- ### Run SMD Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the SMD dataset. ```bash bash ./scripts/SMD.sh ``` -------------------------------- ### Initialize and Run DCdetector Model Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Instantiate the DCdetector model with specific hyperparameters and perform a forward pass. Ensure the input tensor matches the configured window size and channel dimensions. ```python import torch from model.DCdetector import DCdetector # Initialize the DCdetector model model = DCdetector( win_size=100, # Window size for time series segments enc_in=55, # Number of input channels/features c_out=55, # Number of output channels n_heads=1, # Number of attention heads d_model=256, # Model dimension e_layers=3, # Number of encoder layers patch_size=[5, 7, 11], # Multi-scale patch sizes channel=55, # Number of channels (same as enc_in) d_ff=512, # Feed-forward dimension dropout=0.0, # Dropout rate activation='gelu', # Activation function output_attention=True # Whether to output attention matrices ) # Move to GPU if available if torch.cuda.is_available(): model.cuda() # Forward pass with sample input # Input shape: (batch_size, window_size, num_channels) x = torch.randn(32, 100, 55).cuda() series_list, prior_list = model(x) # series_list: List of patch-wise attention outputs (one per encoder layer per scale) # prior_list: List of in-patch attention outputs (one per encoder layer per scale) print(f"Number of attention outputs: {len(series_list)}") ``` -------------------------------- ### Run SWAT Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the SWAT dataset. ```bash bash ./scripts/SWAT.sh ``` -------------------------------- ### Iterate Through Training Data Batches Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Demonstrates iterating through the training data loader to access batches of data and labels. The shapes of data and labels are printed for the first batch. ```python # Iterate through batches for batch_idx, (data, labels) in enumerate(train_loader): # data shape: (batch_size, win_size, num_channels) # labels shape: (batch_size, win_size) print(f"Batch {batch_idx}: data={data.shape}, labels={labels.shape}") break ``` -------------------------------- ### Run Ablation Study on Window Size Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for ablation study on window size. ```bash bash ./scripts/Ablation_Window_Size.sh ``` -------------------------------- ### Initialize Full Attention Layer Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Initializes a full AttentionLayer, integrating the DAC structure with projections. Requires model dimension, patch sizes, channel count, number of heads, and window size. ```python attn_layer = AttentionLayer( attention=dac, d_model=256, # Model dimension patch_size=[5, 7, 11], # Patch sizes channel=55, # Number of channels n_heads=1, # Number of attention heads win_size=100 # Window size ) ``` -------------------------------- ### Run PSM Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the PSM dataset. ```bash bash ./scripts/PSM.sh ``` -------------------------------- ### Run MSL Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the MSL dataset. ```bash bash ./scripts/MSL.sh ``` -------------------------------- ### Run Ablation Study on Multiscale Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for ablation study on multiscale. ```bash bash ./scripts/Ablation_Multiscale.sh ``` -------------------------------- ### Run SMAP Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the SMAP dataset. ```bash bash ./scripts/SMAP.sh ``` -------------------------------- ### Load SMD Dataset for Training Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Loads the Server Machine Dataset (SMD) for training using specified parameters like data path, batch size, and window size. Ensure the 'dataset/SMD' path is correct. ```python train_loader = get_loader_segment( index=0, # Dataset index (used for UCR, SMD_Ori) data_path='dataset/SMD', # Path to data files batch_size=256, # Batch size win_size=100, # Window size for segmentation step=100, # Step size (default 100) mode='train', # Mode: 'train', 'val', 'test', or 'thre' dataset='SMD' # Dataset type ) ``` -------------------------------- ### Implement Early Stopping in Training Loop Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Use the EarlyStopping class to monitor validation losses and automatically trigger a stop when performance plateaus. Requires the solver module and torch/numpy dependencies. ```python from solver import EarlyStopping import torch import numpy as np # Initialize early stopping early_stopping = EarlyStopping( patience=5, # Epochs to wait before stopping verbose=True, # Print messages dataset_name='SMD', # Dataset name for checkpoint filename delta=0 # Minimum change to qualify as improvement ) # Simulated training loop model = torch.nn.Linear(10, 1) # Example model checkpoint_path = 'checkpoints' for epoch in range(100): # Simulated validation losses val_loss1 = np.random.random() * (0.5 ** (epoch / 10)) val_loss2 = np.random.random() * (0.5 ** (epoch / 10)) # Check early stopping early_stopping(val_loss1, val_loss2, model, checkpoint_path) if early_stopping.early_stop: print(f"Early stopping triggered at epoch {epoch}") break # Load best model checkpoint model.load_state_dict(torch.load(f'{checkpoint_path}/SMD_checkpoint.pth')) ``` -------------------------------- ### Load SMD Dataset for Testing Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Loads the Server Machine Dataset (SMD) for testing, including labels. Uses 'test' mode. Verify the data path and dataset settings. ```python test_loader = get_loader_segment( index=0, data_path='dataset/SMD', batch_size=256, win_size=100, mode='test', dataset='SMD' ) ``` -------------------------------- ### Run Ablation Study on Encoder Layers Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for ablation study on encoder layers. ```bash bash ./scripts/Ablation_encoder_layer.sh ``` -------------------------------- ### Run NIPS TS Water Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the NIPS TS Water dataset. ```bash bash ./scripts/NIPS_TS_Water.sh ``` -------------------------------- ### Run NIPS TS Swan Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the NIPS TS Swan dataset. ```bash bash ./scripts/NIPS_TS_Swan.sh ``` -------------------------------- ### Run Ablation Study on Attention Heads Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for ablation study on attention heads. ```bash bash ./scripts/Ablation_attention_head.sh ``` -------------------------------- ### Initialize Token Embedding Layer Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Initializes a TokenEmbedding layer using 1D convolution with circular padding. Requires input channel count and model dimension. ```python # Token embedding alone (1D convolution with circular padding) token_emb = TokenEmbedding(c_in=5, d_model=256) tokens = token_emb(x) print(f"Token embedding shape: {tokens.shape}") # (32, 20, 256) ``` -------------------------------- ### Run UCR Experiment Source: https://github.com/damo-di-ml/kdd2023-dcdetector/blob/main/readme.md Execute the experiment script for the UCR dataset. ```bash bash ./scripts/UCR.sh ``` -------------------------------- ### Import Data Loader Factory Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Import the utility function for loading time series anomaly detection datasets. ```python from data_factory.data_loader import get_loader_segment ``` -------------------------------- ### Initialize Data Embedding Layer Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Initializes the DataEmbedding layer, which combines token and positional embeddings. Requires input channel count and model dimension. Dropout rate can be adjusted. ```python # Initialize data embedding layer embedding = DataEmbedding( c_in=5, # Input dimension (patch size or window/patch_num) d_model=256, # Output embedding dimension dropout=0.05 # Dropout rate ) ``` -------------------------------- ### Initialize DAC Structure for Attention Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Initializes the Dual Attention Contrastive structure (DAC_structure) with specified window size, multi-scale patch sizes, and channel count. Set mask_flag to False if masking is not needed. ```python dac = DAC_structure( win_size=100, # Window size patch_size=[5, 7, 11], # Multi-scale patch sizes channel=55, # Number of input channels mask_flag=False, # Whether to use attention masking scale=None, # Attention scale (default: 1/sqrt(d_k)) attention_dropout=0.05, # Dropout for attention weights output_attention=True # Return attention matrices ) ``` -------------------------------- ### Initialize Positional Embedding Layer Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Initializes a PositionalEmbedding layer using sinusoidal functions. Requires model dimension and maximum sequence length. ```python # Positional embedding (sinusoidal) pos_emb = PositionalEmbedding(d_model=256, max_len=5000) positions = pos_emb(x) print(f"Positional embedding shape: {positions.shape}") # (1, 20, 256) ``` -------------------------------- ### Load SMD Dataset for Validation Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Loads the Server Machine Dataset (SMD) for validation. Similar to training loader, but uses 'val' mode. Check data path and dataset configuration. ```python val_loader = get_loader_segment( index=0, data_path='dataset/SMD', batch_size=256, win_size=100, mode='val', dataset='SMD' ) ``` -------------------------------- ### Implement KL Divergence Loss Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Computes the KL divergence loss used for contrastive learning between dual attention branches. ```python import torch def my_kl_loss(p, q): """ Compute KL divergence between distributions p and q. Args: p: Source distribution tensor q: Target distribution tensor Returns: KL divergence averaged over batch dimension """ res = p * (torch.log(p + 0.0001) - torch.log(q + 0.0001)) return torch.mean(torch.sum(res, dim=-1), dim=1) # Example: Computing contrastive loss between series and prior attention series = torch.softmax(torch.randn(32, 1, 100, 100), dim=-1).cuda() prior = torch.softmax(torch.randn(32, 1, 100, 100), dim=-1).cuda() # Normalize prior for comparison prior_normalized = prior / torch.unsqueeze(torch.sum(prior, dim=-1), dim=-1) # Symmetric KL divergence loss series_loss = my_kl_loss(series, prior_normalized.detach()) prior_loss = my_kl_loss(prior_normalized, series.detach()) # Final contrastive loss (maximize difference) loss = prior_loss - series_loss print(f"Series loss: {series_loss.mean():.4f}") print(f"Prior loss: {prior_loss.mean():.4f}") print(f"Contrastive loss: {loss.mean():.4f}") ``` -------------------------------- ### Apply Data Embedding to Input Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Applies the DataEmbedding layer to a sample input tensor. The input shape is (batch_size, sequence_length, input_channels), and the output shape is (batch_size, sequence_length, d_model). ```python # Sample input: (batch_size, sequence_length, input_channels) x = torch.randn(32, 20, 5) # 32 samples, 20 time steps, 5 features embedded = embedding(x) print(f"Input shape: {x.shape}") print(f"Embedded shape: {embedded.shape}") # (32, 20, 256) ``` -------------------------------- ### Forward Pass Through Attention Layer Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Performs a forward pass through the initialized attention layer using sample embedded input tensors. Outputs 'series' (patch-wise attention) and 'prior' (in-patch attention). Ensure CUDA is available for .cuda() calls. ```python # Sample embedded input tensors batch_size, patch_num, d_model = 32 * 55, 20, 256 # batch*channel, patches, dim x_patch_size = torch.randn(batch_size, patch_num, d_model).cuda() x_patch_num = torch.randn(batch_size, 5, d_model).cuda() # patch_size dimension x_ori = torch.randn(32, 100, d_model).cuda() # Original window embedding # Forward pass through attention layer series, prior = attn_layer( x_patch_size, # Patch-size embeddings x_patch_num, # Patch-num embeddings x_ori, # Original embeddings patch_index=0, # Current patch scale index attn_mask=None # Optional attention mask ) # series: Patch-wise attention (inter-patch relationships) # prior: In-patch attention (intra-patch relationships) print(f"Series attention shape: {series.shape}") print(f"Prior attention shape: {prior.shape}") ``` -------------------------------- ### Compute Anomaly Detection Metrics Source: https://context7.com/damo-di-ml/kdd2023-dcdetector/llms.txt Calculates comprehensive anomaly detection metrics including point-adjusted F1, MCC, and VUS scores. ```python import numpy as np from metrics.metrics import combine_all_evaluation_scores # Ground truth and predictions (binary arrays) y_test = np.array([0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0]) pred_labels = np.array([0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0]) # Anomaly scores (continuous values, higher = more anomalous) anomaly_scores = np.array([0.1, 0.2, 0.6, 0.8, 0.9, 0.85, 0.4, 0.1, 0.15, 0.7, 0.8, 0.75, 0.2, 0.1]) # Compute all evaluation metrics scores = combine_all_evaluation_scores(y_test, pred_labels, anomaly_scores) # Available metrics: # - pa_accuracy: Point-adjusted accuracy # - pa_precision: Point-adjusted precision # - pa_recall: Point-adjusted recall # - pa_f_score: Point-adjusted F1 score # - MCC_score: Matthews Correlation Coefficient # - Affiliation precision: Event-based precision # - Affiliation recall: Event-based recall # - R_AUC_ROC: Range-based AUC-ROC # - R_AUC_PR: Range-based AUC-PR # - VUS_ROC: Volume Under Surface (ROC) # - VUS_PR: Volume Under Surface (PR) for metric, value in scores.items(): print(f"{metric:25}: {value:.4f}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.