-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_plot_nist.py
327 lines (286 loc) · 12.9 KB
/
main_plot_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
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
import h5py
import matplotlib.pyplot as plt
import numpy as np
import argparse
import importlib
import random
import os
import tensorflow as tf
from flearn.utils.model_utils import read_data
import matplotlib
matplotlib.use('Agg')
# GLOBAL PARAMETERS
OPTIMIZERS = ['fedavg', 'fedprox', 'fedsvrg', 'fedsarah', 'fedsgd']
DATASETS = ['sent140', 'nist', 'shakespeare', 'mnist', 'synthetic_iid', 'synthetic_0_0',
'synthetic_0.5_0.5', 'synthetic_1_1', 'fashion_mnist'] # NIST is EMNIST in the paper
DATA_SET = "nist"
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, alg='fedprox', weight=True):
''' 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=DATA_SET)
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=10
) # 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('--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)
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.cnn 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, alg='fedprox', weight=True):
# 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, alg, weight)
# 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
t = optimizer(options, learner, dataset)
t.train()
def simple_read_data(loc_ep, alg):
hf = h5py.File('{}_{}.h5'.format(alg, loc_ep), 'r')
rs_glob_acc = np.array(hf.get('rs_glob_acc')[:])
rs_train_acc = np.array(hf.get('rs_train_acc')[:])
rs_train_loss = np.array(hf.get('rs_train_loss')[:])
return rs_train_acc, rs_train_loss, rs_glob_acc
def plot_summary_2(num_users=100, loc_ep1=5, Numb_Glob_Iters=10, lamb=[], learning_rate=[], algorithms_list=[]):
Numb_Algs = len(algorithms_list)
train_acc = np.zeros((Numb_Algs, Numb_Glob_Iters))
train_loss = np.zeros((Numb_Algs, Numb_Glob_Iters))
glob_acc = np.zeros((Numb_Algs, Numb_Glob_Iters))
algs_lbl = algorithms_list.copy()
for i in range(Numb_Algs):
if(lamb[i] > 0):
algorithms_list[i] = algorithms_list[i] + "_prox_" + str(lamb[i])
algs_lbl[i] = algs_lbl[i] + "_prox"
algorithms_list[i] = algorithms_list[i] + \
"_" + str(learning_rate[i]) + "_" + str(num_users) + "u"
train_acc[i, :], train_loss[i, :], glob_acc[i, :] = np.array(
simple_read_data(loc_ep1[i], DATA_SET + algorithms_list[i]))[:, :Numb_Glob_Iters]
algs_lbl[i] = algs_lbl[i]
plt.figure(1)
MIN = train_loss.min() - 0.001
linestyles = ['-', '--', '-.', ':', '-', '--', '-.', ':']
for i in range(Numb_Algs):
plt.plot(train_acc[i, 1:],linestyle=linestyles[i], label=algs_lbl[i])
#plt.plot(train_acc1[i, 1:], label=algs_lbl1[i])
plt.legend(loc='upper right')
plt.ylabel('Training Accuracy')
plt.xlabel('Number of Global Iterations')
plt.title('Number of users: ' + str(num_users) +
', Lr: ' + str(learning_rate[0]))
plt.ylim([MIN, 0.32])
plt.savefig('train_acc.png')
plt.figure(2)
for i in range(Numb_Algs):
plt.plot(train_loss[i, 1:], linestyle=linestyles[i], label=algs_lbl[i])
#plt.plot(train_loss1[i, 1:], label=algs_lbl1[i])
plt.legend(loc='upper right')
#plt.ylim([MIN, 0.34])
plt.ylabel('Training Loss')
plt.xlabel('Number of Global Iterations')
plt.title('Number of users: ' + str(num_users) +', Lr: ' + str(learning_rate[0]))
#plt.ylim([train_loss.min(), 0.3])
plt.savefig('train_loss.png')
plt.figure(3)
for i in range(Numb_Algs):
plt.plot(glob_acc[i, 1:], linestyle=linestyles[i], label=algs_lbl[i])
#plt.plot(glob_acc1[i, 1:], label=algs_lbl1[i])
plt.legend(loc='upper right')
#plt.ylim([0.9, glob_acc.max()])
plt.ylabel('Test Accuracy')
plt.xlabel('Number of Global Iterations')
plt.title('Number of users: ' + str(num_users) +
', Lr: ' + str(learning_rate[0]))
plt.savefig('glob_acc.png')
def plot_summary(num_users=100, loc_ep1=[], Numb_Glob_Iters=10, lamb=[], learning_rate=[], algorithms_list=[]):
#+'$\mu$'
Numb_Algs = len(algorithms_list)
train_acc = np.zeros((Numb_Algs, Numb_Glob_Iters))
train_loss = np.zeros((Numb_Algs, Numb_Glob_Iters))
glob_acc = np.zeros((Numb_Algs, Numb_Glob_Iters))
algs_lbl = algorithms_list.copy()
for i in range(Numb_Algs):
if(lamb[i] > 0):
algorithms_list[i] = algorithms_list[i] + "_prox_" + str(lamb[i])
algs_lbl[i] = algs_lbl[i] + "_prox"
algorithms_list[i] = algorithms_list[i] + "_" + str(learning_rate[i]) + "_" + str(num_users) + "u"
train_acc[i, :], train_loss[i, :], glob_acc[i, :] = np.array(
simple_read_data(loc_ep1[i], DATA_SET + algorithms_list[i]))[:, :Numb_Glob_Iters]
algs_lbl[i] = algs_lbl[i]
plt.figure(1)
linestyles = ['-', '--', '-.', ':', '--', '-.']
algs_lbl = ["FedProxVR_Sarah", "FedProxVR_Svrg", "FedAvg", "FedProx",
"FedProxVR_Sarah", "FedProxVR_Svrg", "FedAvg", "FedProx"]
fig = plt.figure(figsize=(10, 4))
ax = fig.add_subplot(111) # The big subplot
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
#min = train_loss.min()
min = train_loss.min() - 0.01
num_al = 4
# Turn off axis lines and ticks of the big subplot
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off')
for i in range(num_al):
ax2.plot(train_loss[i, 1:], linestyle=linestyles[i], label=algs_lbl[i] + " : " + '$\mu = $' + str(lamb[i]))
ax2.set_ylim([min, 2.5])
ax2.legend(loc='upper right')
ax2.set_title("FENIST: 10 users, " + r'$\beta =10,$' + r'$\tau = 40$', y=1.02)
for i in range(num_al):
ax1.plot(train_loss[i+num_al, 1:], linestyle=linestyles[i], label=algs_lbl[i + num_al] + " : " + '$\mu = $' + str(lamb[i]))
ax1.set_ylim([min, 2.5])
ax1.legend(loc='upper right')
ax1.set_title("FENIST: 10 users, " +
r'$\beta = 7,$' + r'$\tau = 20$', y=1.02)
ax.set_xlabel('Number of Global Iterations')
ax.set_ylabel('Training Loss', labelpad=15)
plt.savefig('train_loss.pdf')
plt.figure(2)
fig = plt.figure(figsize=(10, 4))
ax = fig.add_subplot(111) # The big subplot
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
max = glob_acc.max() + 0.01
# Turn off axis lines and ticks of the big subplot
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.tick_params(labelcolor='w', top='off',
bottom='off', left='off', right='off')
for i in range(num_al):
ax2.plot(glob_acc[i, 1:], linestyle=linestyles[i], label=algs_lbl[i] + " : " + '$\mu = $' + str(lamb[i]))
ax2.set_ylim([0.8, max])
ax2.legend(loc='upper right')
ax2.set_title("MNIST: 100 users, " +
r'$\beta = 10,$' + r'$\tau = 50$', y=1.02)
for (i) in range(num_al):
ax1.plot(glob_acc[i+num_al, 1:], linestyle=linestyles[i],
label=algs_lbl[i + num_al] + " : " + '$\mu = $' + str(lamb[i]))
ax1.set_title("MNIST: 100 users, " +
r'$\beta = 7,$' + r'$\tau = 20$', y=1.02)
ax1.set_ylim([0.8, max])
ax1.legend(loc='upper right')
ax.set_xlabel('Number of Global Iterations')
ax.set_ylabel('Test Accuracy', labelpad=15)
plt.savefig('glob_acc.pdf')
if __name__ == '__main__':
algorithms_list = [ "fedsarah"]
if(1):
lamb_value = [0]
#learning_rate = [0.01, 0.01, 0.01, 0.01,0.015, 0.015, 0.015, 0.015]
learning_rate = [0.015, 0.015, 0.015, 0.015,0.01, 0.01, 0.01, 0.01]
local_ep = [10,10,10,10,20,20,20,20]
number_users = 10
Numb_Glob_I = 4
if(0):
plot_summary_2(num_users=number_users, loc_ep1=local_ep, Numb_Glob_Iters=Numb_Glob_I, lamb=lamb_value,
learning_rate=learning_rate, algorithms_list=algorithms_list)
else:
for i in range(len(algorithms_list)):
main(num_users=number_users, loc_ep=local_ep[i], Numb_Glob_Iters=Numb_Glob_I, lamb=lamb_value[i], learning_rate=learning_rate[i], alg=algorithms_list[i])
plot_summary_2(num_users=number_users, loc_ep1=local_ep, Numb_Glob_Iters=Numb_Glob_I, lamb=lamb_value,
learning_rate=learning_rate, algorithms_list=algorithms_list)
print("-- FINISH -- :",)