-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_iteration.py
223 lines (156 loc) · 8.29 KB
/
run_iteration.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 5 16:35:00 2023
@author: zmzhai
"""
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import pickle
import time
import ML1_1
def lstm_obs(attack_out='single', file_name='data_case14', cut_data=0.5,
obs_range=np.exp(np.linspace(np.log(0.05), np.log(1), num=10)),
iteration=20, input_dim=[-2], epoch=15, batch_size=64, class_weight={0: 1, 1: 2},
naive_threshold=0.5, seq_length=8):
f1_lstm, acc_lstm = np.zeros((len(obs_range), iteration)), np.zeros((len(obs_range), iteration))
# f1_naive, acc_naive = np.zeros((len(obs_range), iteration)), np.zeros((len(obs_range), iteration))
count = 0
for obs_i in range(len(obs_range)):
obs_prop = obs_range[obs_i]
for iter_i in range(iteration):
ml = ML1_1.ML(input_dim=input_dim, obs_prop=obs_prop, attack_out=attack_out, norm_multi='softmax')
ml.read_data(file_path='./data/', file_name=file_name)
ml.data_process(cut_data=cut_data, random_choose=True, centrality=None)
ml.normalization(add_noise=True)
ml.create_dataset_lstm(seq_length=seq_length)
ml.lstm_layers()
f1_1, accuracy_1 = ml.train_nn(epoch=epoch, batch_size=64, class_weight={0:1, 1:1})
# naive_f1, naive_accuracy = ml.naive_method(threshold=naive_threshold)
count += 1
print('----lstm training---, ', count)
f1_lstm[obs_i, iter_i] = f1_1
acc_lstm[obs_i, iter_i] = accuracy_1
# f1_naive[obs_i, iter_i] = naive_f1
# acc_naive[obs_i, iter_i] = naive_accuracy
if 'case14' in file_name:
save_file_name = 'lstm_obs_' + 'snet_' + attack_out
else:
save_file_name = 'lstm_obs_' + 'lnet_' + attack_out
save_file = open('./data_save/' + save_file_name + '.pkl', 'wb')
pickle.dump(f1_lstm, save_file)
pickle.dump(acc_lstm, save_file)
# pickle.dump(f1_naive, save_file)
# pickle.dump(acc_naive, save_file)
pickle.dump(obs_range, save_file)
save_file.close()
def lstm_centrality(attack_out='single', file_name='data_case14', cut_data=0.5,
obs_range=np.exp(np.linspace(np.log(0.05), np.log(1), num=10)),
iteration=20, input_dim=[-2], epoch=15, batch_size=64, class_weight={0: 1, 1: 2},
naive_threshold=0.5, seq_length=8):
# f1_rf, acc_rf = np.zeros((len(obs_range), iteration)), np.zeros((len(obs_range), iteration))
f1_central, acc_central = np.zeros((len(obs_range), iteration)), np.zeros((len(obs_range), iteration))
if 'case14' in file_name:
pkl_file = open('./data/' + 'central_case14' + '.pkl', 'rb')
centrality = pickle.load(pkl_file)
pkl_file.close()
else:
pkl_file = open('./data/' + 'central_wcci2022' + '.pkl', 'rb')
centrality = pickle.load(pkl_file)
pkl_file.close()
count = 0
for obs_i in range(len(obs_range)):
obs_prop = obs_range[obs_i]
for iter_i in range(iteration):
# centrality
ml = ML1_1.ML(input_dim=input_dim, obs_prop=obs_prop, attack_out=attack_out, norm_multi='softmax')
ml.read_data(file_path='./data/', file_name=file_name)
ml.data_process(cut_data=cut_data, random_choose=False, centrality=centrality)
ml.normalization(add_noise=True)
ml.create_dataset_lstm(seq_length=seq_length)
ml.lstm_layers()
f1_1, accuracy_1 = ml.train_nn(epoch=epoch, batch_size=64, class_weight={0:1, 1:1})
# naive_f1, naive_accuracy = ml.naive_method(threshold=naive_threshold)
count += 1
print('----lstm training---, ', count)
f1_central[obs_i, iter_i] = f1_1
acc_central[obs_i, iter_i] = accuracy_1
aaa = 1
if 'case14' in file_name:
save_file_name = 'lstm_centrality_' + 'snet_' + attack_out
else:
save_file_name = 'lstm_centrality_' + 'lnet_' + attack_out
save_file = open('./data_save/' + save_file_name + '.pkl', 'wb')
pickle.dump(f1_central, save_file)
pickle.dump(acc_central, save_file)
pickle.dump(obs_range, save_file)
save_file.close()
def ngrc_time_embedding(attack_out='single', file_name='data_case14', cut_data=0.5,
obs_range=np.exp(np.linspace(np.log(0.05), np.log(1), num=10)),
iteration=20, input_dim=[-2], epoch=15, batch_size=64, class_weight={0: 1, 1: 2},
naive_threshold=0.5, seq_range=[1, 3, 5]):
f1_ng, acc_ng = np.zeros((len(obs_range), len(seq_range), iteration)), np.zeros((len(obs_range), len(seq_range), iteration))
count = 0
for obs_i in range(len(obs_range)):
obs_prop = obs_range[obs_i]
for seq_i in range(len(seq_range)):
seq_length = seq_range[seq_i]
for iter_i in range(iteration):
ml = ML1_1.ML(input_dim=input_dim, obs_prop=obs_prop, attack_out=attack_out, norm_multi='softmax')
ml.read_data(file_path='./data/', file_name=file_name)
ml.data_process(cut_data=cut_data, random_choose=True, centrality=None)
ml.normalization(add_noise=True)
ml.create_dataset_ngrc(seq_length=seq_length)
ml.fnn_layers()
f1_1, accuracy_1 = ml.train_nn(epoch=10, batch_size=64, class_weight={0:1, 1:1})
# naive_f1, naive_accuracy = ml.naive_method(threshold=naive_threshold)
count += 1
print('----lstm training---, ', count)
f1_ng[obs_i, seq_i, iter_i] = f1_1
acc_ng[obs_i, seq_i, iter_i] = accuracy_1
if 'case14' in file_name:
save_file_name = 'ngrc_obs_' + 'snet_' + attack_out
else:
save_file_name = 'ngrc_obs_' + 'lnet_' + attack_out
save_file = open('./data_save/' + save_file_name + '.pkl', 'wb')
pickle.dump(f1_ng, save_file)
pickle.dump(acc_ng, save_file)
pickle.dump(obs_range, save_file)
pickle.dump(seq_range, save_file)
save_file.close()
if __name__ == '__main__':
attack_out = 'single'
file_name='data_wcci2022'
cut_data=0.7
obs_range = np.arange(0.1, 1.1, 0.1)
iteration=20
input_dim=[-2]
naive_threshold=0.5
seq_length=5
lstm_centrality(attack_out=attack_out, file_name=file_name, cut_data=cut_data,
obs_range=obs_range, iteration=iteration, input_dim=input_dim,
epoch=5, batch_size=64, class_weight={0: 1, 1: 1},
naive_threshold=naive_threshold, seq_length=seq_length)
attack_out = 'multi'
file_name='data_wcci2022'
lstm_centrality(attack_out=attack_out, file_name=file_name, cut_data=cut_data,
obs_range=obs_range, iteration=iteration, input_dim=input_dim,
epoch=5, batch_size=64, class_weight={0: 1, 1: 1},
naive_threshold=naive_threshold, seq_length=seq_length)
attack_out='single'
file_name='data_case14'
cut_data=0.5
obs_range = np.exp(np.linspace(np.log(0.05), np.log(1), num=10))
iteration=20
input_dim=[-2]
naive_threshold=0.5
seq_length=5
lstm_centrality(attack_out=attack_out, file_name=file_name, cut_data=cut_data,
obs_range=obs_range, iteration=iteration, input_dim=input_dim,
epoch=5, batch_size=64, class_weight={0: 1, 1: 1},
naive_threshold=naive_threshold, seq_length=seq_length)
attack_out='multi'
lstm_centrality(attack_out=attack_out, file_name=file_name, cut_data=cut_data,
obs_range=obs_range, iteration=iteration, input_dim=input_dim,
epoch=5, batch_size=64, class_weight={0: 1, 1: 1},
naive_threshold=naive_threshold, seq_length=seq_length)