-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathml_trainer.py
334 lines (262 loc) · 14.6 KB
/
ml_trainer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from sklearn.model_selection import PredefinedSplit
from collections import defaultdict
import itertools as it
import torch
from architectures import CNN2Layers
from data_preparation import MyDataset
from assess_performance import ModelPerformance, PerformanceEpochs
from config import FileManager, GeneralInformation
from pytorchtools import EarlyStopping
class MLTrainer(object):
def __init__(self, pos_weights, batch_size=406):
self.batch_size = batch_size
if torch.cuda.is_available():
self.device = 'cuda:0'
else:
self.device = 'cpu'
self.pos_weights = torch.tensor(pos_weights).to(self.device)
def train_validate(self, params, train_ids, validation_ids, sequences, embeddings, labels, max_length,
verbose=True):
"""
Train & validate predictor for one set of parameters and ids
:param params:
:param train_ids:
:param validation_ids:
:param sequences:
:param embeddings:
:param labels:
:param max_length:
:param verbose:
:return:
"""
model, train_performance, val_performance = self._train_validate(params, train_ids, validation_ids, sequences,
embeddings, labels, max_length,
verbose=verbose)
train_loss, train_acc, train_prec, train_recall, train_f1, train_mcc = \
train_performance.get_performance_last_epoch()
val_loss, val_acc, val_prec, val_recall, val_f1, val_mcc = val_performance.get_performance_last_epoch()
print("Train loss: {:.3f}, Prec: {:.3f}, Recall: {:.3f}, F1: {:.3f}, MCC: {:.3f}".format(train_loss,
train_prec,
train_recall,
train_f1,
train_mcc))
print("Val loss: {:.3f}, Prec: {:.3f}, Recall: {:.3f}, F1: {:.3f}, MCC: {:.3f}".format(val_loss,
val_prec,
val_recall, val_f1,
val_mcc))
return model
def cross_validate(self, params, ids, fold_array, sequences, embeddings, labels, max_length, result_file):
"""
Perform cross-validation to optimize hyperparameters
:param params:
:param ids:
:param fold_array:
:param sequences:
:param embeddings:
:param labels:
:param max_length:
:param result_file:
:return:
"""
ps = PredefinedSplit(fold_array)
# create parameter grid
param_sets = defaultdict(dict)
sorted_keys = sorted(params.keys())
param_combos = it.product(*(params[s] for s in sorted_keys))
counter = 0
for p in list(param_combos):
curr_params = list(p)
param_dict = dict(zip(sorted_keys, curr_params))
param_sets[counter] = param_dict
counter += 1
best_score = 0
best_params = dict() # save best parameter set
best_classifier = None # save best classifier
performance = defaultdict(list) # save performance for each parameter combination
params_counter = 1
for p in param_sets.keys():
curr_params = param_sets[p]
print('{}\t{}'.format(params_counter, curr_params))
model = None
train_model_performance = ModelPerformance()
val_model_performance = ModelPerformance()
for train_index, test_index in ps.split():
train_ids, validation_ids = ids[train_index], ids[test_index]
model, train_performance, val_performance = self._train_validate(curr_params, train_ids, validation_ids,
sequences, embeddings, labels,
max_length)
train_loss, train_acc, train_prec, train_recall, train_f1, train_mcc = \
train_performance.get_performance_last_epoch()
val_loss, val_acc, val_prec, val_recall, val_f1, val_mcc = val_performance.get_performance_last_epoch()
train_model_performance.add_single_performance(train_loss, train_acc, train_prec, train_recall,
train_f1, train_mcc)
val_model_performance.add_single_performance(val_loss, val_acc, val_prec, val_recall, val_f1, val_mcc)
# take average over all splits
train_loss, train_acc, train_prec, train_recall, train_f1, train_mcc = \
train_model_performance.get_mean_performance()
val_loss, val_acc, val_prec, val_recall, val_f1, val_mcc = val_model_performance.get_mean_performance()
performance['train_precision'].append(train_prec)
performance['train_recall'].append(train_recall)
performance['train_f1'].append(train_f1)
performance['train_mcc'].append(train_mcc)
performance['train_acc'].append(train_acc)
performance['train_loss'].append(train_loss)
performance['val_precision'].append(val_prec)
performance['val_recall'].append(val_recall)
performance['val_f1'].append(val_f1)
performance['val_mcc'].append(val_mcc)
performance['val_acc'].append(val_acc)
performance['val_loss'].append(val_loss)
for param in curr_params.keys():
performance[param].append(curr_params[param])
if val_f1 > best_score:
best_score = val_f1
best_params = curr_params
best_classifier = model
params_counter += 1
FileManager.save_cv_results(performance, result_file)
print(best_score)
print(best_params)
return best_classifier
def _train_validate(self, params, train_ids, validation_ids, sequences, embeddings, labels, max_length,
verbose=True):
"""
Train and validate bindEmbed21DL model
:param params:
:param train_ids:
:param validation_ids:
:param sequences:
:param labels:
:param max_length:
:param verbose:
:return:
"""
# define data sets
train_set = MyDataset(train_ids, embeddings, sequences, labels, max_length)
validation_set = MyDataset(validation_ids, embeddings, sequences, labels, max_length)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=self.batch_size, shuffle=True, pin_memory=True,
worker_init_fn=GeneralInformation.seed_worker)
validation_loader = torch.utils.data.DataLoader(validation_set, batch_size=self.batch_size, shuffle=True,
pin_memory=True, worker_init_fn=GeneralInformation.seed_worker)
pos_weights = self.pos_weights.expand(max_length, 3)
pos_weights = pos_weights.t()
loss_fun = torch.nn.BCEWithLogitsLoss(reduction='none', pos_weight=pos_weights)
sigm = torch.nn.Sigmoid()
padding = int((params['kernel'] - 1) / 2)
model = CNN2Layers(train_set.get_input_dimensions(), params['features'], params['kernel'], params['stride'],
padding, params['dropout'])
model.to(self.device)
optim_args = {'lr': params['lr'], 'betas': params['betas'], 'eps': params['eps'],
'weight_decay': params['weight_decay']}
optimizer = torch.optim.Adamax(model.parameters(), **optim_args)
checkpoint_file = 'checkpoint_early_stopping.pt'
early_stopping = EarlyStopping(patience=10, delta=0.01, checkpoint_file=checkpoint_file)
train_performance = PerformanceEpochs()
validation_performance = PerformanceEpochs()
num_epochs = 0
for epoch in range(params['epochs']):
if verbose:
print("Epoch {}".format(epoch))
train_loss = val_loss = 0
train_loss_count = val_loss_count = 0
train_tp = train_tn = train_fn = train_fp = 0
val_tp = val_tn = val_fn = val_fp = 0
train_acc = train_prec = train_rec = train_f1 = train_mcc = 0
val_acc = val_prec = val_rec = val_f1 = val_mcc = 0
# training
model.train()
for in_feature, target, loss_mask in train_loader:
optimizer.zero_grad()
in_feature = in_feature.to(self.device)
in_feature_1024 = in_feature[:, :-1, :]
target = target.to(self.device)
loss_mask = loss_mask.to(self.device)
pred = model.forward(in_feature_1024)
# don't consider padded positions for loss calculation
loss_el = loss_fun(pred, target)
loss_el_masked = loss_el * loss_mask
loss_norm = torch.sum(loss_el_masked)
train_loss += loss_norm.item()
train_loss_count += in_feature.shape[0]
for idx, i in enumerate(in_feature): # remove padded positions to calculate tp, fp, tn, fn
pred_i, target_i = GeneralInformation.remove_padded_positions(pred[idx], target[idx], i)
pred_i = sigm(pred_i)
tp, fp, tn, fn, acc, prec, rec, f1, mcc = \
train_performance.get_performance_batch(pred_i.detach().cpu(), target_i.detach().cpu())
train_tp += tp
train_fp += fp
train_tn += tn
train_fn += fn
train_acc += acc
train_prec += prec
train_rec += rec
train_f1 += f1
train_mcc += mcc
loss_norm.backward()
optimizer.step()
# validation
model.eval()
with torch.no_grad():
for in_feature, target, loss_mask in validation_loader:
in_feature = in_feature.to(self.device)
in_feature_1024 = in_feature[:, :-1, :]
target = target.to(self.device)
loss_mask = loss_mask.to(self.device)
pred = model.forward(in_feature_1024)
# don't consider padded position for loss calculation
loss_el = loss_fun(pred, target)
loss_el_masked = loss_el * loss_mask
val_loss += torch.sum(loss_el_masked).item()
val_loss_count += in_feature.shape[0]
for idx, i in enumerate(in_feature): # remove padded positions to calculate tp, fp, tn, fn
pred_i, target_i = GeneralInformation.remove_padded_positions(pred[idx], target[idx], i)
pred_i = sigm(pred_i)
tp, fp, tn, fn, acc, prec, rec, f1, mcc = \
train_performance.get_performance_batch(pred_i.detach().cpu(), target_i.detach().cpu())
val_tp += tp
val_fp += fp
val_tn += tn
val_fn += fn
val_acc += acc
val_prec += prec
val_rec += rec
val_f1 += f1
val_mcc += mcc
train_loss = train_loss / (train_loss_count * 3)
val_loss = val_loss / (val_loss_count * 3)
train_acc = train_acc / train_loss_count
train_prec = train_prec / train_loss_count
train_rec = train_rec / train_loss_count
train_f1 = train_f1 / train_loss_count
train_mcc = train_mcc / train_loss_count
val_acc = val_acc / val_loss_count
val_prec = val_prec / val_loss_count
val_rec = val_rec / val_loss_count
val_f1 = val_f1 / val_loss_count
val_mcc = val_mcc / val_loss_count
if verbose:
print("Train loss: {:.3f}, Prec: {:.3f}, Recall: {:.3f}, F1: {:.3f}, MCC: {:.3f}".format(train_loss,
train_prec,
train_rec,
train_f1,
train_mcc))
print('TP: {}, FP: {}, TN: {}, FN: {}'.format(train_tp, train_fp, train_tn, train_fn))
print("Val loss: {:.3f}, Prec: {:.3f}, Recall: {:.3f}, F1: {:.3f}, MCC: {:.3f}".format(val_loss,
val_prec,
val_rec, val_f1,
val_mcc))
print('TP: {}, FP: {}, TN: {}, FN: {}'.format(val_tp, val_fp, val_tn, val_fn))
# append average performance for this epoch
train_performance.add_performance_epoch(train_loss, train_mcc, train_prec, train_rec, train_f1, train_acc)
validation_performance.add_performance_epoch(val_loss, val_mcc, val_prec, val_rec, val_f1, val_acc)
num_epochs += 1
# stop training if F1 score doesn't improve anymore
if 'early_stopping' in params.keys() and params['early_stopping']:
eval_val = val_f1 * (-1)
# eval_val = val_loss
early_stopping(eval_val, model, verbose)
if early_stopping.early_stop:
break
if 'early_stopping' in params.keys() and params['early_stopping']: # load best model
model = torch.load(checkpoint_file)
return model, train_performance, validation_performance