-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom_net.py
163 lines (125 loc) · 5.7 KB
/
custom_net.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
from nolearn.lasagne import NeuralNet
import numpy as np
from time import time
class CustomAUCNeuralNet(NeuralNet):
def __init__(self, *args, **kwargs):
self.custom_train_scores = kwargs.pop("custom_train_scores", None)
super(CustomAUCNeuralNet, self).__init__(*args, **kwargs)
def train_loop(self, X, y, epochs=None):
epochs = epochs or self.max_epochs
X_train, X_valid, y_train, y_valid = self.train_split(X, y, self)
on_batch_finished = self.on_batch_finished
if not isinstance(on_batch_finished, (list, tuple)):
on_batch_finished = [on_batch_finished]
on_epoch_finished = self.on_epoch_finished
if not isinstance(on_epoch_finished, (list, tuple)):
on_epoch_finished = [on_epoch_finished]
on_training_started = self.on_training_started
if not isinstance(on_training_started, (list, tuple)):
on_training_started = [on_training_started]
on_training_finished = self.on_training_finished
if not isinstance(on_training_finished, (list, tuple)):
on_training_finished = [on_training_finished]
epoch = 0
best_valid_loss = (
min([row['valid_loss'] for row in self.train_history_]) if
self.train_history_ else np.inf
)
best_train_loss = (
min([row['train_loss'] for row in self.train_history_]) if
self.train_history_ else np.inf
)
for func in on_training_started:
func(self, self.train_history_)
num_epochs_past = len(self.train_history_)
while epoch < epochs:
epoch += 1
train_outputs = []
valid_outputs = []
if self.custom_scores:
custom_scores = [[] for _ in self.custom_scores]
else:
custom_scores = []
t0 = time()
"""
##############################################################
-> CUSTOM PART
"""
if self.custom_train_scores:
for Xb, yb in self.batch_iterator_train(X_train, y_train):
y_prob = self.apply_batch_func(self.predict_iter_, Xb)
for custom_scorer in self.custom_train_scores:
custom_scorer[1](yb, y_prob)
"""
##############################################################
"""
batch_train_sizes = []
for Xb, yb in self.batch_iterator_train(X_train, y_train):
train_outputs.append(
self.apply_batch_func(self.train_iter_, Xb, yb))
batch_train_sizes.append(len(Xb))
for func in on_batch_finished:
func(self, self.train_history_)
batch_valid_sizes = []
for Xb, yb in self.batch_iterator_test(X_valid, y_valid):
valid_outputs.append(
self.apply_batch_func(self.eval_iter_, Xb, yb))
batch_valid_sizes.append(len(Xb))
if self.custom_scores:
y_prob = self.apply_batch_func(self.predict_iter_, Xb)
for custom_scorer, custom_score in zip(
self.custom_scores, custom_scores):
custom_score.append(custom_scorer[1](yb, y_prob))
train_outputs = np.array(train_outputs, dtype=object).T
train_outputs = [
np.average(
[np.mean(row) for row in col],
weights=batch_train_sizes,
)
for col in train_outputs
]
if valid_outputs:
valid_outputs = np.array(valid_outputs, dtype=object).T
valid_outputs = [
np.average(
[np.mean(row) for row in col],
weights=batch_valid_sizes,
)
for col in valid_outputs
]
if custom_scores:
avg_custom_scores = np.average(
custom_scores, weights=batch_valid_sizes, axis=1)
if train_outputs[0] < best_train_loss:
best_train_loss = train_outputs[0]
if valid_outputs and valid_outputs[0] < best_valid_loss:
best_valid_loss = valid_outputs[0]
info = {
'epoch': num_epochs_past + epoch,
'train_loss': train_outputs[0],
'train_loss_best': best_train_loss == train_outputs[0],
'valid_loss': valid_outputs[0]
if valid_outputs else np.nan,
'valid_loss_best': best_valid_loss == valid_outputs[0]
if valid_outputs else np.nan,
'valid_accuracy': valid_outputs[1]
if valid_outputs else np.nan,
'dur': time() - t0,
}
if self.custom_scores:
for index, custom_score in enumerate(self.custom_scores):
info[custom_score[0]] = avg_custom_scores[index]
if self.scores_train:
for index, (name, func) in enumerate(self.scores_train):
info[name] = train_outputs[index + 1]
if self.scores_valid:
for index, (name, func) in enumerate(self.scores_valid):
info[name] = valid_outputs[index + 2]
self.train_history_.append(info)
try:
for func in on_epoch_finished:
func(self, self.train_history_)
except StopIteration:
break
for func in on_training_finished:
func(self, self.train_history_)