-
Notifications
You must be signed in to change notification settings - Fork 15
/
nd_aggregation.py
185 lines (157 loc) · 7.43 KB
/
nd_aggregation.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
import mxnet as mx
from mxnet import nd, autograd, gluon
import numpy as np
from copy import deepcopy
import time
from sklearn.metrics import roc_auc_score
def simple_mean(old_gradients, param_list, net, lr, b=0, hvp=None):
if hvp is not None:
pred_grad = []
distance = []
for i in range(len(old_gradients)):
pred_grad.append(old_gradients[i] + hvp)
#distance.append((1 - nd.dot(pred_grad[i].T, param_list[i]) / (
#nd.norm(pred_grad[i]) * nd.norm(param_list[i]))).asnumpy().item())
pred = np.zeros(100)
pred[:b] = 1
distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc1 = roc_auc_score(pred, distance)
distance = nd.norm((nd.concat(*pred_grad, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc2 = roc_auc_score(pred, distance)
print("Detection AUC: %0.4f; Detection AUC: %0.4f" % (auc1, auc2))
#distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
#distance = nd.norm(nd.concat(*param_list, dim=1), axis=0).asnumpy()
# normalize distance
distance = distance / np.sum(distance)
else:
distance = None
mean_nd = nd.mean(nd.concat(*param_list, dim=1), axis=-1, keepdims=1)
idx = 0
for j, (param) in enumerate(net.collect_params().values()):
if param.grad_req == 'null':
continue
param.set_data(param.data() - lr * mean_nd[idx:(idx + param.data().size)].reshape(param.data().shape))
idx += param.data().size
return mean_nd, distance
# trimmed mean
def trim(old_gradients, param_list, net, lr, b=0, hvp=None):
'''
gradients: the list of gradients computed by the worker devices
net: the global model
lr: learning rate
byz: attack
f: number of compromised worker devices
b: trim parameter
'''
if hvp is not None:
pred_grad = []
for i in range(len(old_gradients)):
pred_grad.append(old_gradients[i] + hvp)
pred = np.zeros(100)
pred[:b] = 1
distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc1 = roc_auc_score(pred, distance)
distance = nd.norm((nd.concat(*pred_grad, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc2 = roc_auc_score(pred, distance)
print("Detection AUC: %0.4f; Detection AUC: %0.4f" % (auc1, auc2))
#distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
#distance = nd.norm(nd.concat(*param_list, dim=1), axis=0).asnumpy()
# normalize distance
distance = distance / np.sum(distance)
else:
distance = None
# sort
sorted_array = nd.array(np.sort(nd.concat(*param_list, dim=1).asnumpy(), axis=-1), ctx=mx.gpu(5))
#sorted_array = nd.sort(nd.concat(*param_list, dim=1), axis=-1)
# trim
n = len(param_list)
m = n - b * 2
trim_nd = nd.mean(sorted_array[:, b:(b + m)], axis=-1, keepdims=1)
# update global model
idx = 0
for j, (param) in enumerate(net.collect_params().values()):
if param.grad_req == 'null':
continue
param.set_data(param.data() - lr * trim_nd[idx:(idx + param.data().size)].reshape(param.data().shape))
idx += param.data().size
return trim_nd, distance
def median(old_gradients, param_list, net, lr, b=0, hvp=None):
if hvp is not None:
pred_grad = []
distance = []
for i in range(len(old_gradients)):
pred_grad.append(old_gradients[i] + hvp)
#distance.append((1 - nd.dot(pred_grad[i].T, param_list[i]) / (
#nd.norm(pred_grad[i]) * nd.norm(param_list[i]))).asnumpy().item())
pred = np.zeros(100)
pred[:b] = 1
distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc1 = roc_auc_score(pred, distance)
distance = nd.norm((nd.concat(*pred_grad, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc2 = roc_auc_score(pred, distance)
print("Detection AUC: %0.4f; Detection AUC: %0.4f" % (auc1, auc2))
#distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
#distance = nd.norm(nd.concat(*param_list, dim=1), axis=0).asnumpy()
# normalize distance
distance = distance / np.sum(distance)
else:
distance = None
if len(param_list) % 2 == 1:
median_nd = nd.concat(*param_list, dim=1).sort(axis=-1)[:, len(param_list) // 2]
else:
median_nd = nd.concat(*param_list, dim=1).sort(axis=-1)[:, len(param_list) // 2: len(param_list) // 2 + 1].mean(axis=-1, keepdims=1)
idx = 0
for j, (param) in enumerate(net.collect_params().values()):
if param.grad_req == 'null':
continue
param.set_data(param.data() - lr * median_nd[idx:(idx + param.data().size)].reshape(param.data().shape))
idx += param.data().size
return median_nd, distance
def score(gradient, v, f):
num_neighbours = v.shape[1] - 2 - f
sorted_distance = nd.square(v - gradient).sum(axis=0).sort()
return nd.sum(sorted_distance[1:(1+num_neighbours)]).asscalar()
def nearest_distance(gradient, c_p):
sorted_distance = nd.square(c_p - gradient).sum(axis=1).sort(axis=0)
return sorted_distance[1].asscalar()
def krum(old_gradients, param_list, net, lr, b=0, hvp=None):
if hvp is not None:
pred_grad = []
distance = []
for i in range(len(old_gradients)):
pred_grad.append(old_gradients[i] + hvp)
#distance.append((1 - nd.dot(pred_grad[i].T, param_list[i]) / (
#nd.norm(pred_grad[i]) * nd.norm(param_list[i]))).asnumpy().item())
pred = np.zeros(100)
pred[:b] = 1
distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc1 = roc_auc_score(pred, distance)
distance = nd.norm((nd.concat(*pred_grad, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
auc2 = roc_auc_score(pred, distance)
print("Detection AUC: %0.4f; Detection AUC: %0.4f" % (auc1, auc2))
#distance = nd.norm((nd.concat(*old_gradients, dim=1) - nd.concat(*param_list, dim=1)), axis=0).asnumpy()
#distance = nd.norm(nd.concat(*param_list, dim=1), axis=0).asnumpy()
# normalize distance
distance = distance / np.sum(distance)
else:
distance = None
num_params = len(param_list)
q = b
if num_params <= 2:
# if there are too few clients, randomly pick one as Krum aggregation result
random_idx = np.random.choice(num_params)
krum_nd = nd.reshape(param_list[random_idx], shape=(-1, 1))
else:
if num_params - b - 2 <= 0:
q = num_params-3
v = nd.concat(*param_list, dim=1)
scores = nd.array([score(gradient, v, q) for gradient in param_list])
min_idx = int(scores.argmin(axis=0).asscalar())
krum_nd = nd.reshape(param_list[min_idx], shape=(-1, 1))
idx = 0
for j, (param) in enumerate(net.collect_params().values()):
if param.grad_req == 'null':
continue
param.set_data(param.data() - lr * krum_nd[idx:(idx+param.data().size)].reshape(param.data().shape))
idx += param.data().size
return krum_nd, distance