-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient.py
99 lines (86 loc) · 3 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
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
# -*- 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 tqdm import tqdm
from get_data import nn_seq_wind
def get_val_loss(args, model, Val):
model.eval()
loss_function = nn.MSELoss().to(args.device)
val_loss = []
for (seq, label) in Val:
with torch.no_grad():
seq = seq.to(args.device)
label = label.to(args.device)
y_pred = model(seq)
loss = loss_function(y_pred, label)
val_loss.append(loss.item())
return np.mean(val_loss)
def train(args, model, server):
model.train()
Dtr, Val, 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)
# training
min_epochs = 10
best_model = None
min_val_loss = 5
print('training...')
loss_function = nn.MSELoss().to(args.device)
for epoch in tqdm(range(args.E)):
train_loss = []
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
train_loss.append(loss.item())
loss.backward()
optimizer.step()
stepLR.step()
# validation
val_loss = get_val_loss(args, model, Val)
if epoch + 1 >= min_epochs and val_loss < min_val_loss:
min_val_loss = val_loss
best_model = copy.deepcopy(model)
print('epoch {:03d} train_loss {:.8f} val_loss {:.8f}'.format(epoch, np.mean(train_loss), val_loss))
model.train()
return best_model
def test(args, ann):
ann.eval()
Dtr, Val, Dte = nn_seq_wind(ann.name, args.B)
pred = []
y = []
for (seq, target) in tqdm(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)))