Results of anomaly detection using AnomalyTransformer#

from ice.anomaly_detection.datasets import AnomalyDetectionRiethTEP
from ice.anomaly_detection.models import AnomalyTransformer
from sklearn.preprocessing import StandardScaler
import numpy as np
import pandas as pd

Download the dataset.

dataset = AnomalyDetectionRiethTEP()

Normalize the data.

scaler = StandardScaler()
dataset.df[dataset.train_mask] = scaler.fit_transform(dataset.df[dataset.train_mask])
dataset.df[dataset.test_mask] = scaler.transform(dataset.df[dataset.test_mask])

Create the AnomalyTransformer model.

model = AnomalyTransformer(
    window_size=32, 
    lr=0.001, 
    num_epochs=30, 
    device='cuda', 
    verbose=True, 
    val_ratio=0.1,
    save_checkpoints=True,
    threshold_level=0.98,
    d_model=32, 
    e_layers=1,
    d_ff=32, 
    dropout=0.0
    )

Load the checkpoint.

model.load_checkpoint('transformer_anomaly_detection_epoch_30.tar')

Evaluate the model on the test data.

metrics = model.evaluate(dataset.df[dataset.test_mask], dataset.target[dataset.test_mask])
metrics
{'accuracy': 0.8588669950738916,
 'true_positive_rate': [0.830817625],
 'false_positive_rate': [0.012466169724770642]}