-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_2stages_exp_fedrl.py
364 lines (293 loc) · 14.8 KB
/
run_2stages_exp_fedrl.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
import argparse
import datetime
import os
import pickle
import sys
import time
from collections import defaultdict
from copy import deepcopy as dcopy
from os.path import join as oj
import numpy as np
import pandas as pd
import torch
from torchvision import models
from utils.arguments import breakout_args, pong_args, spaceinvader_args
from utils.RL_Environment import AtariEnv, DDQNSolver, DDQNTrainer
from utils.utils import (Logger, add_gradient_updates, add_update_to_model,
compute_grad_update, data_valuation,
hotelling_t2_onesample_ht, l2norm,
performance_summary_rl, selection_proba_processing,
selection_proba_score_type, softmax,
synchronized_epsilon)
''' Parse cmd arguments '''
parser = argparse.ArgumentParser(description='Process which dataset to run')
parser.add_argument('-d', '--dataset', help='Dataset name'
, type=str, required=True)
parser.add_argument('-b', '--beta', help='Beta values for softmax'
, type=float, default=30)
parser.add_argument('-r', '--ratio', help='Selection ratio in stage 2'
, type=float, default=0.4)
parser.add_argument('-st', '--score_type', help='Use which score to do stage 2'
, choices=["random", "reverse", "proportion"], type=str, default="proportion")
parser.add_argument('-T1', '--T1', help='Number of iterations of stage 1'
, nargs='?', type=int, default=0)
parser.add_argument('-T2', '--T2', help='Number of iterations of stage 2'
, nargs='?', const=0, type=int, default=0)
parser.add_argument('-T', '--T', help='Number of overall iterations'
, nargs='?', const=600, type=int, default=600)
parser.add_argument('-n', '--exp_name', help='Name of the experiment'
, nargs='?', type=str, default="")
parser.add_argument('-dv', '--dv_method', help='Method used to do data valuation'
, nargs='?', choices=['fed_loo', 'cos_grad', 'mr'], type=str, default="cos_grad")
parser.add_argument('-a', '--attack', help='Noise attack type'
, nargs='?', choices=['normal', 'reward_noise', 'state_noise','memory_size','exploration'], type=str, default="normal")
parser.add_argument('-nc', '--n_participants', help='Number of participants'
, nargs='?', type=int, default=5)
parser.add_argument('-alpha', '--alpha', help='Threshold for hypothesis testing'
, type=float, default=0.95)
parser.add_argument('-samp_num', '--samp_num', help='Look ahead samples for hypothesis testing'
, type=int, default=20)
parser.add_argument('-samp_dim', '--samp_dim', help='Number of participants used in hypothesis testing'
, type=int, default=5)
cmd_args = parser.parse_args()
print(cmd_args)
if cmd_args.dataset == 'Breakout':
args = dcopy(breakout_args)
elif cmd_args.dataset == 'SpaceInvaders':
args = dcopy(spaceinvader_args)
elif cmd_args.dataset == 'Pong':
args = dcopy(pong_args)
else:
pass
args.update(vars(cmd_args))
''' Set up experiment arguments '''
E = args['E']
T1 = args['T1']
T2 = args['T2']
T = args['T']
split = args["split"]
score_type = args["score_type"]
ratio = args["ratio"]
beta = args["beta"]
sample_size_cap = 0
optimizer_fn = args['optimizer_fn']
loss_fn = args['loss_fn']
n_participants = args['n_participants']
if args['attack'] in ["reward_noise", "state_noise"]:
if args['n_participants'] == 5:
if args['dataset'] in ["Pong"]:
noise_level = [0.2, 0.1, 0.05, 0, 0]
else:
noise_level = [0.6, 0.4, 0.2, 0, 0]
elif args['n_participants'] == 10:
if args['dataset'] in ["Pong"]:
noise_level = [0.18, 0.16, 0.14, 0.12, 0.10, 0.08, 0.06, 0.04, 0.02, 0]
else:
noise_level = [0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0]
elif args['attack'] == "memory_size":
if args['n_participants'] == 5:
noise_level = [0.05, 0.1, 0.15, 0.35, 0.35]
elif args['n_participants'] == 10:
noise_level = [0.01, 0.02, 0.04, 0.08, 0.1, 0.15, 0.15, 0.15, 0.15, 0.15]
elif args['attack'] == "exploration":
if args['n_participants'] == 5:
noise_level = np.arange(0,1,0.2)
elif args['n_participants'] == 10:
noise_level = np.arange(0,1,0.1)
else:
noise_level = [0] * args['n_participants']
args["noise_level"] = noise_level
device = torch.device("cuda")
''' Set up entire experiment directory '''
str_time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H:%M')
exp_dir = 'Exp_{}_{}'.format(cmd_args.exp_name, str_time)
exp_dir = oj(args['dataset'], exp_dir)
results_dir = 'FedRL-2stage'
os.makedirs(oj(results_dir, exp_dir), exist_ok=True)
''' Set up individual/specific experiment directory and logger '''
exp_spec = 'FedRL_{}_{}_{:.2f}_{:.2f}_T1-{}_T2-{}_B{}_E{}_lr{}_N{}_D{}'.format(args['dataset'], args['split'][:3], beta, ratio,
T1, T2, args['batch_size'], E, str(args['lr']).replace('.',''), n_participants, sample_size_cap)
individual_exp_dir = oj(results_dir, exp_dir, exp_spec)
os.makedirs(individual_exp_dir, exist_ok=True)
log = open(oj(individual_exp_dir, 'log'), "w")
sys.stdout = Logger(log)
print("Experimental settings are: ", args, '\n')
print("Logging to the : {}.".format(oj(individual_exp_dir, 'log')))
""" Setting up the data loader and model/optimizer for each clients """
env = AtariEnv(args['dataset'])
n_action_space = env.action_space
server_model = args['model_fn'](4,84,84, n_action_space,device=device).cuda()
init_backup = dcopy(server_model)
# setting up the RL agents and model
models, optimizers, agents = [], [], []
for i in range(n_participants):
local_model = dcopy(server_model).cuda()
local_optimizer = args['optimizer_fn'](local_model.parameters(), lr=args['lr'])
agent = DDQNTrainer(action_space=n_action_space, model=local_model,
optimizer=local_optimizer, device=device, args=args, noise=args["attack"], noise_level=noise_level[i])
models.append(local_model)
optimizers.append(local_optimizer)
agents.append(agent)
eval_agent = DDQNSolver(n_action_space, dcopy(server_model).cuda(), device, args=args)
shard_sizes = torch.tensor([1]*n_participants)
relative_shard_sizes = torch.div(shard_sizes, torch.sum(shard_sizes))
weights = relative_shard_sizes
print("Shard sizes are: ", shard_sizes.tolist())
''' Start federated training - stage 1 '''
# define experimental recorder
pre_server_model = dcopy(server_model)
server_perfs = defaultdict(list)
dv_recorder = defaultdict(list)
dv_round_recorder = defaultdict(list)
hypothesis_test_recorder = defaultdict(list)
local_perfs = defaultdict(list)
valid_perfs = defaultdict(list)
fed_perfs = defaultdict(list)
model_diffs = defaultdict(list)
historical_gradients = [[torch.zeros(param.shape).to(device) for param in server_model.parameters()] for i in range(n_participants)]
data_value_score = torch.zeros(n_participants,device=device)
sampled_dims = np.random.choice(np.arange(n_participants), args["samp_dim"], replace=False)
# if T1 = 0 then use KL to determine the stopping
if T1 == 0:
T1 = sys.maxsize
# pre-play each agent first to initilize the memory to certrain capacity
for agent in agents:
env.agent_play(agent, int(args["replay_start_size"]/n_participants))
for t in range(T1):
server_gradient = [torch.zeros(param.shape).to(device) for param in server_model.parameters()]
device_gradients = [[torch.zeros(param.shape).to(device) for param in server_model.parameters()] for i in range(args['n_participants'])]
# get synchronized exporation ratio
epsilon = synchronized_epsilon(agents)
print("T1 step {}: Epsilon: {:.4f}".format(t, epsilon))
for i, local_agent in enumerate(agents):
# distribute the model to the agents
local_agent._assign_epsilon(epsilon, n_participants)
local_agent._assign_model(server_model.state_dict())
env.agent_play(local_agent, args['train_step'])
model = local_agent.get_model()
gradient = compute_grad_update(server_model, model)
model.load_state_dict(server_model.state_dict())
add_gradient_updates(device_gradients[i], gradient, weight=weights[i])
add_gradient_updates(server_gradient, gradient, weight=weights[i])
add_gradient_updates(historical_gradients[i], gradient, weight=weights[i])
add_update_to_model(server_model, server_gradient)
# predication on validation
print("Validation performance at round : ", t+1 )
epsilon = synchronized_epsilon(agents)
eval_agent._assign_epsilon(epsilon)
eval_agent._assign_model(server_model.state_dict())
score, step = env.agent_play(eval_agent, None, args['eval_run'])
server_perfs['_score'].append(score)
server_perfs['_step'].append(step)
print("Server model score {:.2f}, step: {:.2f}".format(score,step))
# data valuation on each clients
data_value_score, data_value_round = data_valuation(data_value_score, server_model, pre_server_model, server_gradient,
device_gradients, weights, n_participants, t, None, loss_fn, device, None, dv_method=args["dv_method"])
data_value_score_d, data_value_round_d = data_value_score.cpu().detach().numpy(), data_value_round.cpu().detach().numpy()
for i in range(n_participants):
dv_recorder[i].append(data_value_score_d[i])
dv_round_recorder[i].append(data_value_round_d[i])
# hotelling's T2 hypothesis testing
dv_round_df = pd.DataFrame(dv_round_recorder)
stop_flag, p_value = hotelling_t2_onesample_ht(dv_round_df.iloc[:,sampled_dims],
alpha=args["alpha"], shift=0,sample_num=args["samp_num"], verbose=False)
hypothesis_test_recorder["stop_flag"].append(stop_flag)
hypothesis_test_recorder["p_value"].append(p_value)
# determine the stopping of stage 1
if stop_flag and cmd_args.T1==0:
break
''' Show the information computed from stage 1 '''
T1 = t + 1
print("Step 1 train stop at: {}\n\n".format((t+1)))
print("Data values are: ", data_value_score.data)
data_value_score_ = torch.div(data_value_score, torch.max(data_value_score))
print("Data values are(max norm): ", data_value_score_.data)
print("Beginning of rewarding stage:")
print("-"*60)
param_counts = [ len(param.view(-1)) for param in server_model.parameters() ]
print("Parameter counts for each layer:", param_counts)
''' Start federated training stage 2 '''
# processing the selection probability
data_value_score = data_value_score.cpu().detach().numpy()
selection_probs = softmax(data_value_score, beta=beta)
selection_probs = selection_proba_processing(selection_probs)
print("Beta value:{:.2f}:", beta)
print("Throughout ratio:{:.2f}:", ratio)
print("Selection probability linear:", selection_probs)
probs_new = selection_proba_score_type(selection_probs, score_type)
if T != 0:
T2 = T - T1
# preparing parameters
size_S = int(ratio * n_participants) # randomly picked devices to reward
staleness_status = {"client {}".format(tmp):[0]*T2 for tmp in range(n_participants)}
for t in range(T1, T1+T2):
# randomly sample devices to reward them
selected_S = np.random.choice(range(n_participants), size=size_S, replace=False, p=probs_new)
server_gradient = [torch.zeros(param.shape).to(device) for param in server_model.parameters()]
# get synchronized exporation ratio
epsilon = synchronized_epsilon(agents)
for i in selected_S:
staleness_status['client {}'.format(i)][t-T1] = 1
# distribute model to agents
local_agent = agents[i]
local_agent._assign_epsilon(epsilon, len(selected_S))
local_agent._assign_model(server_model.state_dict())
env.agent_play(local_agent, args['train_step'])
model = local_agent.get_model()
gradient = compute_grad_update(server_model, model)
model.load_state_dict(server_model.state_dict())
add_gradient_updates(server_gradient, gradient, weight=weights[i])
add_update_to_model(server_model, server_gradient)
print("Validation performance at round : ", t+1)
epsilon = synchronized_epsilon(agents)
eval_agent._assign_epsilon(epsilon)
eval_agent._assign_model(server_model.state_dict())
score, step = env.agent_play(eval_agent, None, args['eval_run'])
print("Server model score {:.2f}, step: {:.2f}".format(score,step))
server_perfs['_score'].append(score)
server_perfs['_step'].append(step)
for i, local_model in enumerate(models):
epsilon = synchronized_epsilon(agents)
eval_agent._assign_epsilon(epsilon)
eval_agent._assign_model(local_model.state_dict())
score, step = env.agent_play(eval_agent, None, args['eval_run'])
valid_perfs[str(i)+'_score'].append(score)
valid_perfs[str(i)+'_step'].append(step)
fed_loss, fed_accu = 0, 0
model_diff_norm = l2norm(compute_grad_update(server_model, local_model)).item()
model_diffs[str(i)+"_model_diff_l2norm"].append(model_diff_norm)
''' Compiling results and plotting '''
pd.DataFrame(server_perfs).to_csv(oj(individual_exp_dir,'server.csv'), index=False)
pd.DataFrame(fed_perfs).to_csv(oj(individual_exp_dir,'fed.csv'), index=False)
pd.DataFrame(valid_perfs).to_csv(oj(individual_exp_dir,'valid.csv'), index=False)
pd.DataFrame(local_perfs).to_csv(oj(individual_exp_dir,'local.csv'), index=False)
pd.DataFrame(staleness_status).to_csv(oj(individual_exp_dir,'staleness.csv'), index=False)
pd.DataFrame(dv_recorder).to_csv(oj(individual_exp_dir,'dv_recorder.csv'), index=False)
pd.DataFrame(dv_round_recorder).to_csv(oj(individual_exp_dir, 'dv_round_recorder.csv'), index=False)
pd.DataFrame(hypothesis_test_recorder).to_csv(oj(individual_exp_dir, 'hypothesis_test_recorder.csv'), index=False)
pd.DataFrame(model_diffs).to_csv(oj(individual_exp_dir, 'model_diff.csv'), index=False)
# write the sys settings down
args["data_value_score"] = data_value_score
args["stage1_stop_step"] = T1
args["selection_probability"] = probs_new
# compute the performance of the framework
try:
args = performance_summary_rl(args=args, path=oj(individual_exp_dir))
except Exception as e:
print(e)
with open(oj(individual_exp_dir, 'settings_dict.txt'), 'w') as file:
[file.write(key + ' : ' + str(value) + '\n') for key, value in args.items()]
with open(oj(individual_exp_dir, 'settings_dict.pickle'), 'wb') as f:
pickle.dump(args, f)
from utils.new_plot import plot_rl
try:
plot_rl(oj(individual_exp_dir))
except Exception as e:
print(e)
with open(oj(individual_exp_dir, 'complete.txt'), 'w') as file:
file.write('complete')
log.close() # close the writer of log
with open(oj(individual_exp_dir, 'settings_dict.txt'), 'w') as file:
[file.write(key + ' : ' + str(value) + '\n') for key, value in args.items()]
with open(oj(individual_exp_dir, 'settings_dict.pickle'), 'wb') as f:
pickle.dump(args, f)