-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_bl.py
162 lines (110 loc) · 4.65 KB
/
train_bl.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
import time
from tkinter import W
import numpy as np
from tqdm.autonotebook import tqdm
import torch
from torch.nn import BCEWithLogitsLoss
from torch.optim import Adam
from torch.optim.lr_scheduler import ExponentialLR
from data import get_dataset, get_dataloader
from model import get_model
from util import save_csv, cal_auc
device = "cuda:0" if torch.cuda.is_available() else "cpu"
def train_one_epoch(epoch, model, optimiser, data_loader, loss_function, scheduler=None):
losses = []
auc_scores = []
epoch_start = time.time()
model.train()
dataloader = tqdm(data_loader)
for _, (inputs, labels) in enumerate(dataloader, 0):
inputs, labels = inputs.to(device), labels.to(device)
# zero the parameter gradients
optimiser.zero_grad()
# forward
prob_preference = model(inputs)
# loss + backward
loss = loss_function(prob_preference, labels.float()) / (inputs.size()[0])
loss.backward()
losses.append(loss.item())
# auc
auc = cal_auc(labels, prob_preference)
auc_scores.append(auc)
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
# optimise (update weights)
optimiser.step()
dataloader.set_postfix(epoch=epoch, loss=np.mean(losses), auc=np.mean(auc_scores))
if scheduler is not None:
scheduler.step()
epoch_end = time.time()
epoch_time = epoch_end - epoch_start
return {'epoch': epoch, 'loss': np.mean(losses), 'auc': np.mean(auc_scores), 'time': epoch_time}
def valid_one_epoch(epoch, model, data_loader, loss_function):
losses = []
auc_scores = []
model.eval()
dataloader = tqdm(data_loader)
for _, (inputs, labels) in enumerate(dataloader, 0):
# get the inputs
inputs, labels = inputs.to(device), labels.to(device)
# forward
prob_preference = model(inputs)
# loss + backward
loss = loss_function(prob_preference, labels.float()) / (inputs.size()[0])
losses.append(loss.item())
# auc
auc = cal_auc(labels, prob_preference)
auc_scores.append(auc)
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
dataloader.set_postfix(epoch=epoch, loss=np.mean(losses), auc=np.mean(auc_scores))
return np.mean(losses), np.mean(auc_scores)
def test(model, data_loader, loss_function):
losses = []
auc_scores = []
model.eval()
dataloader = tqdm(data_loader)
for _, (inputs, labels) in enumerate(dataloader, 0):
# get the inputs
inputs, labels = inputs.to(device), labels.to(device)
# forward
prob_preference = model(inputs)
# loss + backward
loss = loss_function(prob_preference, labels.float()) / (inputs.size()[0])
losses.append(loss.item())
# auc
auc = cal_auc(labels, prob_preference)
auc_scores.append(auc)
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
# sparsity, params = calc_sparsity(model.embedding)
dataloader.set_postfix(loss=np.mean(losses), auc=np.mean(auc_scores))
return {'test_loss': np.mean(losses), 'test_auc': np.mean(auc_scores)}
def train(dataset, model,
epoch_size=30, log_path='./log/bl-{model_name}-{dataset_name}/{file_name}',
learning_rate=1e-3, weight_decay=1e-5, gamma=1):
print('*' * 10, 'Start Training', '*' * 18)
train_loader, valid_loader, test_loader = get_dataloader(dataset)
model = model.to(device)
print('model:', model)
loss_function = BCEWithLogitsLoss(reduction='sum')
optimiser = Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
scheduler = ExponentialLR(optimiser, gamma=gamma)
for epoch in range(epoch_size):
# Train
train_result = train_one_epoch(epoch, model, optimiser, train_loader, loss_function, scheduler)
# Vaildation
valid_loss, valid_auc = valid_one_epoch(epoch, model, valid_loader, loss_function)
train_result['valid_loss'] = valid_loss
train_result['valid_acc'] = valid_auc
result_path = log_path.format(
file_name='bl-train-log'
)
if epoch == 0:
header = ['epoch', 'loss', 'auc', 'time', 'valid_loss', 'valid_acc']
save_csv(result_path, train_result, header)
else:
save_csv(result_path, train_result, mode='a+')
# save embedding param every 3 epoch and test it
test_result = test(model, test_loader, loss_function)
test_path = log_path.format(file_name='test-log')
header = ['test_loss', 'test_auc']
save_csv(test_path, test_result, header)
print('**** Finished Training ****\n')