-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_general.py
240 lines (179 loc) · 9.23 KB
/
util_general.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
from util_libs import *
import wandb
def test_img(net_g, datatest, args):
net_g.eval()
# testing
test_loss = 0
correct = 0
data_loader = DataLoader(datatest, batch_size=args['bs'])
l = len(data_loader)
for idx, (data, target) in enumerate(data_loader):
data, target = data.to(args['device']), target.to(args['device'])
log_probs = net_g(data)
test_loss += F.cross_entropy(log_probs, target, reduction='sum').item()
y_pred = log_probs.data.max(1, keepdim=True)[1]
correct += y_pred.eq(target.data.view_as(y_pred)).long().cpu().sum()
test_loss /= len(data_loader.dataset)
accuracy = 100.00 * correct / len(data_loader.dataset)
return accuracy.numpy(), test_loss
def test_all(net_g, dataset, args, is_train_set=False):
return test_img(net_g, dataset, args)
class LocalUpdate(object):
def __init__(self, args, args_hyperparameters, dataset=None):
self.args = args
self.loss_func = nn.CrossEntropyLoss()
self.dataset = dataset
self.ldr_train = DataLoader(dataset, batch_size=self.args['bs'], shuffle=True)
self.lr = args_hyperparameters['eta_l']
self.use_data_augmentation = args_hyperparameters['use_augmentation']
self.max_norm = args_hyperparameters['max_norm']
self.weight_decay = args_hyperparameters['weight_decay']
self.transform_train = transforms.Compose([transforms.RandomCrop(32, padding=4),transforms.RandomHorizontalFlip(),])
def train_and_sketch(self, net):
net.train()
optimizer = torch.optim.SGD(net.parameters(), lr=self.lr, weight_decay=self.weight_decay)
prev_net = copy.deepcopy(net)
batch_loss = []
step_count = 0
norms = []
while(True):
for batch_idx, (images, labels) in enumerate(self.ldr_train):
images, labels = images.to(self.args['device']), labels.to(self.args['device'])
if(self.use_data_augmentation == True):
images = self.transform_train(images)
net.zero_grad()
log_probs = net(images)
loss = self.loss_func(log_probs, labels)
local_par_list = parameters_to_vector(net.parameters())
loss += 0.5 * self.args['l2_reg'] * torch.norm(local_par_list) ** 2
loss.backward()
total_norm = torch.nn.utils.clip_grad_norm_(parameters=net.parameters(), max_norm=self.max_norm)
if total_norm >= self.max_norm:
norms.append(total_norm)
optimizer.step()
batch_loss.append(loss.item())
step_count=step_count+1
if(step_count >= self.args['cp']):
break
if(step_count >= self.args['cp']):
break
with torch.no_grad():
vec_curr = parameters_to_vector(net.parameters())
vec_prev = parameters_to_vector(prev_net.parameters())
params_delta_vec = vec_curr-vec_prev
model_to_return = params_delta_vec
return model_to_return, norms
class LocalUpdate_scaffold(object):
def __init__(self, args, args_hyperparameters, dataset=None):
self.args = args
self.loss_func = nn.CrossEntropyLoss()
self.dataset = dataset
self.ldr_train = DataLoader(dataset, batch_size=self.args['bs'], shuffle=True)
self.lr = args_hyperparameters['eta_l']
self.use_data_augmentation = args_hyperparameters['use_augmentation']
self.max_norm = args_hyperparameters['max_norm']
self.weight_decay = args_hyperparameters['weight_decay']
self.transform_train = transforms.Compose([transforms.RandomCrop(32, padding=4),transforms.RandomHorizontalFlip(),])
def train_and_sketch(self, net, idx, mem_mat, c):
net.train()
optimizer = torch.optim.SGD(net.parameters(), lr=self.lr, momentum = 0, weight_decay = self.weight_decay)
prev_net = copy.deepcopy(net)
eta = self.lr
batch_loss = []
step_count = 0
norms = []
while(True):
for batch_idx, (images, labels) in enumerate(self.ldr_train):
images, labels = images.to(self.args['device']), labels.to(self.args['device'])
if(self.use_data_augmentation == True):
images = self.transform_train(images)
net.zero_grad()
log_probs = net(images)
loss = self.loss_func(log_probs, labels)
state_params_diff = c-mem_mat[idx]
local_par_list = parameters_to_vector(net.parameters())
loss_algo = torch.sum(local_par_list * state_params_diff)
loss = loss + loss_algo
loss += 0.5 * self.args['l2_reg'] * torch.norm(local_par_list) ** 2
loss.backward()
total_norm = torch.nn.utils.clip_grad_norm_(parameters=net.parameters(), max_norm=self.max_norm)
if total_norm >= self.max_norm:
norms.append(total_norm)
optimizer.step()
batch_loss.append(loss.item())
step_count=step_count+1
if(step_count >= self.args['cp']):
break
if(step_count >= self.args['cp']):
break
with torch.no_grad():
vec_curr = parameters_to_vector(net.parameters())
vec_prev = parameters_to_vector(prev_net.parameters())
params_delta_vec = vec_curr-vec_prev
mem_mat[idx] = (mem_mat[idx]-c) - params_delta_vec/(step_count*eta)
model_to_return = params_delta_vec
return model_to_return, norms
class LocalUpdate_fedprox(object):
def __init__(self, args, args_hyperparameters, dataset=None):
self.args = args
self.loss_func = nn.CrossEntropyLoss()
self.dataset = dataset
self.ldr_train = DataLoader(dataset, batch_size=self.args['bs'], shuffle=True)
self.lr = args_hyperparameters['eta_l']
self.use_data_augmentation = args_hyperparameters['use_augmentation']
self.max_norm = args_hyperparameters['max_norm']
self.weight_decay = args_hyperparameters['weight_decay']
self.mu = args_hyperparameters['mu']
self.transform_train = transforms.Compose([transforms.RandomCrop(32, padding=4),transforms.RandomHorizontalFlip(),])
def train_and_sketch(self, net):
net.train()
optimizer = torch.optim.SGD(net.parameters(), lr=self.lr, momentum = 0, weight_decay = self.weight_decay)
prev_net = copy.deepcopy(net)
prev_net_vec = parameters_to_vector(prev_net.parameters())
eta = self.lr
mu = self.mu
batch_loss = []
step_count = 0
norms = []
while(True):
for batch_idx, (images, labels) in enumerate(self.ldr_train):
images, labels = images.to(self.args['device']), labels.to(self.args['device'])
if(self.use_data_augmentation == True):
images = self.transform_train(images)
net.zero_grad()
log_probs = net(images)
loss = self.loss_func(log_probs, labels)
local_par_list = parameters_to_vector(net.parameters())
loss_algo = torch.linalg.norm(local_par_list-prev_net_vec)**2
loss = loss + mu*0.5*loss_algo
loss += 0.5 * self.args['l2_reg'] * torch.norm(local_par_list) ** 2
loss.backward()
total_norm = torch.nn.utils.clip_grad_norm_(parameters=net.parameters(), max_norm=self.max_norm)
if total_norm >= self.max_norm:
norms.append(total_norm)
optimizer.step()
batch_loss.append(loss.item())
step_count=step_count+1
if(step_count >= self.args['cp']):
break
if(step_count >= self.args['cp']):
break
with torch.no_grad():
vec_curr = parameters_to_vector(net.parameters())
vec_prev = parameters_to_vector(prev_net.parameters())
params_delta_vec = vec_curr-vec_prev
model_to_return = params_delta_vec
return model_to_return, norms
def get_grad(net_glob, args, args_hyperparameters, dataset, alg, idx, mem_mat, c, t, grad_square_avg=None):
if(alg=='fedadam' or alg == 'fedexp' or alg =='fedavg' or alg=='fedavgm' or alg=='fedavgm(exp)' or alg=='fedadam'):
local = LocalUpdate(args, args_hyperparameters, dataset=dataset)
grad = local.train_and_sketch(copy.deepcopy(net_glob))
return grad
elif(alg=='scaffold' or alg=='scaffold(exp)'):
local = LocalUpdate_scaffold(args, args_hyperparameters, dataset=dataset)
grad = local.train_and_sketch(copy.deepcopy(net_glob),idx,mem_mat,c)
return grad
elif(alg=='fedprox' or alg=='fedprox(exp)'):
local = LocalUpdate_fedprox(args, args_hyperparameters, dataset=dataset)
grad = local.train_and_sketch(copy.deepcopy(net_glob))
return grad