forked from Chenglu0426/FairGraphFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_oneDS.py
280 lines (203 loc) · 10.2 KB
/
main_oneDS.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
import os
import argparse
import random
import torch
from pathlib import Path
import copy
import setupGC
from training import *
def process_selftrain(clients, server, local_epoch):
print("Self-training ...")
df = pd.DataFrame()
allAccs = run_selftrain_GC(clients, server, local_epoch)
for k, v in allAccs.items():
df.loc[k, [f'train_acc', f'val_acc', f'test_acc']] = v
print(df)
if args.repeat is None:
outfile = os.path.join(outpath, f'accuracy_selftrain_GC{suffix}.csv')
else:
outfile = os.path.join(outpath, "repeats", f'{args.repeat}_accuracy_selftrain_GC{suffix}.csv')
df.to_csv(outfile)
print(f"Wrote to file: {outfile}")
def process_fedavg(clients, server):
print("\nDone setting up FedAvg devices.")
print("Running FedAvg ...")
frame = run_fedavg(clients, server, args.num_rounds, args.local_epoch, samp=None)
if args.repeat is None:
outfile = os.path.join(outpath, f'accuracy_fedavg_GC{suffix}.csv')
else:
outfile = os.path.join(outpath, "repeats", f'{args.repeat}_accuracy_fedavg_GC{suffix}.csv')
frame.to_csv(outfile)
print(f"Wrote to file: {outfile}")
def process_fedprox(clients, server, mu):
print("\nDone setting up FedProx devices.")
print("Running FedProx ...")
frame = run_fedprox(clients, server, args.num_rounds, args.local_epoch, mu, samp=None)
if args.repeat is None:
outfile = os.path.join(outpath, f'accuracy_fedprox_mu{mu}_GC{suffix}.csv')
else:
outfile = os.path.join(outpath, "repeats", f'{args.repeat}_accuracy_fedprox_mu{mu}_GC{suffix}.csv')
frame.to_csv(outfile)
print(f"Wrote to file: {outfile}")
def process_gcfl(clients, server):
print("\nDone setting up GCFL devices.")
print("Running GCFL ...")
if args.repeat is None:
outfile = os.path.join(outpath, f'accuracy_gcfl_GC{suffix}.csv')
else:
outfile = os.path.join(outpath, "repeats", f'{args.repeat}_accuracy_gcfl_GC{suffix}.csv')
frame = run_gcfl(clients, server, args.num_rounds, args.local_epoch, EPS_1, EPS_2)
frame.to_csv(outfile)
print(f"Wrote to file: {outfile}")
def process_gcflplus(clients, server):
print("\nDone setting up GCFL devices.")
print("Running GCFL plus ...")
if args.repeat is None:
outfile = os.path.join(outpath, f'accuracy_gcflplus_GC{suffix}.csv')
else:
outfile = os.path.join(outpath, "repeats", f'{args.repeat}_accuracy_gcflplus_GC{suffix}.csv')
frame = run_gcflplus(clients, server, args.num_rounds, args.local_epoch, EPS_1, EPS_2, args.seq_length, args.standardize)
frame.to_csv(outfile)
print(f"Wrote to file: {outfile}")
def process_gcflplusdWs(clients, server):
print("\nDone setting up GCFL devices.")
print("Running GCFL plus with dWs ...")
if args.repeat is None:
outfile = os.path.join(outpath, f'accuracy_gcflplusDWs_GC{suffix}.csv')
else:
outfile = os.path.join(outpath, "repeats", f'{args.repeat}_accuracy_gcflplusDWs_GC{suffix}.csv')
frame = run_gcflplus_dWs(clients, server, args.num_rounds, args.local_epoch, EPS_1, EPS_2, args.seq_length, args.standardize)
frame.to_csv(outfile)
print(f"Wrote to file: {outfile}")
def process_prototype(clients, server):
print("\nDone setting up prototype devices.")
print("Running fed prototype ...")
frame = run_prototype(clients, server, args.num_rounds, args.local_epoch, samp=None)
#outfile = os.path.join()
def process_protoreput(clients, server):
print("\nDone setting up prototype devices.")
print("Running fed prototype ...")
frame = run_protoreput(clients, server, args.num_rounds, args.device, samp=None)
def process_protoreput2(clients, server):
print("\nDone setting up prototype devices.")
print("Running fed prototype ...")
frame = run_protoreput2(clients, server, args.num_rounds, args.device, args.disable_dp, samp=None)
# def process_protoreput4(clients, server):
# print("\nDone setting up prototype devices.")
# print("Running fed prototype ...")
# frame = run_protoreput4(clients, server, args.num_rounds, args.device, args.disable_dp, samp=None)
def process_protoreput3(clients, server):
print("\nDone setting up prototype devices.")
print("Running fed prototype ...")
frame = run_protoreput3(clients, server, args.num_rounds, args.device, samp=None)
def process_reput(clients, server):
print('\nDone setting up devices.')
print('Running fed reput ...')
frame = run_reput(clients, server, args.num_rounds, args.local_epoch, samp=None)
def process_reput2(clients, server):
print('\nDone setting up devices.')
print('Running fed reput ...')
frame = run_reput2(clients, server, args.num_rounds, args.local_epoch, samp=None)
def process_reput3(clients, server):
print('\nDone setting up devices.')
print('Running fed reput ...')
frame = run_reput3(clients, server, args.num_rounds, 1, samp=None)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=str, default='cpu',
help='CPU / GPU device.')
parser.add_argument('--num_repeat', type=int, default=5,
help='number of repeating rounds to simulate;')
parser.add_argument('--num_rounds', type=int, default=200,
help='number of rounds to simulate;')
parser.add_argument('--local_epoch', type=int, default=1,
help='number of local epochs;')
parser.add_argument('--lr', type=float, default=0.001,
help='learning rate for inner solver;')
parser.add_argument('--weight_decay', type=float, default=5e-4,
help='Weight decay (L2 loss on parameters).')
parser.add_argument('--nlayer', type=int, default=3,
help='Number of GINconv layers')
parser.add_argument('--hidden', type=int, default=64,
help='Number of hidden units.')
parser.add_argument('--dropout', type=float, default=0.5,
help='Dropout rate (1 - keep probability).')
parser.add_argument('--batch_size', type=int, default=128,
help='Batch size for node classification.')
parser.add_argument('--seed', help='seed for randomness;',
type=int, default=123)
parser.add_argument('--datapath', type=str, default='./data',
help='The input path of data.')
parser.add_argument('--outbase', type=str, default='./outputs',
help='The base path for outputting.')
parser.add_argument('--repeat', help='index of repeating;',
type=int, default=None)
parser.add_argument('--data_group', help='specify the group of datasets',
type=str, default='IMDB-BINARY')
parser.add_argument('--convert_x', help='whether to convert original node features to one-hot degree features',
type=bool, default=False)
parser.add_argument('--num_clients', help='number of clients',
type=int, default=10)
parser.add_argument('--overlap', help='whether clients have overlapped data',
type=bool, default=False)
parser.add_argument('--standardize', help='whether to standardize the distance matrix',
type=bool, default=False)
parser.add_argument('--seq_length', help='the length of the gradient norm sequence',
type=int, default=5)
parser.add_argument('--epsilon1', help='the threshold epsilon1 for GCFL',
type=float, default=0.03)
parser.add_argument('--epsilon2', help='the threshold epsilon2 for GCFL',
type=float, default=0.06)
parser.add_argument('--lamb', type=float, default=0.1)
parser.add_argument('--beta', type=float, default=0.85)
parser.add_argument('--aug', type=bool, default=False)
parser.add_argument('--disable_dp', type=bool, default=False)
try:
args = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
seed_dataSplit = 123
# set seeds
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
args.device = "cuda:0" if torch.cuda.is_available() else "cpu"
EPS_1 = args.epsilon1
EPS_2 = args.epsilon2
outbase = os.path.join(args.outbase, f'seqLen{args.seq_length}')
if args.overlap and args.standardize:
outpath = os.path.join(outbase, f"standardizedDTW/oneDS-overlap")
elif args.overlap:
outpath = os.path.join(outbase, f"oneDS-overlap")
elif args.standardize:
outpath = os.path.join(outbase, f"standardizedDTW/oneDS-nonOverlap")
else:
outpath = os.path.join(outbase, f"oneDS-nonOverlap")
outpath = os.path.join(outpath, f'{args.data_group}-{args.num_clients}clients', f'eps_{EPS_1}_{EPS_2}')
Path(outpath).mkdir(parents=True, exist_ok=True)
print(f"Output Path: {outpath}")
""" distributed one dataset to multiple clients """
if not args.convert_x:
""" using original features """
suffix = ""
print("Preparing data (original features) ...")
else:
""" using node degree features """
suffix = "_degrs"
print("Preparing data (one-hot degree features) ...")
if args.repeat is not None:
Path(os.path.join(outpath, 'repeats')).mkdir(parents=True, exist_ok=True)
splitedData, df_stats = setupGC.prepareData_oneDS(args.datapath, args.data_group, num_client=args.num_clients, batchSize=args.batch_size,
convert_x=args.convert_x, seed=seed_dataSplit, overlap=args.overlap, aug=args.aug)
print("Done")
# save statistics of data on clients
if args.repeat is None:
outf = os.path.join(outpath, f'stats_trainData{suffix}.csv')
else:
outf = os.path.join(outpath, "repeats", f'{args.repeat}_stats_trainData{suffix}.csv')
df_stats.to_csv(outf)
print(f"Wrote to {outf}")
init_clients, init_server, init_idx_clients = setupGC.setup_devices(splitedData, args)
print("\nDone setting up devices.")
process_protoreput2(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server))