-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbfi_evaluation.py
327 lines (283 loc) · 13 KB
/
bfi_evaluation.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
import numpy as np
from sklearn.metrics import accuracy_score, precision_score, recall_score, precision_recall_curve, average_precision_score
def get_nr_child_idx(clf):
n_nodes = clf.tree_.node_count
children_left = clf.tree_.children_left
children_right = clf.tree_.children_right
feature = clf.tree_.feature
threshold = clf.tree_.threshold
n_leaves_h = 0
node_depth = np.zeros(shape=n_nodes, dtype=np.int64)
is_leaves = np.zeros(shape=n_nodes, dtype=bool)
stack = [(0, 0)] # start with the root node id (0) and its depth (0)
while len(stack) > 0:
# `pop` ensures each node is only visited once
node_id, depth = stack.pop()
node_depth[node_id] = depth
# If the left and right child of a node is not the same we have a split
# node
is_split_node = children_left[node_id] != children_right[node_id]
# If a split node, append left and right children and depth to `stack`
# so we can loop through them
if is_split_node:
stack.append((children_left[node_id], depth + 1))
stack.append((children_right[node_id], depth + 1))
else:
is_leaves[node_id] = True
# print("The binary tree structure has {n} nodes and has "
# "the following tree structure:\n".format(n=n_nodes))
for i in range(n_nodes):
if is_leaves[i]:
# print("{space}node={node} is a leaf node.".format(
# space=node_depth[i] * "\t", node=i))
n_leaves_h += 1
#else:
# print("{space}node={node} is a split node: "
# "go to node {left} if X[:, {feature}] <= {threshold} "
# "else to node {right}.".format(
# space=node_depth[i] * "\t",
# node=i,
# left=children_left[i],
# feature=feature[i],
# threshold=threshold[i],
# right=children_right[i]))
return n_nodes - n_leaves_h
def bfi_tree(exp_dict):
# write experiment data to file
nr_features = exp_dict["X_train"].shape[1]
# get data from dictionary
tree = exp_dict["model"]
X_test = exp_dict["X_test"]
y_test = exp_dict["y_test"]
reps = exp_dict["reps"]
bers = exp_dict["bers"]
dataset_name = exp_dict["dataset_name"]
export_accuracy = exp_dict["export_accuracy"]
depth = exp_dict["depth"]
# split
split_inj = exp_dict["split_inj"]
int_split = exp_dict["int_split"]
nr_bits_split = exp_dict["nr_bits_split"]
# feature
feature_inj = exp_dict["feature_inj"]
feature_idx_inj = exp_dict["feature_idx_inj"]
# child
child_idx_inj = exp_dict["child_idx_inj"]
exp_path = exp_dict["experiment_path"]
# exp_data = exp_dict["experiment_data"]
exp_data = open(exp_path + "/results.txt", "a")
exp_data.write("--- BER TEST ---\n")
estims = exp_dict["estims"]
depth = exp_dict["depth"]
exp_data.write("(Summary) trees: {}, depth: {}, reps: {}, dataset: {}\n".format(estims, depth, reps, dataset_name))
# exp_data.close()
accuracy_all = []
for ber in bers:
exp_data = open(exp_path + "/results.txt", "a")
# reset configs
tree.tree_.bit_flip_injection_split = 0
tree.tree_.bit_flip_injection_featval = 0
tree.tree_.bit_flip_injection_featidx = 0
tree.tree_.bit_flip_injection_chidx = 0
# split value injection
if split_inj == 1:
tree.tree_.bit_error_rate_split = ber
tree.tree_.bit_flip_injection_split = split_inj
tree.tree_.int_rounding_for_thresholds = int_split
tree.tree_.int_threshold_bits = nr_bits_split
# feature value injection
if feature_inj == 1:
tree.tree_.bit_error_rate_featval = ber
tree.tree_.bit_flip_injection_featval = feature_inj
# tree.tree_.bit_flip_injection_featval_floatInt = int_featval
# tree.tree_.nr_feature_val = int_featval
# feature index injection
if feature_idx_inj == 1:
tree.tree_.bit_error_rate_featidx = ber
tree.tree_.bit_flip_injection_featidx = 1
tree.tree_.nr_feature_idx = np.floor(np.log2(nr_features)) + 1
# child indices injection
if child_idx_inj == 1:
# TODO: execute once before bet experiments
nr_ch_idx = get_nr_child_idx(tree)
nr_ch_idx *= 2
tree.tree_.nr_child_idx = np.floor(np.log2(nr_ch_idx)) + 1
tree.tree_.bit_error_rate_chidx = ber
tree.tree_.bit_flip_injection_chidx = 1
acc_scores = []
for rep in range(reps):
out = tree.predict(X_test)
acc_scores.append(accuracy_score(y_test, out))
acc_scores_np = np.array(acc_scores)
# print("ACC scores", acc_scores_np)
acc_mean = np.mean(acc_scores_np)
# print("means:", acc_mean)
acc_min = np.min(acc_scores_np)
acc_max = np.max(acc_scores_np)
# print("BER: {:.4f}, Accuracy: {:.4f} ({:.4f},{:.4f})".format(ber, acc_mean, acc_mean - acc_min, acc_max - acc_mean))
# exp_data = open(exp_path + "/results.txt", "a")
exp_data.write("{:.8f} {:.4f} {:.4f} {:.4f}\n".format(ber, (acc_mean), (acc_max - acc_mean), (acc_mean - acc_min)))
if export_accuracy is not None:
accuracy_all.append(acc_scores)
# dump exp data for each error rate
# if export_accuracy is not None:
# filename = "ber_{}.npy".format(ber*100)
# with open(filename, 'wb') as f:
# np.save(f, acc_scores_np)
# exp_data.close()
# print("{:.8f} {:.4f} {:.4f} {:.4f}\n".format(ber*100, (acc_mean)*100, (acc_max - acc_mean)*100, (acc_mean - acc_min)*100))
exp_data.close()
# reset configs
tree.tree_.bit_flip_injection_split = 0
tree.tree_.bit_flip_injection_featval = 0
tree.tree_.bit_flip_injection_featidx = 0
tree.tree_.bit_flip_injection_chidx = 0
# TODO: export accuracy in a certain format
if export_accuracy is not None:
dim_1_all = []
for idx, acc in enumerate(accuracy_all):
dim_1 = [bers[idx] for x in range(reps)]
dim_1_all.append(dim_1)
bers_flattened = np.array(dim_1_all).flatten()
accuracy_all_flattened = np.array(accuracy_all).flatten()
stacked = np.stack((bers_flattened, accuracy_all_flattened))
filename = exp_path + f"/acc_over_ber_DT{depth}_{dataset_name}_reps_{reps}.npy"
with open(filename, 'wb') as f:
np.save(f, stacked)
def bfi_forest(exp_dict):
# write experiment data to file
nr_features = exp_dict["X_train"].shape[1]
# get data from dictionary
clf = exp_dict["model"]
X_test = exp_dict["X_test"]
# X_test = X_test[0].reshape(1, -1)
y_test = exp_dict["y_test"]
reps = exp_dict["reps"]
bers = exp_dict["bers"]
dataset_name = exp_dict["dataset_name"]
export_accuracy = exp_dict["export_accuracy"]
depth = exp_dict["depth"]
estims = exp_dict["estims"]
true_majority = exp_dict["true_majority"]
# split
split_inj = exp_dict["split_inj"]
int_split = exp_dict["int_split"]
nr_bits_split = exp_dict["nr_bits_split"]
# feature
feature_inj = exp_dict["feature_inj"]
feature_idx_inj = exp_dict["feature_idx_inj"]
# child
child_idx_inj = exp_dict["child_idx_inj"]
exp_path = exp_dict["experiment_path"]
# exp_data = exp_dict["experiment_data"]
exp_data = open(exp_path + "/results.txt", "a")
exp_data.write("--- BER TEST ---\n")
estims = exp_dict["estims"]
depth = exp_dict["depth"]
exp_data.write("(Summary) trees: {}, depth: {}, reps: {}, dataset: {}\n".format(estims, depth, reps, dataset_name))
# exp_data.close()
accuracy_all = []
for ber in bers:
exp_data = open(exp_path + "/results.txt", "a")
for tree in clf.estimators_:
# reset configs
tree.tree_.bit_flip_injection_split = 0
tree.tree_.bit_flip_injection_featval = 0
tree.tree_.bit_flip_injection_featidx = 0
tree.tree_.bit_flip_injection_chidx = 0
# split value injection
if split_inj == 1:
tree.tree_.bit_error_rate_split = ber
tree.tree_.bit_flip_injection_split = split_inj
tree.tree_.int_rounding_for_thresholds = int_split
tree.tree_.int_threshold_bits = nr_bits_split
# feature value injection
if feature_inj == 1:
tree.tree_.bit_error_rate_featval = ber
tree.tree_.bit_flip_injection_featval = feature_inj
# tree.tree_.bit_flip_injection_featval_floatInt = int_featval
# tree.tree_.nr_feature_val = int_featval
# feature index injection
if feature_idx_inj == 1:
tree.tree_.bit_error_rate_featidx = ber
tree.tree_.bit_flip_injection_featidx = 1
tree.tree_.nr_feature_idx = np.floor(np.log2(nr_features)) + 1
# child indices injection
if child_idx_inj == 1:
# TODO: execute once before bet experiments
nr_ch_idx = get_nr_child_idx(tree)
nr_ch_idx *= 2
tree.tree_.nr_child_idx = np.floor(np.log2(nr_ch_idx)) + 1
tree.tree_.bit_error_rate_chidx = ber
tree.tree_.bit_flip_injection_chidx = 1
acc_scores = []
for rep in range(reps):
if true_majority == 0:
# weighted majority vote
out = clf.predict(X_test)
acc_scores.append(accuracy_score(y_test, out))
else:
### true maj. vote (for ECML paper)
# for idx in range(len(clf.estimators_)):
store_pred_matrix = []
for idx in range(len(clf.estimators_)):
store_pred = clf.estimators_[idx].predict(X_test)
store_pred_matrix.append(store_pred)
# finished all trees, preds are in store_pred_matrix
store_pred_matrix_np = np.array(store_pred_matrix, dtype=np.uint8)
# majority vote over dim 0
argmax_list = []
for j in range(store_pred_matrix_np.shape[1]):
maj_vote = np.bincount(store_pred_matrix_np[:,j])
argmax = np.argmax(maj_vote)
argmax_list.append(argmax)
# print("shapre", store_pred_matrix_np[:,0].shape)
# print("len", len(argmax_list))
argmax_list_np = np.array(argmax_list)
acc_scores.append(accuracy_score(y_test, argmax_list_np))
# print("predicted label 6: ", argmax_list_np[48])
# print("actual label 6: ", y_test[48])
# print("old", accuracy_score(y_test, out))
# print("new", accuracy_score(y_test, argmax_list_np))
###
# acc_scores_np = np.array(acc_scores)
acc_scores_np = np.array(acc_scores)
# print("ACC scores", acc_scores_np)
acc_mean = np.mean(acc_scores_np)
# print("means:", acc_mean)
acc_min = np.min(acc_scores_np)
acc_max = np.max(acc_scores_np)
# print("BER: {:.4f}, Accuracy: {:.4f} ({:.4f},{:.4f})".format(ber, acc_mean, acc_mean - acc_min, acc_max - acc_mean))
# exp_data = open(exp_path + "/results.txt", "a")
exp_data.write("{:.8f} {:.4f} {:.4f} {:.4f}\n".format(ber, (acc_mean), (acc_max - acc_mean), (acc_mean - acc_min)))
if export_accuracy is not None:
accuracy_all.append(acc_scores)
# dump exp data for each error rate
# if export_accuracy is not None:
# filename = "ber_{}.npy".format(ber*100)
# with open(filename, 'wb') as f:
# np.save(f, acc_scores_np)
# exp_data.close()
# print("{:.8f} {:.4f} {:.4f} {:.4f}\n".format(ber*100, (acc_mean)*100, (acc_max - acc_mean)*100, (acc_mean - acc_min)*100))
exp_data.close()
for tree in clf.estimators_:
# reset configs
tree.tree_.bit_flip_injection_split = 0
tree.tree_.bit_flip_injection_featval = 0
tree.tree_.bit_flip_injection_featidx = 0
tree.tree_.bit_flip_injection_chidx = 0
# TODO: export accuracy in a certain format
if export_accuracy is not None:
dim_1_all = []
for idx, acc in enumerate(accuracy_all):
dim_1 = [bers[idx] for x in range(reps)]
dim_1_all.append(dim_1)
bers_flattened = np.array(dim_1_all).flatten()
accuracy_all_flattened = np.array(accuracy_all).flatten()
stacked = np.stack((bers_flattened, accuracy_all_flattened))
filename = exp_path + f"/acc_over_ber_RF_D{depth}_T{estims}_{dataset_name}_reps_{reps}.npy"
with open(filename, 'wb') as f:
np.save(f, stacked)