forked from CharlieDinh/FEDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_nist.py
executable file
·217 lines (188 loc) · 8.86 KB
/
main_nist.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
import h5py
import matplotlib.pyplot as plt
import numpy as np
import argparse
import importlib
import random
import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from flearn.utils.plot_utils import *
from flearn.utils.model_utils import read_data
import matplotlib
matplotlib.use('Agg')
# GLOBAL PARAMETERS
OPTIMIZERS = ['fedsgd', 'fedfedl']
DATASETS = ['nist', 'mnist', 'fashion_mnist'] # NIST is EMNIST in the paper
MODEL_PARAMS = {
'sent140.bag_dnn': (2,), # num_classes
'sent140.stacked_lstm': (25, 2, 100), # seq_len, num_classes, num_hidden
# seq_len, num_classes, num_hidden
'sent140.stacked_lstm_no_embeddings': (25, 2, 100),
# num_classes, should be changed to 62 when using EMNIST
'nist.mclr': (62,),
'nist.cnn': (62,),
'mnist.mclr': (10,), # num_classes
'mnist.cnn': (10,), # num_classes
'fashion_mnist.mclr': (10,),
'fashion_mnist.cnn': (10,),
'shakespeare.stacked_lstm': (80, 80, 256), # seq_len, emb_dim, num_hidden
'synthetic.mclr': (10, ) # num_classes
}
def read_options(num_users=5, loc_ep=10, Numb_Glob_Iters=100, lamb=0, learning_rate=0.01, hyper_learning_rate = 0.01, alg='fedprox', weight=True, batch_size=0, times = 10, rho = 0, dataset="mnist"):
''' Parse command line arguments or load defaults '''
parser = argparse.ArgumentParser()
parser.add_argument('--optimizer',
help='name of optimizer;',
type=str,
choices=OPTIMIZERS,
default=alg) # fedavg, fedprox
parser.add_argument('--dataset',
help='name of dataset;',
type=str,
choices=DATASETS,
default=dataset)
parser.add_argument('--model',
help='name of model;',
type=str,
default='mclr.py') # 'stacked_lstm.py'
parser.add_argument('--num_rounds',
help='number of rounds to simulate;',
type=int,
default=Numb_Glob_Iters)
parser.add_argument('--eval_every',
help='evaluate every ____ rounds;',
type=int,
default=1)
parser.add_argument('--clients_per_round',
help='number of clients trained per round;',
type=int,
default=num_users)
parser.add_argument('--batch_size',
help='batch size when clients train on data;',
type=int,
default=batch_size
) # 0 is full dataset
parser.add_argument('--num_epochs',
help='number of epochs when clients train on data;',
type=int,
default=loc_ep)
parser.add_argument('--learning_rate',
help='learning rate for inner solver;',
type=float,
default=learning_rate) # 0.003
parser.add_argument('--hyper_learning_rate',
help='learning rate for inner solver;',
type=float,
default=hyper_learning_rate) # 0.003
parser.add_argument('--mu',
help='constant for prox;',
type=float,
default=0.) # 0.01
parser.add_argument('--seed',
help='seed for randomness;',
type=int,
default=0)
parser.add_argument('--weight',
help='enable weight value;',
type=int,
default=weight)
parser.add_argument('--lamb',
help='Penalty value for proximal term;',
type=int,
default=lamb)
parser.add_argument('--times',
help='Number of running time;',
type=int,
default=1)
parser.add_argument('--rho',
help='Condition number only for synthetic data;',
type=float,
default=rho)
try:
parsed = vars(parser.parse_args())
except IOError as msg:
parser.error(str(msg))
# Set seeds
random.seed(1 + parsed['seed'])
np.random.seed(12 + parsed['seed'])
tf.set_random_seed(123 + parsed['seed'])
# load selected model
# all synthetic datasets use the same model
if parsed['dataset'].startswith("synthetic"):
model_path = '%s.%s.%s.%s' % (
'flearn', 'models', 'synthetic', parsed['model'])
else:
model_path = '%s.%s.%s.%s' % (
'flearn', 'models', parsed['dataset'], parsed['model'])
# mod = importlib.import_module(model_path)
import flearn.models.mnist.mclr as mclr
mod = mclr
learner = getattr(mod, 'Model')
# load selected trainer
opt_path = 'flearn.trainers.%s' % parsed['optimizer']
mod = importlib.import_module(opt_path)
optimizer = getattr(mod, 'Server')
# add selected model parameter
parsed['model_params'] = MODEL_PARAMS['.'.join(
model_path.split('.')[2:-1])]
# parsed['model_params'] = MODEL_PARAMS['mnist.mclr']
# print and return
maxLen = max([len(ii) for ii in parsed.keys()])
fmtString = '\t%' + str(maxLen) + 's : %s'
print('Arguments:')
for keyPair in sorted(parsed.items()):
print(fmtString % keyPair)
return parsed, learner, optimizer
def main(num_users=5, loc_ep=10, Numb_Glob_Iters=100, lamb=0, learning_rate=0.01,hyper_learning_rate= 0.01, alg='fedprox', weight=True, batch_size=0, times =10, rho = 0, dataset="mnist"):
# suppress tf warnings
tf.logging.set_verbosity(tf.logging.WARN)
# parse command line arguments
options, learner, optimizer = read_options(
num_users, loc_ep, Numb_Glob_Iters, lamb, learning_rate,hyper_learning_rate, alg, weight, batch_size, times, rho, dataset)
# read data
train_path = os.path.join('data', options['dataset'], 'data', 'train')
test_path = os.path.join('data', options['dataset'], 'data', 'test')
dataset = read_data(train_path, test_path)
# call appropriate trainer
for i in range(times):
# Set seeds
random.seed(1 + i)
np.random.seed(12 + i)
tf.set_random_seed(123 + i)
print('......time for runing......',i)
t = optimizer(options, learner, dataset)
t.train(i)
average_data(num_users=num_users, loc_ep1=loc_ep, Numb_Glob_Iters=Numb_Glob_Iters, lamb=lamb, learning_rate=learning_rate, hyper_learning_rate = hyper_learning_rate, algorithms=alg, batch_size=batch_size, dataset=dataset, rho = rho, times = times)
# if __name__ == '__main__':
# algorithms_list = ["fedfedl", "fedsgd",
# "fedfedl", "fedsgd",
# "fedfedl", "fedsgd"]
# lamb_value = [0, 0, 0, 0, 0, 0]
# learning_rate = [0.001, 0.001, 0.001, 0.001, 0.001, 0.001]
# local_ep = [10,10, 20, 20, 50, 50]
# batch_size = [20,20, 20, 20, 20, 20]
# DATA_SET = "nist"
# number_users = 30
# for i in range(len(algorithms_list)):
# main(num_users=number_users, loc_ep=local_ep[i], Numb_Glob_Iters=1000, lamb=lamb_value[i],
# learning_rate=learning_rate[i], alg=algorithms_list[i], batch_size=batch_size[i], rho = rho[i], dataset=DATA_SET)
# plot_summary_three_figures(num_users=number_users, loc_ep1=local_ep, Numb_Glob_Iters=800, lamb=lamb_value,
# learning_rate=learning_rate, algorithms_list=algorithms_list, batch_size=batch_size, rho = rho[i], dataset=DATA_SET)
# print("-- FINISH -- :",)
if __name__ == '__main__':
algorithms_list = ["fedfedl","fedsgd","fedfedl", "fedfedl","fedsgd","fedfedl", "fedfedl","fedsgd","fedfedl"]
rho = [0,0,0,0,0,0,0,0,0,0,0,0]
lamb_value = [0, 0, 0, 0, 0, 0, 0, 0, 0]
learning_rate = [0.003, 0.003, 0.015, 0.003, 0.003, 0.015, 0.003, 0.003, 0.015]
hyper_learning_rate = [0.2, 0, 0.5, 0.2, 0, 0.5, 0.2, 0, 0.5]
local_ep = [10, 10, 10, 20, 20, 20, 40, 40, 40]
batch_size = [20, 20, 0, 20, 20, 0, 20, 20, 0]
DATA_SET = "nist"
number_users = 10
# for i in range(len(algorithms_list)):
# main(num_users=number_users, loc_ep=local_ep[i], Numb_Glob_Iters=800, lamb=lamb_value[i],
# learning_rate=learning_rate[i],hyper_learning_rate=hyper_learning_rate[i], alg=algorithms_list[i], batch_size=batch_size[i], rho = rho[i], dataset=DATA_SET)
plot_summary_nist(num_users=number_users, loc_ep1=local_ep, Numb_Glob_Iters=800, lamb=lamb_value,
learning_rate=learning_rate, hyper_learning_rate = hyper_learning_rate, algorithms_list=algorithms_list, batch_size=batch_size, rho = rho, dataset=DATA_SET)
print("-- FINISH -- :",)