-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
125 lines (105 loc) · 4.68 KB
/
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
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
from keras.callbacks import EarlyStopping
from sklearn.cross_validation import KFold
import numpy as np
from keras import backend as K
# some global variables
max_features = 30000
maxlen = 200
embed_size_fastText = 300
embed_size_glove = 300
embed_size_glove_twitter = 200
batch_size = 128
epochs = 50
# utils functions
def run_5fold(x_train, y_train, x_test, early_stop_mon, get_model, **kwargs):
kf = KFold(len(x_train), n_folds=5)
loss_scores = []
acc_scores = []
roc_scores = []
if early_stop_mon == "auc_roc":
earlystop = EarlyStopping(monitor="val_auc_roc", min_delta=0, patience=1, verbose=1, mode="max")
elif early_stop_mon == "loss":
earlystop = EarlyStopping(monitor="val_loss", min_delta=0, patience=1, verbose=1, mode="auto")
else:
print ("No valid early stopping method")
return -1
y_preds = []
for train, test in kf:
model = get_model(**kwargs)
hist = model.fit(x_train[train], y_train[train],
batch_size=batch_size, epochs=epochs, verbose=2,
validation_data=(x_train[test], y_train[test]),
callbacks=[earlystop])
val_loss = hist.history["val_loss"][-1]
val_acc = hist.history["val_acc"][-1]
val_auc_roc = hist.history["val_auc_roc"][-1]
print ("val loss: {}".format(val_loss))
print ("val acc: {}".format(val_acc))
print ("val auc roc: {}\n".format(val_auc_roc))
loss_scores.append(val_loss)
acc_scores.append(val_acc)
roc_scores.append(val_auc_roc)
y_pred = model.predict(x_test, batch_size=batch_size)
y_preds.append(y_pred)
# release gpu memory
K.clear_session()
avg_val_loss = "loss: {} (+/- {})".format(np.mean(loss_scores), np.std(loss_scores))
avg_val_acc = "acc: {} (+/- {})".format(np.mean(acc_scores), np.std(acc_scores))
avg_roc_auc = "roc_auc: {} (+/- {})\n\n".format(np.mean(roc_scores), np.std(roc_scores))
result = {"avg_val_loss": avg_val_loss, "avg_val_acc": avg_val_acc, "avg_roc_auc": avg_roc_auc, "pred": y_preds}
return result
def run_5fold_2channels(x_trains, y_train, x_test, early_stop_mon, get_model, **kwargs):
# x_train1 and x_train2 are the same
x_train1 = x_trains[0]
x_train2 = x_trains[1]
kf1 = KFold(len(x_train1), n_folds=5)
loss_scores = []
acc_scores = []
roc_scores = []
if early_stop_mon == "auc_roc":
earlystop = EarlyStopping(monitor="val_auc_roc", min_delta=0, patience=1, verbose=1, mode="max")
elif early_stop_mon == "loss":
earlystop = EarlyStopping(monitor="val_loss", min_delta=0, patience=1, verbose=1, mode="auto")
else:
print ("No valid early stopping method")
return -1
y_preds = []
for train, test in kf1:
model = get_model(**kwargs)
hist = model.fit([x_train1[train], x_train2[train]], y_train[train],
batch_size=batch_size, epochs=epochs, verbose=2,
validation_data=([x_train1[test], x_train2[test]], y_train[test]),
callbacks=[earlystop])
val_loss = hist.history["val_loss"][-1]
val_acc = hist.history["val_acc"][-1]
val_auc_roc = hist.history["val_auc_roc"][-1]
print ("val loss: {}".format(val_loss))
print ("val acc: {}".format(val_acc))
print ("val auc roc: {}\n".format(val_auc_roc))
loss_scores.append(val_loss)
acc_scores.append(val_acc)
roc_scores.append(val_auc_roc)
y_pred = model.predict([x_test, x_test], batch_size=batch_size)
y_preds.append(y_pred)
# release gpu memory
K.clear_session()
avg_val_loss = "loss: {} (+/- {})".format(np.mean(loss_scores), np.std(loss_scores))
avg_val_acc = "acc: {} (+/- {})".format(np.mean(acc_scores), np.std(acc_scores))
avg_roc_auc = "roc_auc: {} (+/- {})\n\n".format(np.mean(roc_scores), np.std(roc_scores))
result = {"avg_val_loss": avg_val_loss, "avg_val_acc": avg_val_acc, "avg_roc_auc": avg_roc_auc, "pred": y_preds}
return result
def average_elementwise(ys):
y = np.mean(ys, axis=0)
return y
def write_prediction(ys, submission, output_file):
y_prediction = average_elementwise(ys)
submission[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]] = y_prediction
submission.to_csv(output_file, index=False)
def eval_results(results):
for key in results:
result = results[key]
print ("key: {}".format(key))
print (result["avg_val_loss"])
print (result["avg_val_acc"])
print (result["avg_roc_auc"])
print ()