-
Notifications
You must be signed in to change notification settings - Fork 26
/
run.py
160 lines (129 loc) · 6.2 KB
/
run.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
import os
import pickle
import tensorflow as tf
import argparse
from model_core.config import ModelParam
from data_factory import LoadData, BatchLoader
from model_core.state_model import ClusterStateRecognition
from model_core.models import EvoNet_TSC
import model_core.metrics as mt
datainfos = {'djia30': [50, 5, 4, 3.0],
'webtraffic': [12, 30, 1, 3.0],
'netflow': [15, 24, 2, 6.0],
'clockerr': [12, 4, 2, 6.0]}
def main(dataname, gpu=0):
params = ModelParam()
params.data_name = dataname
params.his_len = datainfos[params.data_name][0]
params.segment_len = datainfos[params.data_name][1]
params.segment_dim = datainfos[params.data_name][2]
params.node_dim = 2 * params.segment_dim * params.segment_len
params.id_gpu = '{}'.format(gpu)
params.pos_weight = datainfos[params.data_name][3]
params.learning_rate = 0.001
os.environ["CUDA_VISIBLE_DEVICES"] = params.id_gpu
dataloader = LoadData()
dataloader.set_configuration(params)
trainx, trainy, testx, testy = dataloader.fetch_data()
rawx, rawy = dataloader.fetch_raw_data()
print(rawx.shape, rawy.shape, trainx.shape, trainy.shape, testx.shape, testy.shape)
n = trainx.shape[0]+testx.shape[0]
y1 = sum(trainy[:,-1])+sum(testy[:, -1])
print(n, y1/n, rawx.shape[0] * rawx.shape[1])
# state
print("state recognizing...")
state_model = ClusterStateRecognition()
state_model.set_configuration(params)
state_model.build_model()
# state_model.fit(rawx)
train_prob, train_patterns = state_model.predict(trainx)
test_prob, test_patterns = state_model.predict(testx)
print(train_patterns.shape, train_prob.shape, test_patterns.shape, test_prob.shape)
# establish dataloader
trainloader = BatchLoader(params.batch_size)
trainloader.load_data(trainx, trainy, train_prob, train_patterns, shuffle=True)
testloader = BatchLoader(params.batch_size)
testloader.load_data(testx, testy, test_prob, test_patterns, shuffle=False)
# model
model = EvoNet_TSC()
model.set_configuration(params)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)
config = tf.ConfigProto(gpu_options=gpu_options)
print('model training...')
with tf.Session(config=config) as sess:
model.build_model(is_training=True)
init_vars = tf.global_variables_initializer()
sess.run(init_vars)
bestP = 0.0
for i in range(100):
loss = model.fit(sess, trainloader)
y_pred, y_pred_prob = model.predict(sess, testloader)
results = mt.predict_accuracy(testy[:, -1], y_pred)
auc = mt.predict_auc(testy[:, -1], y_pred_prob[:, 1])
logstr = 'Epochs {:d}, loss {:f}, Accuracy {:f}, Precision {:f}, Recall {:f}, F1 {:f}, AUC {:f}'.format(i, loss, results['Accuracy'], results['Precision'], results['Recall'], results['F1'], auc)
print(logstr)
p = 2 * results['Precision'] * results['Recall'] / (results['Precision'] + results['Recall'])
if p > bestP:
model.store(params.model_save_path, sess=sess)
bestP = p
print('epoch {} store.'.format(i))
print("model testing...")
tf.reset_default_graph()
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
model.build_model(is_training=False)
model.restore(params.model_save_path, sess=sess)
y_pred, y_pred_prob = model.predict(sess, testloader)
results = mt.predict_accuracy(testy[:, -1], y_pred)
auc = mt.predict_auc(testy[:, -1], y_pred_prob[:, 1])
logstr = 'Accuracy {:f}, Precision {:f}, Recall {:f}, F1 {:f}, AUC {:f}'.format(results['Accuracy'], results['Precision'], results['Recall'], results['F1'], auc)
print(logstr)
def getattention(dataname, gpu=0):
params = ModelParam()
params.data_name = dataname
params.his_len = datainfos[params.data_name][0]
params.segment_len = datainfos[params.data_name][1]
params.segment_dim = datainfos[params.data_name][2]
params.node_dim = 2 * params.segment_dim * params.segment_len
params.id_gpu = '{}'.format(gpu)
params.pos_weight = datainfos[params.data_name][3]
params.learning_rate = 0.001
os.environ["CUDA_VISIBLE_DEVICES"] = params.id_gpu
dataloader = LoadData()
dataloader.set_configuration(params)
trainx, trainy, _, _ = dataloader.fetch_data()
rawx = trainx
rawy = trainy
print(rawx.shape, rawy.shape)
# state
print("state recognizing...")
state_model = ClusterStateRecognition()
state_model.set_configuration(params)
state_model.build_model()
prob, patterns = state_model.predict(rawx)
print(patterns.shape, prob.shape)
# establish dataloader
testloader = BatchLoader(params.batch_size)
testloader.load_data(rawx, rawy, prob, patterns, shuffle=False)
# model
model = EvoNet_TSC()
model.set_configuration(params)
tf.reset_default_graph()
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
model.build_model(is_training=False)
model.restore(params.model_save_path, sess=sess)
y_pred, y_pred_prob = model.predict(sess, testloader)
attentions = model.getAttention(sess, testloader)
results = mt.predict_accuracy(rawy[:, -1], y_pred)
auc = mt.predict_auc(rawy[:, -1], y_pred_prob[:, 1])
logstr = 'Accuracy {:f}, Precision {:f}, Recall {:f}, F1 {:f}, AUC {:f}'.format(results['Accuracy'], results['Precision'], results['Recall'], results['F1'], auc)
print(logstr)
store_obj = {'x': rawx, 'y': rawy, 'prob': prob, 'pattern':patterns, 'attention':attentions}
pickle.dump(store_obj, open('./Repo/output/result_{}.pkl'.format(dataname), 'wb'), pickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dataset", type=str, choices=['djia30', 'webtraffic', 'netflow', 'clockerr'], default='djia30', help="select dataset")
parser.add_argument("-g", "--gpu", type=str, choices=['0', '1', '2'], default='0', help="target gpu id")
args = parser.parse_args()
main(args.dataset, gpu=args.gpu)
getattention(args.dataset, gpu=args.gpu)