-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimesnet_tasks.py
More file actions
93 lines (71 loc) · 3.14 KB
/
timesnet_tasks.py
File metadata and controls
93 lines (71 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import argparse
from timesnet.timesnet import *
from timesnet.timesnetmodule import *
from datetime import datetime
import time
import os
def train_TimesNet(train_data, model_name):
device = 'cuda' if torch.cuda.is_available() else 'cpu'
clf = TimesNet(seq_len=100,
stride=1,
lr=0.0001,
epochs=10,
batch_size=128,
epoch_steps=20,
prt_steps=1,
device=device,
pred_len=0,
e_layers=2,
d_model=64,
d_ff=64,
dropout=0.1,
top_k=3,
num_kernels=1, # hyperparameter decided by Bayesian Optimization
verbose=2,
random_state=42)
print("---Training TimesNet---")
clf.fit(train_data)
torch.save(clf, model_name+'.pt')
print("Saved", model_name+".pt\n")
def detect_anomalies(model_name, data): # anomly detection in real time
device = 'cuda' if torch.cuda.is_available() else 'cpu'
saved_model = torch.load(model_name + ".pt", device)
saved_model.device = device
score = saved_model.decision_function(data)
label_ = np.where(score > saved_model.threshold_, 1, 0)
if 1 in label_:
print("Annomaly occured at", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
def simulate_detecting_anomalies(model_name, test_data): # for simulating anomaly detection
device = 'cuda' if torch.cuda.is_available() else 'cpu'
saved_model = torch.load(model_name + ".pt", device)
saved_model.device = device
seq_num = len(test_data) // saved_model.seq_len
for i in range(seq_num):
data = test_data[i * saved_model.seq_len: (i + 1) * saved_model.seq_len]
score = saved_model.decision_function(data)
label_ = np.where(score > saved_model.threshold_, 1, 0)
if 1 in label_:
print("Anomaly occured at", datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(0.01)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default="train", help="whether to train before detect anomalies")
parser.add_argument("--data", type=str, default="PSM", help="train, test data should be inside this path")
parser.add_argument("--model_name", type=str, default="timesnet", help="which model to use")
opt = parser.parse_args()
# train and save timesnet for anoamly detection
if opt.task == "train":
X_train = np.load("./" + opt.data + "/X_train.npy")
train_TimesNet(X_train, opt.model_name)
# simulate anomaly detection with test data
if opt.task == "simulate":
X_test = np.load("./" + opt.data + "/X_test.npy")
print("---Start simulating anomaly detection---")
simulate_detecting_anomalies(opt.model_name, X_test)
# real-time anomaly detection
if opt.task == "detect":
path = "./" + opt.data
print("---Start detecting anomalies---")
for data in os.listdir(path):
data_ = np.load(path + "/" + data)
detect_anomalies(opt.model_name, data_)