-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFedGMA.py
328 lines (247 loc) · 10.6 KB
/
FedGMA.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
"""
Name: FedGMA.py
Aim: To test the hyper-parameters used in GRADIENT-MASKED FEDERATED OPTIMIZATION
Author: Siddarth C
Date: September, 2021
"""
# Import required libraries
import os
import shutil
from scipy.signal import savgol_filter
import numpy as np
import matplotlib.pyplot as plt
import glob
import warnings
warnings.filterwarnings("ignore")
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
# Setting random seed for reproducability
import random
random.seed(7)
# Use CUDA if available
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if os.path.exists('Output'):
shutil.rmtree('Output')
print('Deleted exisitng *Output* folder')
os.mkdir('Output')
os.mkdir('Output/P')
os.mkdir('Output/E')
print('Created *Output* and sub folders')
# Load train data
trainx = []
trainy = []
# Load test data
for folder in glob.glob('ClientData/*'):
x = np.load(folder + '/x.npy')
y = np.load(folder + '/y.npy')
trainx.append(torch.FloatTensor(x))
trainy.append(torch.FloatTensor(y))
trainx = torch.stack(trainx).to(device)
trainy = torch.stack(trainy).to(device)
# Load test data
testx = torch.FloatTensor(np.load('TestData/x.npy')).to(device)
testy = torch.FloatTensor(np.load('TestData/y.npy')).to(device)
# Define the classifier - MNISTtier
class MNISTtier(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 32, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 5)
self.fc1 = nn.Linear(1024, 512)
self.fc2 = nn.Linear(512, 1)
def forward(self, x):
x = self.pool(self.conv1(x))
x = self.pool(self.conv2(x))
x = torch.flatten(x, 1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x.squeeze()
def GenerateModels():
"""
Returns client and global model
Args:
None
Returns:
100 client models, Global model
"""
client_models = [MNISTtier().to(device) for i in range(100)]
global_model = MNISTtier().to(device)
return client_models, global_model
def FedAvg(train_x, train_y, test_x, test_y, GD = 'Full', epochs = 3):
"""
Performs FedAvg to train a global model
Args:
train_x: Training data of shape [#clients, #samples, 28, 28, 3]
train_y: Training labels of shape [#clients, #samples]
test_x: Testing data of shape [#clients, #samples, 28, 28, 3]
test_y: Testing labels of shape [#clients, #samples]
GD: Type of gradient Descent: Full(Batch), SGD (Default: 'Full')
epochs: Number of local client epochs (Default: 3)
Returns:
test_loss: Communication round wise test loss
test_acc: Communication round wise test accuracy
"""
client_models, global_model = GenerateModels()
for client_model in client_models:
client_model.load_state_dict(global_model.state_dict())
zero_client = MNISTtier().to(device)
for p in zero_client.parameters():
p.data = p.data * 0
comm_rounds = 50
no_clients = 100
test_loss = []
test_acc = []
for cr in range(comm_rounds):
for ci in range(no_clients):
optimizer = optim.Adam(client_models[ci].parameters(), lr = 0.001)
criterion = nn.BCEWithLogitsLoss()
x_600 = train_x[ci]
y_600 = train_y[ci]
for e in range(epochs):
if GD == 'SGD':
for x, y in zip(x_600, y_600):
optimizer.zero_grad()
pred = client_models[ci](x.reshape((1, 3, 28, 28)))
loss = criterion(pred, y)
loss.backward()
optimizer.step()
else:
optimizer.zero_grad()
pred = client_models[ci](x_600.reshape((600, 3, 28, 28)))
loss = criterion(pred, y_600)
loss.backward()
optimizer.step()
global_model.load_state_dict(zero_client.state_dict())
for ind in range(no_clients):
for p1, p2 in zip(global_model.parameters(), client_models[ind].parameters()):
p1.data = p1.data + p2.data
for p in global_model.parameters():
p.data = p.data / no_clients
for client_model in client_models:
client_model.load_state_dict(global_model.state_dict())
pred = global_model(test_x.reshape((10000, 3, 28, 28)))
loss = criterion(pred, test_y)
pred = torch.round(F.sigmoid(pred))
acc = sum(pred == test_y) / len(pred)
test_loss.append(loss.cpu().detach().numpy())
test_acc.append(acc.cpu().detach().numpy())
if cr % 20 == 0:
print('Communication Round:', cr, ' Loss:', np.round(loss.cpu().detach().numpy(), 4), ' Acc:', np.round(acc.cpu().detach().numpy(), 4))
return test_loss, test_acc
FedAvg_loss, FedAvg_acc = FedAvg(trainx, trainy, testx, testy)
np.save('Output/P/FedAvg_Acc.npy' , np.array(savgol_filter(FedAvg_acc, 9, 4)))
np.save('Output/P/FedAvg_Loss.npy' , np.array(savgol_filter(FedAvg_loss, 9, 4)))
np.save('Output/E/FedAvg_Acc_3.npy' , np.array(savgol_filter(FedAvg_acc, 9, 4)))
np.save('Output/E/FedAvg_Loss_3.npy' , np.array(savgol_filter(FedAvg_loss, 9, 4)))
def FedGMA(train_x, train_y, test_x, test_y, p_thresh = 0.8, GD = 'Full', epochs = 3):
"""
Performs FedGMA to train a global model
Args:
train_x: Training data of shape [#clients, #samples, 28, 28, 3]
train_y: Training labels of shape [#clients, #samples]
test_x: Testing data of shape [#clients, #samples, 28, 28, 3]
test_y: Testing labels of shape [#clients, #samples]
p_thresh: AND mask threshold (default: 0.8)
GD: Type of gradient Descent: Full(Batch), SGD (Default: 'Full')
epochs: Number of local client epochs (Default: 3)
Returns:
test_loss: Communication round wise test loss
test_acc: Communication round wise test accuracy
"""
client_models, global_model = GenerateModels()
for client_model in client_models:
client_model.load_state_dict(global_model.state_dict())
zero_client = MNISTtier().to(device)
sign_counter = MNISTtier().to(device)
for p in zero_client.parameters():
p.data = p.data * 0
comm_rounds = 50
no_clients = 100
server_lr = 0.0001
test_loss = []
test_acc = []
for cr in range(comm_rounds):
sign_counter.load_state_dict(zero_client.state_dict())
for ci in range(no_clients):
optimizer = optim.Adam(client_models[ci].parameters(), lr = 0.001)
criterion = nn.BCEWithLogitsLoss()
x_600 = train_x[ci]
y_600 = train_y[ci]
for e in range(epochs):
if GD == 'SGD':
for x, y in zip(x_600, y_600):
optimizer.zero_grad()
pred = client_models[ci](x.reshape((1, 3, 28, 28)))
loss = criterion(pred, y)
loss.backward()
optimizer.step()
else:
optimizer.zero_grad()
pred = client_models[ci](x_600.reshape((600, 3, 28, 28)))
loss = criterion(pred, y_600)
loss.backward()
optimizer.step()
global_model.load_state_dict(zero_client.state_dict())
for ind in range(no_clients):
for p1, p2, p3 in zip(global_model.parameters(), client_models[ind].parameters(), sign_counter.parameters()):
p2_grad_sign = torch.sign(p2.grad)
p3.data += p2_grad_sign
p1.data = p1.data + p2.data
for p in global_model.parameters():
p.data = p.data / no_clients
for ind in range(no_clients):
for p1, p2, p3 in zip(global_model.parameters(), client_models[ind].parameters(), sign_counter.parameters()):
p2_mask = 1 * (p2.grad > 0)
p3_mask = 1 * (p3.data > 0)
final_mask = torch.logical_and(torch.logical_not(torch.logical_xor(p2_mask, p3_mask)), 1 * (torch.abs(p3.data) > p_thresh * no_clients))
new_grad = p2.grad * final_mask
p1.data -= (server_lr * new_grad/no_clients)
for client_model in client_models:
client_model.load_state_dict(global_model.state_dict())
pred = global_model(test_x.reshape((10000, 3, 28, 28)))
loss = criterion(pred, test_y)
pred = torch.round(F.sigmoid(pred))
acc = sum(pred == test_y) / len(pred)
test_loss.append(loss.cpu().detach().numpy())
test_acc.append(acc.cpu().detach().numpy())
if cr % 20 == 0:
print('Communication Round:', cr, ' Loss:', np.round(loss.cpu().detach().numpy(), 4), ' Acc:', np.round(acc.cpu().detach().numpy(), 4))
return test_loss, test_acc
print('FedGMA - Testing of hyperparmeter - P - Threshold of number of client gradients to be consistent')
for p in np.arange(0.5, 1, 0.1):
p = np.round(p, 1)
FedGMA_loss, FedGMA_acc = FedGMA(trainx, trainy, testx, testy, p_thresh = p)
np.save('Output/P/FedGMA_Acc_' + str(p) + '.npy', np.array(savgol_filter(FedGMA_acc, 9, 4)))
np.save('Output/P/FedGMA_Loss_' + str(p) + '.npy', np.array(savgol_filter(FedGMA_loss, 9, 4)))
print('Probability threshold', p, 'done \n', '-' * 5)
print('')
print('FedGMA - Testing of hyperparmeter - Local Client Epochs')
for epochs in [1, 3, 5, 7, 9]:
FedGMA_loss, FedGMA_acc = FedGMA(trainx, trainy, testx, testy, p_thresh = 0.7, epochs = epochs)
np.save('Output/E/FedGMA_Acc_' + str(epochs) + '.npy', np.array(savgol_filter(FedGMA_acc, 9, 4)))
np.save('Output/E/FedGMA_Loss_' + str(epochs) + '.npy', np.array(savgol_filter(FedGMA_loss, 9, 4)))
print('Local Epochs', epochs, 'done \n', '-' * 5)
acc = []
acc_names = []
loss = []
loss_names = []
hyper_parameter = 'P' # or 'E'
# Please do modify the following part if the graphs are not plotted
for fname in glob.glob('Output/' + hyper_parameter + '/*.npy'):
ar = np.load(fname)
if fname.split('/')[-1].split('_')[1] == 'Loss':
loss.append(ar)
loss_names.append(fname.split('\\')[-1].split('.npy')[0])
else:
acc.append(ar)
acc_names.append(fname.split('\\')[-1].split('.npy')[0])
plt.rcParams["figure.figsize"] = (15,15)
for a in acc:
plt.plot(a)
plt.xlabel('Communication Rounds')
plt.ylabel('Test Accuracy')
plt.legend(acc_names)
plt.show()