-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_utils.py
60 lines (42 loc) · 1.67 KB
/
train_utils.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
#Forked from https://github.com/PavelOstyakov/toxic/blob/master/toxic/train_utils.py
from sklearn.metrics import log_loss
import numpy as np
def _train_model(model, batch_size, train_x, train_y, val_x, val_y):
best_loss = -1
best_weights = None
best_epoch = 0
current_epoch = 0
while True:
model.fit(train_x, train_y, batch_size=batch_size, epochs=1)
y_pred = model.predict(val_x, batch_size=batch_size)
total_loss = 0
for j in range(6):
loss = log_loss(val_y[:, j], y_pred[:, j])
total_loss += loss
total_loss /= 6.
print("Epoch {0} loss {1} best_loss {2}".format(current_epoch, total_loss, best_loss))
current_epoch += 1
if total_loss < best_loss or best_loss == -1:
best_loss = total_loss
best_weights = model.get_weights()
best_epoch = current_epoch
else:
if current_epoch - best_epoch == 5:
break
model.set_weights(best_weights)
return model
def train_folds(X, y, fold_count, batch_size, get_model_func):
fold_size = len(X) // fold_count
models = []
for fold_id in range(0, fold_count):
fold_start = fold_size * fold_id
fold_end = fold_start + fold_size
if fold_id == fold_size - 1:
fold_end = len(X)
train_x = np.concatenate([X[:fold_start], X[fold_end:]])
train_y = np.concatenate([y[:fold_start], y[fold_end:]])
val_x = X[fold_start:fold_end]
val_y = y[fold_start:fold_end]