forked from omarfoq/FedEM
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_experiment_feddef_loop.py
158 lines (125 loc) · 5.17 KB
/
run_experiment_feddef_loop.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
"""Run Experiment
This script allows to run one federated learning experiment; the experiment name, the method and the
number of clients/tasks should be precised along side with the hyper-parameters of the experiment.
The results of the experiment (i.e., training logs) are written to ./logs/ folder.
This file can also be imported as a module and contains the following function:
* run_experiment - runs one experiments given its arguments
"""
from utils.utils import *
from utils.constants import *
from utils.args import *
from run_experiment import *
from torch.utils.tensorboard import SummaryWriter
# Import General Libraries
import os
import argparse
import torch
import copy
import pickle
import random
import numpy as np
import pandas as pd
from models import *
# Import Transfer Attack
from transfer_attacks.Personalized_NN import *
from transfer_attacks.Params import *
from transfer_attacks.Transferer import *
from transfer_attacks.Args import *
from transfer_attacks.TA_utils import *
import numba
if __name__ == "__main__":
exp_names = ['g0_1', 'g0_5', 'g1', 'g2', 'g4']
G_val = [0.01,0.05,0.1,0.2,0.4]
n_learners = 3
for itt in range(len(exp_names)):
print("running trial:", itt)
# Manually set argument parameters
args_ = Args()
args_.experiment = "cifar10"
args_.method = "FedEM_adv"
args_.decentralized = False
args_.sampling_rate = 1.0
args_.input_dimension = None
args_.output_dimension = None
args_.n_learners= n_learners
args_.n_rounds = 100
args_.bz = 128
args_.local_steps = 1
args_.lr_lambda = 0
args_.lr =0.03
args_.lr_scheduler = 'multi_step'
args_.log_freq = 10
args_.device = 'cuda'
args_.optimizer = 'sgd'
args_.mu = 0
args_.communication_probability = 0.1
args_.q = 1
args_.locally_tune_clients = False
args_.seed = 1234
args_.verbose = 1
args_.save_path = 'weights/neurips/celeba/G_sweep/' + exp_names[itt]
args_.validation = False
# Other Argument Parameters
Q = 10 # update per round
G = G_val[itt]
num_clients = 40
S = 0.05 # Threshold
step_size = 0.01
K = 10
eps = 0.1
# Randomized Parameters
# Ru = np.random.uniform(low=R_val[itt]-0.1, high=R_val[itt]+0.1, size=num_clients)
Ru = np.ones(num_clients)
# Generate the dummy values here
aggregator, clients = dummy_aggregator(args_, num_clients)
# Set attack parameters
x_min = torch.min(clients[0].adv_nn.dataloader.x_data)
x_max = torch.max(clients[0].adv_nn.dataloader.x_data)
atk_params = PGD_Params()
atk_params.set_params(batch_size=1, iteration = K,
target = -1, x_val_min = x_min, x_val_max = x_max,
step_size = 0.05, step_norm = "inf", eps = eps, eps_norm = "inf")
# Obtain the central controller decision making variables (static)
num_h = args_.n_learners= 3
Du = np.zeros(len(clients))
for i in range(len(clients)):
num_data = clients[i].train_iterator.dataset.targets.shape[0]
Du[i] = num_data
D = np.sum(Du) # Total number of data points
# Train the model
print("Training..")
pbar = tqdm(total=args_.n_rounds)
current_round = 0
while current_round <= args_.n_rounds:
# If statement catching every Q rounds -- update dataset
if current_round != 0 and current_round%Q == 0: #
# print("Round:", current_round, "Calculation Adv")
# Obtaining hypothesis information
Whu = np.zeros([num_clients,num_h]) # Hypothesis weight for each user
for i in range(len(clients)):
# print("client", i)
temp_client = aggregator.clients[i]
hyp_weights = temp_client.learners_ensemble.learners_weights
Whu[i] = hyp_weights
row_sums = Whu.sum(axis=1)
Whu = Whu / row_sums[:, np.newaxis]
Wh = np.sum(Whu,axis=0)/num_clients
# Solve for adversarial ratio at every client
Fu = solve_proportions(G, num_clients, num_h, Du, Whu, S, Ru, step_size)
# print(Fu)
# Assign proportion and attack params
# Assign proportion and compute new dataset
for i in range(len(clients)):
aggregator.clients[i].set_adv_params(Fu[i], atk_params)
aggregator.clients[i].update_advnn()
aggregator.clients[i].assign_advdataset()
aggregator.mix()
if aggregator.c_round != current_round:
pbar.update(1)
current_round = aggregator.c_round
if "save_path" in args_:
save_root = os.path.join(args_.save_path)
os.makedirs(save_root, exist_ok=True)
aggregator.save_state(save_root)
del args_, aggregator, clients
torch.cuda.empty_cache()