-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaggregation.py
452 lines (388 loc) · 15.5 KB
/
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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import copy
import enum
import torch
import numpy as np
import math
from scipy import stats
from functools import reduce
import time
import sklearn.metrics.pairwise as smp
import hdbscan
from scipy.spatial.distance import cdist
from scipy.stats import entropy
from sklearn.cluster import KMeans
from utils import *
def get_pca(data, threshold = 0.99):
normalized_data = StandardScaler().fit_transform(data)
pca = PCA()
reduced_data = pca.fit_transform(normalized_data)
# Determine explained variance using explained_variance_ration_ attribute
exp_var = pca.explained_variance_ratio_
cum_sum_eigenvalues = np.cumsum(exp_var)
select_pcas = np.where(cum_sum_eigenvalues <=threshold)[0]
# print('Number of components with variance <= {:0.0f}%: {}'.format(threshold*100, len(select_pcas)))
reduced_data = reduced_data[:, select_pcas]
return reduced_data
eps = np.finfo(float).eps
class LFD():
def __init__(self, num_classes):
self.memory = np.zeros([num_classes])
def clusters_dissimilarity(self, clusters):
n0 = len(clusters[0])
n1 = len(clusters[1])
m = n0 + n1
cs0 = smp.cosine_similarity(clusters[0]) - np.eye(n0)
cs1 = smp.cosine_similarity(clusters[1]) - np.eye(n1)
mincs0 = np.min(cs0, axis=1)
mincs1 = np.min(cs1, axis=1)
ds0 = n0/m * (1 - np.mean(mincs0))
ds1 = n1/m * (1 - np.mean(mincs1))
return ds0, ds1
def aggregate(self, global_model, local_models, ptypes):
local_weights = [copy.deepcopy(model).state_dict() for model in local_models]
m = len(local_models)
for i in range(m):
local_models[i] = list(local_models[i].parameters())
global_model = list(global_model.parameters())
dw = [None for i in range(m)]
db = [None for i in range(m)]
for i in range(m):
dw[i]= global_model[-2].cpu().data.numpy() - \
local_models[i][-2].cpu().data.numpy()
db[i]= global_model[-1].cpu().data.numpy() - \
local_models[i][-1].cpu().data.numpy()
dw = np.asarray(dw)
db = np.asarray(db)
"If one class or two classes classification model"
if len(db[0]) <= 2:
data = []
for i in range(m):
data.append(dw[i].reshape(-1))
kmeans = KMeans(n_clusters=2, random_state=0).fit(data)
labels = kmeans.labels_
clusters = {0:[], 1:[]}
for i, l in enumerate(labels):
clusters[l].append(data[i])
good_cl = 0
cs0, cs1 = self.clusters_dissimilarity(clusters)
if cs0 < cs1:
good_cl = 1
# print('Cluster 0 weighted variance', cs0)
# print('Cluster 1 weighted variance', cs1)
# print('Potential good cluster is:', good_cl)
scores = np.ones([m])
for i, l in enumerate(labels):
# print(ptypes[i], 'Cluster:', l)
if l != good_cl:
scores[i] = 0
global_weights = average_weights(local_weights, scores)
return global_weights
"For multiclassification models"
norms = np.linalg.norm(dw, axis = -1)
self.memory = np.sum(norms, axis = 0)
self.memory +=np.sum(abs(db), axis = 0)
max_two_freq_classes = self.memory.argsort()[-2:]
print('Potential source and target classes:', max_two_freq_classes)
data = []
for i in range(m):
data.append(dw[i][max_two_freq_classes].reshape(-1))
kmeans = KMeans(n_clusters=2, random_state=0).fit(data)
labels = kmeans.labels_
clusters = {0:[], 1:[]}
for i, l in enumerate(labels):
clusters[l].append(data[i])
good_cl = 0
cs0, cs1 = self.clusters_dissimilarity(clusters)
if cs0 < cs1:
good_cl = 1
# print('Cluster 0 weighted variance', cs0)
# print('Cluster 1 weighted variance', cs1)
# print('Potential good cluster is:', good_cl)
scores = np.ones([m])
for i, l in enumerate(labels):
# print(ptypes[i], 'Cluster:', l)
if l != good_cl:
scores[i] = 0
global_weights = average_weights(local_weights, scores)
return global_weights
################################################
# Takes in grad
# Compute similarity
# Get weightings
def foolsgold(grads):
n_clients = grads.shape[0]
cs = smp.cosine_similarity(grads) - np.eye(n_clients)
maxcs = np.max(cs, axis=1)
# pardoning
for i in range(n_clients):
for j in range(n_clients):
if i == j:
continue
if maxcs[i] < maxcs[j]:
cs[i][j] = cs[i][j] * maxcs[i] / maxcs[j]
wv = 1 - (np.max(cs, axis=1))
wv[wv > 1] = 1
wv[wv < 0] = 0
# Rescale so that max value is wv
wv = wv / np.max(wv)
wv[(wv == 1)] = .99
# Logit function
wv = (np.log(wv / (1 - wv)) + 0.5)
wv[(np.isinf(wv) + wv > 1)] = 1
wv[(wv < 0)] = 0
return wv
class FoolsGold:
def __init__(self, num_peers):
self.memory = None
self.wv_history = []
self.num_peers = num_peers
def score_gradients(self, local_grads, selectec_peers):
m = len(local_grads)
grad_len = np.array(local_grads[0][-2].cpu().data.numpy().shape).prod()
if self.memory is None:
self.memory = np.zeros((self.num_peers, grad_len))
grads = np.zeros((m, grad_len))
for i in range(m):
grads[i] = np.reshape(local_grads[i][-2].cpu().data.numpy(), (grad_len))
self.memory[selectec_peers]+= grads
wv = foolsgold(self.memory) # Use FG
self.wv_history.append(wv)
return wv[selectec_peers]
#######################################################################################
class Tolpegin:
def __init__(self):
pass
def score(self, global_model, local_models, peers_types, selected_peers):
global_model = list(global_model.parameters())
last_g = global_model[-2].cpu().data.numpy()
m = len(local_models)
grads = [None for i in range(m)]
for i in range(m):
grad= (last_g - \
list(local_models[i].parameters())[-2].cpu().data.numpy())
grads[i] = grad
grads = np.array(grads)
num_classes = grad.shape[0]
# print('Number of classes:', num_classes)
dist = [ ]
labels = [ ]
for c in range(num_classes):
data = grads[:, c]
data = get_pca(copy.deepcopy(data))
kmeans = KMeans(n_clusters=2, random_state=0).fit(data)
cl = kmeans.cluster_centers_
dist.append(((cl[0] - cl[1])**2).sum())
labels.append(kmeans.labels_)
dist = np.array(dist)
candidate_class = dist.argmax()
print("Candidate source/target class", candidate_class)
labels = labels[candidate_class]
if sum(labels) < m/2:
scores = 1 - labels
else:
scores = labels
for i, pt in enumerate(peers_types):
print(pt, 'scored', scores[i])
return scores
#################################################################################################################
# Clip local updates
def clipp_model(g_w, w, gamma = 1):
for layer in w.keys():
w[layer] = g_w[layer] + (w[layer] - g_w[layer])*min(1, gamma)
return w
def FLAME(global_model, local_models, noise_scalar):
# Compute number of local models
m = len(local_models)
# Flattent local models
g_m = np.array([torch.nn.utils.parameters_to_vector(global_model.parameters()).cpu().data.numpy()])
f_m = np.array([torch.nn.utils.parameters_to_vector(model.parameters()).cpu().data.numpy() for model in local_models])
grads = g_m - f_m
# Compute model-wise cosine similarity
cs = smp.cosine_similarity(grads)
# Compute the minimum cluster size value
msc = int(m*0.5) + 1
# Apply HDBSCAN on the computed cosine similarities
clusterer = hdbscan.HDBSCAN(min_cluster_size=msc, min_samples=1, allow_single_cluster = True)
clusterer.fit(cs)
labels = clusterer.labels_
# print('Clusters:', labels)
if sum(labels) == -(m):
# In case all of the local models identified as outliers, consider all of as benign
benign_idxs = np.arange(m)
else:
benign_idxs = np.where(labels!=-1)[0]
# Compute euclidean distances to the current global model
euc_d = cdist(g_m, f_m)[0]
# Identify the median of computed distances
st = np.median(euc_d)
# Clipp admitted updates
W_c = []
for i, idx in enumerate(benign_idxs):
w_c = clipp_model(global_model.state_dict(), local_models[idx].state_dict(), gamma = st/euc_d[idx])
W_c.append(w_c)
# Average admitted clipped updates to obtain a new global model
g_w = average_weights(W_c, np.ones(len(W_c)))
'''From the original paper: {We use standard DP parameters and set eps = 3705 for IC,
eps = 395 for the NIDS and eps = 4191 for the NLP scenario.
Accordingly, lambda = 0.001 for IC and NLP, and lambda = 0.01 for the NIDS scenario.}
However, we found lambda = 0.001 with the CIFAR10-ResNet18 benchmark spoils the model
and therefore we tried lower lambda values, which correspond to greater eps values.'''
# Add adaptive noise to the global model
lamb = 0.001
sigma = lamb*st*noise_scalar
# print('Sigma:{:0.4f}'.format(sigma))
for key in g_w.keys():
noise = torch.FloatTensor(g_w[key].shape).normal_(mean=0, std=(sigma**2)).to(g_w[key].device)
g_w[key] = g_w[key] + noise
return g_w
#################################################################################################################
def median_opt(input):
shape = input.shape
input = input.sort()[0]
if shape[-1] % 2 != 0:
output = input[..., int((shape[-1] - 1) / 2)]
else:
output = (input[..., int(shape[-1] / 2 - 1)] + input[..., int(shape[-1] / 2)]) / 2.0
return output
def Repeated_Median_Shard(w):
SHARD_SIZE = 100000
w_med = copy.deepcopy(w[0])
device = w[0][list(w[0].keys())[0]].device
for k in w_med.keys():
shape = w_med[k].shape
if len(shape) == 0:
continue
total_num = reduce(lambda x, y: x * y, shape)
y_list = torch.FloatTensor(len(w), total_num).to(device)
for i in range(len(w)):
y_list[i] = torch.reshape(w[i][k], (-1,))
y = torch.t(y_list)
if total_num < SHARD_SIZE:
slopes, intercepts = repeated_median(y)
y = intercepts + slopes * (len(w) - 1) / 2.0
else:
y_result = torch.FloatTensor(total_num).to(device)
assert total_num == y.shape[0]
num_shards = int(math.ceil(total_num / SHARD_SIZE))
for i in range(num_shards):
y_shard = y[i * SHARD_SIZE: (i + 1) * SHARD_SIZE, ...]
slopes_shard, intercepts_shard = repeated_median(y_shard)
y_shard = intercepts_shard + slopes_shard * (len(w) - 1) / 2.0
y_result[i * SHARD_SIZE: (i + 1) * SHARD_SIZE] = y_shard
y = y_result
y = y.reshape(shape)
w_med[k] = y
return w_med
def repeated_median(y):
num_models = y.shape[1]
total_num = y.shape[0]
y = y.sort()[0]
yyj = y.repeat(1, 1, num_models).reshape(total_num, num_models, num_models)
yyi = yyj.transpose(-1, -2)
xx = torch.FloatTensor(range(num_models)).to(y.device)
xxj = xx.repeat(total_num, num_models, 1)
xxi = xxj.transpose(-1, -2) + eps
diag = torch.Tensor([float('Inf')] * num_models).to(y.device)
diag = torch.diag(diag).repeat(total_num, 1, 1)
dividor = xxi - xxj + diag
slopes = (yyi - yyj) / dividor + diag
slopes, _ = slopes.sort()
slopes = median_opt(slopes[:, :, :-1])
slopes = median_opt(slopes)
# get intercepts (intercept of median)
yy_median = median_opt(y)
xx_median = [(num_models - 1) / 2.0] * total_num
xx_median = torch.Tensor(xx_median).to(y.device)
intercepts = yy_median - slopes * xx_median
return slopes, intercepts
# Repeated Median estimator
def Repeated_Median(w):
cur_time = time.time()
w_med = copy.deepcopy(w[0])
device = w[0][list(w[0].keys())[0]].device
for k in w_med.keys():
shape = w_med[k].shape
if len(shape) == 0:
continue
total_num = reduce(lambda x, y: x * y, shape)
y_list = torch.FloatTensor(len(w), total_num).to(device)
for i in range(len(w)):
y_list[i] = torch.reshape(w[i][k], (-1,))
y = torch.t(y_list)
slopes, intercepts = repeated_median(y)
y = intercepts + slopes * (len(w) - 1) / 2.0
y = y.reshape(shape)
w_med[k] = y
print('repeated median aggregation took {}s'.format(time.time() - cur_time))
return w_med
# simple median estimator
def simple_median(w):
device = w[0][list(w[0].keys())[0]].device
w_med = copy.deepcopy(w[0])
for k in w_med.keys():
shape = w_med[k].shape
if len(shape) == 0:
continue
total_num = reduce(lambda x, y: x * y, shape)
y_list = torch.FloatTensor(len(w), total_num).to(device)
for i in range(len(w)):
y_list[i] = torch.reshape(w[i][k], (-1,))
y = torch.t(y_list)
median_result = median_opt(y)
assert total_num == len(median_result)
weight = torch.reshape(median_result, shape)
w_med[k] = weight
return w_med
def trimmed_mean(w, trim_ratio):
if trim_ratio == 0:
return average_weights(w, [1 for i in range(len(w))])
assert trim_ratio < 0.5, 'trim ratio is {}, but it should be less than 0.5'.format(trim_ratio)
trim_num = int(trim_ratio * len(w))
device = w[0][list(w[0].keys())[0]].device
w_med = copy.deepcopy(w[0])
for k in w_med.keys():
shape = w_med[k].shape
if len(shape) == 0:
continue
total_num = reduce(lambda x, y: x * y, shape)
y_list = torch.FloatTensor(len(w), total_num).to(device)
for i in range(len(w)):
y_list[i] = torch.reshape(w[i][k], (-1,))
y = torch.t(y_list)
y_sorted = y.sort()[0]
result = y_sorted[:, trim_num:-trim_num]
result = result.mean(dim=-1)
assert total_num == len(result)
weight = torch.reshape(result, shape)
w_med[k] = weight
return w_med
# Get average weights
def average_weights(w, marks):
"""
Returns the average of the weights.
"""
w_avg = copy.deepcopy(w[0])
for key in w_avg.keys():
w_avg[key] = w_avg[key] * marks[0]
for key in w_avg.keys():
for i in range(1, len(w)):
w_avg[key] += w[i][key] * marks[i]
w_avg[key] = w_avg[key] *(1/sum(marks))
return w_avg
def Krum(updates, f, multi = False):
n = len(updates)
updates = [torch.nn.utils.parameters_to_vector(update.parameters()) for update in updates]
updates_ = torch.empty([n, len(updates[0])])
for i in range(n):
updates_[i] = updates[i]
k = n - f - 2
# collection distance, distance from points to points
cdist = torch.cdist(updates_, updates_, p=2)
dist, idxs = torch.topk(cdist, k , largest=False)
dist = dist.sum(1)
idxs = dist.argsort()
if multi:
return idxs[:k]
else:
return idxs[0]
##################################################################