forked from ki-ljl/FedProx-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
71 lines (62 loc) · 2.13 KB
/
client.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
# -*- coding:utf-8 -*-
"""
@Time: 2022/03/03 12:25
@Author: KI
@File: client.py
@Motto: Hungry And Humble
"""
import copy
from itertools import chain
import numpy as np
import torch
from torch.optim.lr_scheduler import StepLR
from sklearn.metrics import mean_absolute_error, mean_squared_error
from torch import nn
from data_process import nn_seq_wind
def train(args, model, server):
model.train()
Dtr, Dte = nn_seq_wind(model.name, args.B)
model.len = len(Dtr)
global_model = copy.deepcopy(server)
lr = args.lr
if args.optimizer == 'adam':
optimizer = torch.optim.Adam(model.parameters(), lr=lr,
weight_decay=args.weight_decay)
else:
optimizer = torch.optim.SGD(model.parameters(), lr=lr,
momentum=0.9, weight_decay=args.weight_decay)
stepLR = StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)
print('training...')
loss_function = nn.MSELoss().to(args.device)
loss = 0
for epoch in range(args.E):
for (seq, label) in Dtr:
seq = seq.to(args.device)
label = label.to(args.device)
y_pred = model(seq)
optimizer.zero_grad()
# compute proximal_term
proximal_term = 0.0
for w, w_t in zip(model.parameters(), global_model.parameters()):
proximal_term += (w - w_t).norm(2)
loss = loss_function(y_pred, label) + (args.mu / 2) * proximal_term
loss.backward()
optimizer.step()
stepLR.step()
print('epoch', epoch, ':', loss.item())
return model
def test(args, ann):
ann.eval()
Dtr, Dte = nn_seq_wind(ann.name, args.B)
pred = []
y = []
for (seq, target) in Dte:
with torch.no_grad():
seq = seq.to(args.device)
y_pred = ann(seq)
pred.extend(list(chain.from_iterable(y_pred.data.tolist())))
y.extend(list(chain.from_iterable(target.data.tolist())))
pred = np.array(pred)
y = np.array(y)
print('mae:', mean_absolute_error(y, pred), 'rmse:',
np.sqrt(mean_squared_error(y, pred)))