-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
400 lines (382 loc) · 13.7 KB
/
main.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
import wandb
import torch
import os
import pandas as pd
import numpy as np
from copy import deepcopy
from initialise import initNetworkData
from algorithms import (
fed_avg_run,
fed_prox_run,
sfedavg_run,
ucb_run,
greedy_shap_run,
power_of_choice_run,
centralised_run,
)
from utils import dict_hash
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def execute_run(clients, server, algorithm, parameters, config_global, seed, wandb_logging=True):
E = config_global["E"]
B = config_global["batches"]
T = config_global["T"]
lr = config_global["lr"]
momentum = config_global["momentum"]
select_fraction = config_global["select_fraction"]
test_acc_arr = []
train_acc_arr = []
train_loss_arr = []
val_loss_arr = []
test_loss_arr = []
shap_arr = []
selections_arr = []
if algorithm == "centralised":
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
) = centralised_run(
clients,
server,
select_fraction,
T,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
elif algorithm == "fedavg":
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
) = fed_avg_run(
clients,
server,
select_fraction,
T,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
elif algorithm == "fedprox":
mu_vals = parameters["mu"]
for mu in mu_vals:
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb_config["mu"] = mu
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
) = fed_prox_run(
clients,
server,
select_fraction,
T,
mu,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
elif algorithm == "sfedavg":
alpha_vals = parameters["alpha"]
for alpha in alpha_vals:
beta = 1 - alpha
# temperature = 1
# alpha_init = 1/clients.length
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb_config["algo_alpha"] = alpha
wandb_config["algo_beta"] = beta
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
shapley_values,
) = sfedavg_run(
clients,
server,
select_fraction,
T,
alpha,
beta,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
temperature=1,
alpha_init=1/len(clients),
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
shap_arr.append(shapley_values)
elif algorithm == "ucb":
beta_vals = parameters["beta"]
for beta in beta_vals:
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb_config["algo_beta"] = beta
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
shapley_values,
sv_rounds,
num_model_evaluations,
ucb_values,
) = ucb_run(
clients,
server,
select_fraction,
T,
beta,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
shap_arr.append(shapley_values)
elif algorithm == "greedyshap":
shap_memory_vals = parameters["memory"]
for shap_memory in shap_memory_vals:
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb_config["memory"] = shap_memory
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
shapley_values,
sv_rounds,
num_model_evaluations,
ucb_values,
) = greedy_shap_run(
clients,
server,
select_fraction,
T,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
shap_memory=shap_memory,
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
shap_arr.append(shapley_values)
elif algorithm == "poc":
decay_factor_vals = parameters["decay_factor"]
for decay_factor in decay_factor_vals:
if wandb_logging:
wandb_config = deepcopy(config_global)
wandb_config["algorithm"] = algorithm
wandb_config["seed"] = seed
wandb_config["decay_factor"] = decay_factor
wandb.init(project=GLOBAL_PROJECT_NAME, config=wandb_config)
(
test_acc,
train_acc,
train_loss,
val_loss,
test_loss,
selections,
) = power_of_choice_run(
clients,
server,
select_fraction,
T,
decay_factor=decay_factor,
random_seed=seed,
E=E,
B=B,
learning_rate=lr,
momentum=momentum,
logging=wandb_logging,
)
test_acc_arr.append(test_acc)
train_acc_arr.append(train_acc)
train_loss_arr.append(train_loss)
val_loss_arr.append(val_loss)
test_loss_arr.append(test_loss)
selections_arr.append(selections)
if algorithm in ["sfedavg", "ucb", "greedyshap"]:
return test_acc_arr, train_acc_arr, train_loss_arr, val_loss_arr, test_loss_arr, selections_arr, shap_arr
return test_acc_arr, train_acc_arr, train_loss_arr, val_loss_arr, test_loss_arr, selections_arr
# def save_to_excel(path, metrics, global_config, algorithm, parameters):
# if not os.path.exists(path):
# os.makedirs(path)
# df = pd.DataFrame(data=metrics)
# df.to_excel(excel_writer=path+'test.xlsx', sheet_name='sheet1')
if __name__ == "__main__":
wandb.login()
wandb.finish()
dataset = "fmnist"
systems_heterogenity_list = [0, 0, 0, 0.5, 0.9, 0, 0]
dataset_alpha_list = [1e-4, 1e-1, 1e1, 1e-4, 1e-4, 1e-4, 1e-4]
noise_list = [0,0,0,0,0, 0.1, 0.05]
if dataset in ["fmnist","mnist"]:
select_fraction = 0.01 # 0.03
num_clients = 300
T = 400
if dataset in ["fmnist"]:
GLOBAL_PROJECT_NAME = "FL-FMNIST-Final"
else:
GLOBAL_PROJECT_NAME = "FL-MNIST-Final"
elif dataset in ["cifar10"]:
select_fraction = 0.1
num_clients = 200
T = 200
GLOBAL_PROJECT_NAME = "FL-CIFAR10-Final"
elif dataset in ["synthetic"]:
select_fraction = 0.03
num_clients = 200
T = 200
dataset_alpha_list = [1, 0.5, 0, 1, 1, 1, 1, 1] # only for synthetic
GLOBAL_PROJECT_NAME = "FL-Synthetic-Final"
for systems_heterogenity_item, dataset_alpha_item, noise_item in zip(systems_heterogenity_list, dataset_alpha_list, noise_list):
dataset_config = {
"dataset": dataset, # dataset from ["cifar10", "mnist", "synthetic", "fmnist"]
"num_clients": num_clients,
"dataset_alpha": dataset_alpha_item,
"dataset_beta": dataset_alpha_item, # active only for synthetic
"noise": noise_item,
"systems_heterogenity": systems_heterogenity_item
}
algo_config_global = {
"select_fraction":select_fraction,
"epochs":5,
"batches":5,
"T":T,
"lr": 0.01,
"momentum":0.5,
}
algo_config_specific = {
"fedavg":{},
"fedprox":{"mu":[1e-2, 1e-1, 1, 10]},
"sfedavg":{"alpha":[0.5]},
"poc":{"decay_factor":[0.9]},
"ucb":{"beta":[1e-2, 1]},
"greedyshap":{"memory":["mean",0, 0.1, 0.5, 0.9]},
"centralised":{}
}
num_seeds = 5
# path = './local_logs/'
systems_heterogenity = dataset_config["systems_heterogenity"]
global_config = {**dataset_config, **algo_config_global}
for seed in range(num_seeds):
data_seed = seed
if systems_heterogenity > 0:
np.random.seed(seed)
num_clients = dataset_config["num_clients"]
num_slow = int(np.floor(systems_heterogenity * num_clients))
slow_clients = np.random.choice(a=num_clients,size=num_slow,replace=False)
E_base = deepcopy(algo_config_global["epochs"])
E = [E_base for i in range(num_clients)]
for i in slow_clients:
E[i] = np.random.choice(a=range(1,E_base+1), size=1)[0]
global_config["E"] = E
else:
global_config["E"] = global_config["epochs"]
clients, server = initNetworkData(
dataset=dataset_config["dataset"],
num_clients=dataset_config["num_clients"],
random_seed=data_seed,
alpha=dataset_config["dataset_alpha"],
beta=dataset_config["dataset_beta"],
update_noise_level=dataset_config["noise"],
)
for algorithm, parameters in algo_config_specific.items():
clients_copy = deepcopy(clients)
server_copy = deepcopy(server)
metrics = execute_run(clients=clients_copy, server=server_copy, algorithm=algorithm, parameters=parameters, config_global=global_config, seed=seed)
# save_to_excel(path=path, metrics=metrics, global_config=global_config, algorithm=algorithm, parameters=parameters)
wandb.init(project="FL-RUN-COMPLETED", name=f"finishing-{dataset_config['dataset']}")
wandb.alert(title=f"finishing-{dataset_config['dataset']}", text="Finishing")
wandb.finish()