-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_multiDS.py
208 lines (169 loc) · 8.45 KB
/
main_multiDS.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
import os
import argparse
import random
import copy
import torch
from pathlib import Path
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 CFL devices.")
print("Running GCFL plus 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}")
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='mix')
parser.add_argument('--convert_x', help='whether to convert original node features to one-hot degree features',
type=bool, default=False)
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=10)
parser.add_argument('--epsilon1', help='the threshold epsilon1 for GCFL',
type=float, default=0.01)
parser.add_argument('--epsilon2', help='the threshold epsilon2 for GCFL',
type=float, default=0.1)
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" if torch.cuda.is_available() else "cpu"
EPS_1 = args.epsilon1
EPS_2 = args.epsilon2
# TODO: change the data input path and output path
outbase = os.path.join(args.outbase, f'seqLen{args.seq_length}')
if args.overlap and args.standardize:
outpath = os.path.join(outbase, f"standardizedDTW/multiDS-overlap")
elif args.overlap:
outpath = os.path.join(outbase, f"multiDS-overlap")
elif args.standardize:
outpath = os.path.join(outbase, f"standardizedDTW/multiDS-nonOverlap")
else:
outpath = os.path.join(outbase, f"multiDS-nonOverlap")
outpath = os.path.join(outpath, args.data_group, f'eps_{EPS_1}_{EPS_2}')
Path(outpath).mkdir(parents=True, exist_ok=True)
print(f"Output Path: {outpath}")
# preparing data
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_multiDS(args.datapath, args.data_group, args.batch_size, convert_x=args.convert_x, seed=seed_dataSplit)
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_selftrain(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server), local_epoch=100)
process_fedavg(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server))
process_fedprox(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server), mu=0.01)
process_gcfl(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server))
process_gcflplus(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server))
process_gcflplusdWs(clients=copy.deepcopy(init_clients), server=copy.deepcopy(init_server))