-
Notifications
You must be signed in to change notification settings - Fork 2
/
local_training.py
112 lines (88 loc) · 3.99 KB
/
local_training.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
import random
import copy
import time
import math
import numpy as np
import torch
import torch.optim as optim
from fedavg.config import get_args
from model import simplecnn, textcnn
from test import compute_local_test_accuracy, compute_acc
from prepare_data import get_dataloader
def local_train(args, nets_this_round, train_local_dls, val_local_dls, test_dl, data_distributions, best_val_acc_list, best_test_acc_list):
for net_id, net in nets_this_round.items():
train_local_dl = train_local_dls[net_id]
data_distribution = data_distributions[net_id]
# Set Optimizer
if args.optimizer == 'adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, weight_decay=args.reg)
elif args.optimizer == 'amsgrad':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, weight_decay=args.reg,
amsgrad=True)
elif args.optimizer == 'sgd':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, momentum=0.9,
weight_decay=args.reg)
criterion = torch.nn.CrossEntropyLoss().cuda()
net.cuda()
net.train()
iterator = iter(train_local_dl)
for iteration in range(args.num_local_iterations):
try:
x, target = next(iterator)
except StopIteration:
iterator = iter(train_local_dl)
x, target = next(iterator)
x, target = x.cuda(), target.cuda()
optimizer.zero_grad()
target = target.long()
out = net(x)
loss = criterion(out, target)
loss.backward()
optimizer.step()
val_acc = compute_acc(net, val_local_dls[net_id])
personalized_test_acc, generalized_test_acc = compute_local_test_accuracy(net, test_dl, data_distribution)
if val_acc > best_val_acc_list[net_id]:
best_val_acc_list[net_id] = val_acc
best_test_acc_list[net_id] = personalized_test_acc
print('>> Client {} | Personalized Test Acc: {:.5f} | Generalized Test Acc: {:.5f}'.format(net_id, personalized_test_acc, generalized_test_acc))
net.to('cpu')
return np.array(best_test_acc_list).mean()
args, cfg = get_args()
print(args)
seed = args.init_seed
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
random.seed(seed)
n_party_per_round = int(args.n_parties * args.sample_fraction)
party_list = [i for i in range(args.n_parties)]
party_list_rounds = []
if n_party_per_round != args.n_parties:
for i in range(args.comm_round):
party_list_rounds.append(random.sample(party_list, n_party_per_round))
else:
for i in range(args.comm_round):
party_list_rounds.append(party_list)
train_local_dls, val_local_dls, test_dl, net_dataidx_map, traindata_cls_counts, data_distributions = get_dataloader(args)
if args.dataset == 'cifar10':
model = simplecnn
elif args.dataset == 'cifar100':
model = simplecnn
elif args.dataset == 'yahoo_answers':
model = textcnn
local_models = []
best_val_acc_list, best_test_acc_list = [],[]
for i in range(args.n_parties):
local_models.append(model(cfg['classes_size']))
best_val_acc_list.append(0)
best_test_acc_list.append(0)
for round in range(args.comm_round): # Federated round loop
party_list_this_round = party_list_rounds[round]
if args.sample_fraction<1.0:
print(f'>> Clients in this round : {party_list_this_round}')
nets_this_round = {k: local_models[k] for k in party_list_this_round}
# Local Model Training
mean_personalized_acc = local_train(args, nets_this_round, train_local_dls, val_local_dls, test_dl, data_distributions, best_val_acc_list, best_test_acc_list)
print('>> (Current) Round {} | Local Per: {:.5f}'.format(round, mean_personalized_acc))
print('-'*80)