-
Notifications
You must be signed in to change notification settings - Fork 1
/
clf_nolearn_simple_play.py
263 lines (197 loc) · 9.19 KB
/
clf_nolearn_simple_play.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
"""
"""
import numpy as np
from nolearn.lasagne import NeuralNet
from nolearn.lasagne import BatchIterator
from lasagne.layers import InputLayer, DenseLayer, DropoutLayer, NINLayer
from lasagne.nonlinearities import LeakyRectify, softmax
from lasagne.init import HeNormal
from lasagne.updates import adagrad
from sklearn.calibration import CalibratedClassifierCV
from sklearn.base import BaseEstimator
from sklearn.cross_validation import StratifiedKFold
from copy import deepcopy
from classifier import logloss_mc
from theano import shared
from clf_nolearn import Clf_nolearn, My_NeuralNet, EarlyStopping, AdjustVariable, float32, OneOneStopping
import pdb
class ClfCal(BaseEstimator):
"""
"""
def __init__(self, nn):
super(ClfCal, self).__init__()
self.nn = nn
def fit(self, X, y):
self.nn.fit(X, y)
return self
def predict_proba(self, X):
y_pred = self.nn.predict_proba(X)
return y_pred
class Clf_nolearn_simple_play(Clf_nolearn):
"""
Simple nolearn based classifier...
"""
def __init__(self, num_features=93, num_classes=9):
"""
"""
self.prefix = 'nl_simp_play'
self.num_features = num_features
self.num_classes = num_classes
self.layers = [('input', InputLayer),
('dropouti', DropoutLayer),
('dense0', DenseLayer),
('dropout0', DropoutLayer),
('dense1', DenseLayer),
('dropout1', DropoutLayer),
('dense2', DenseLayer),
('dropout2', DropoutLayer),
('dense3', DenseLayer),
('dropout3', DropoutLayer),
('output', DenseLayer)]
# self.early_stopping = EarlyStopping(patience=25)
self.oneone_stopping = OneOneStopping(ratio=0.9)
self.oneone_stopping2 = OneOneStopping(ratio=0.9)
self.oneone_stopping3 = OneOneStopping(ratio=0.85)
self.nn = My_NeuralNet(layers=self.layers,
input_shape=(None, self.num_features),
dropouti_p=0.1,
dense0_num_units=900,
dense0_W=HeNormal(),
dense0_nonlinearity = LeakyRectify(leakiness=0.05),
dropout0_p=0.45,
dense1_num_units=600,
dense1_W=HeNormal(),
dense1_nonlinearity = LeakyRectify(leakiness=0.05),
dropout1_p=0.3,
dense2_num_units=400,
dense2_W=HeNormal(),
dense2_nonlinearity = LeakyRectify(leakiness=0.05),
dropout2_p=0.2,
dense3_num_units=300,
dense3_W=HeNormal(),
dense3_nonlinearity = LeakyRectify(leakiness=0.05),
dropout3_p=0.1,
output_num_units=num_classes,
output_nonlinearity=softmax,
update=adagrad,
update_learning_rate=shared(float32(0.01)),
batch_iterator_train=BatchIterator(batch_size=256),
on_epoch_finished=[
AdjustVariable('update_learning_rate', start=0.01,
stop=0.005),
# AdjustVariable('update_momentum', start=0.1, stop=0.0),
self.oneone_stopping,
],
eval_size=0.,
verbose=1,
max_epochs=400
)
self.nnt = deepcopy(self.nn)
self.nn2 = My_NeuralNet(layers=self.layers,
input_shape=(None, self.num_features),
dropouti_p=0.12,
dense0_num_units=900,
dense0_W=HeNormal(),
dense0_nonlinearity = LeakyRectify(leakiness=0.05),
dropout0_p=0.47,
dense1_num_units=600,
dense1_W=HeNormal(),
dense1_nonlinearity = LeakyRectify(leakiness=0.05),
dropout1_p=0.32,
dense2_num_units=400,
dense2_W=HeNormal(),
dense2_nonlinearity = LeakyRectify(leakiness=0.05),
dropout2_p=0.22,
dense3_num_units=300,
dense3_W=HeNormal(),
dense3_nonlinearity = LeakyRectify(leakiness=0.05),
dropout3_p=0.12,
output_num_units=self.num_classes,
output_nonlinearity=softmax,
update=adagrad,
update_learning_rate=shared(float32(0.01)),
batch_iterator_train=BatchIterator(batch_size=256),
on_epoch_finished=[
AdjustVariable('update_learning_rate', start=0.01,
stop=0.005),
# AdjustVariable('update_momentum', start=0.1, stop=0.0),
# self.early_stopping2,
self.oneone_stopping2,
],
eval_size=0.,
verbose=1,
max_epochs=400
)
self.nn2t = deepcopy(self.nn2)
self.nn3 = My_NeuralNet(layers=self.layers,
input_shape=(None, self.num_features),
dropouti_p=0.14,
dense0_num_units=900,
dense0_W=HeNormal(),
dense0_nonlinearity = LeakyRectify(leakiness=0.05),
dropout0_p=0.49,
dense1_num_units=600,
dense1_W=HeNormal(),
dense1_nonlinearity = LeakyRectify(leakiness=0.05),
dropout1_p=0.34,
dense2_num_units=400,
dense2_W=HeNormal(),
dense2_nonlinearity = LeakyRectify(leakiness=0.05),
dropout2_p=0.24,
dense3_num_units=300,
dense3_W=HeNormal(),
dense3_nonlinearity = LeakyRectify(leakiness=0.05),
dropout3_p=0.14,
output_num_units=self.num_classes,
output_nonlinearity=softmax,
update=adagrad,
update_learning_rate=shared(float32(0.01)),
batch_iterator_train=BatchIterator(batch_size=256),
on_epoch_finished=[
AdjustVariable('update_learning_rate', start=0.01,
stop=0.005),
# AdjustVariable('update_momentum', start=0.1, stop=0.0),
# self.early_stopping,
self.oneone_stopping3,
],
eval_size=0.,
verbose=1,
max_epochs=400
)
self.nn3t = deepcopy(self.nn3)
def train_validate(self, X_train, y_train, X_valid, y_valid):
"""
"""
self.nn.max_epochs = 300
self.nn.verbose=1
self.nn.fit(X_train, y_train, X_valid, y_valid)
params = self.nn.get_all_params_values()
self.nn2.load_params_from(params)
self.nn2.fit(X_train, y_train, X_valid, y_valid)
params2 = self.nn2.get_all_params_values()
self.nn3.load_params_from(params2)
self.nn3.fit(X_train, y_train, X_valid, y_valid)
yp0 = self.nn3.predict_proba(X_valid)
print 'Nolearn log-loss: %s'%(logloss_mc(y_valid, yp0))
y_pred = yp0
# pdb.set_trace()
return y_pred
def train_test(self, X, y, X_test):
"""
"""
self.nnt.max_epochs = self.oneone_stopping.best_valid_epoch
self.nnt.fit(X, y)
params = self.nnt.get_all_params_values()
self.nn2t.load_params_from(params)
self.nn2t.max_epochs = self.oneone_stopping2.best_valid_epoch
self.nn2t.fit(X, y)
params2 = self.nn2t.get_all_params_values()
self.nn3t.load_params_from(params2)
self.nn3t.max_epochs = self.oneone_stopping3.best_valid_epoch
self.nn3t.fit(X, y)
yp0 = self.nn3t.predict_proba(X_test)
# self.cal_clf.fit(X, y)
# yp1 = self.cal_clf.predict_proba(X_test)
# y_pred = (yp0 + yp1)/2.
y_pred = yp0
return y_pred