-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkeras_wrapper.py
More file actions
401 lines (330 loc) · 13.1 KB
/
keras_wrapper.py
File metadata and controls
401 lines (330 loc) · 13.1 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from __future__ import absolute_import
import abc
import copy
import numpy as np
from keras.utils.np_utils import to_categorical
class BaseWrapper(object):
"""
Base class for the Keras scikit-learn wrapper.
Warning: This class should not be used directly. Use derived classes instead.
Parameters
----------
train_batch_size : int, optional
Number of training samples evaluated at a time.
test_batch_size : int, optional
Number of test samples evaluated at a time.
nb_epochs : int, optional
Number of training epochs.
shuffle : boolean, optional
Whether to shuffle the samples at each epoch.
show_accuracy : boolean, optional
Whether to display class accuracy in the logs at each epoch.
validation_split : float [0, 1], optional
Fraction of the data to use as held-out validation data.
validation_data : tuple (X, y), optional
Data to be used as held-out validation data. Will override validation_split.
callbacks : list, optional
List of callbacks to apply during training.
verbose : int, optional
Verbosity level.
"""
__metaclass__ = abc.ABCMeta
class Hideout(object):
"""
Class to hide a model from scikit learn,
so that sklearn doen't find its get_params method.
"""
def __init__(self, model):
self.model = model
@abc.abstractmethod
def __init__(self, model, optimizer, loss,
train_batch_size=128, test_batch_size=128,
nb_epoch=100, shuffle=True, show_accuracy=False,
validation_split=0, validation_data=None, callbacks=None,
verbose=0):
# The model should be in a hideout so that sklearn doesn't find the get_params
# method. That get_params method causes problems to
# sklean as it doesn't speak the same language (doesn't return a dict)
# As no get_params method will be found be sklearn clone, the Hideout object
# will be deepcopied when being cloned by sklearn, as expected.
# If is already hidden, do nothing. Else, hide it.
if isinstance(model, self.Hideout):
self.model = model
else:
self.model = self.Hideout(model)
self.optimizer = optimizer
self.loss = loss
self._compiled_model = None
self.config_ = []
self.weights_ = []
self.train_batch_size = train_batch_size
self.test_batch_size = test_batch_size
self.nb_epoch = nb_epoch
self.shuffle = shuffle
self.show_accuracy = show_accuracy
self.validation_split = validation_split
self.validation_data = validation_data
self.callbacks = [] if callbacks is None else callbacks
self.verbose = verbose
def get_params(self, deep=True):
"""
Get parameters for this estimator.
Parameters
----------
deep: boolean, optional
An empty placeholder, for compatibility with sklearn.
Returns
-------
params : dict
Dictionary of parameter names mapped to their values.
"""
return {'model': self.model,
'optimizer': self.optimizer,
'loss': self.loss,
'train_batch_size': self.train_batch_size,
'test_batch_size': self.test_batch_size,
'nb_epoch': self.nb_epoch,
'shuffle': self.shuffle,
'show_accuracy': self.show_accuracy,
'validation_split': self.validation_split,
'validation_data': self.validation_data,
'callbacks': self.callbacks,
'verbose': self.verbose
}
def set_params(self, **params):
"""
Set the parameters of this estimator.
Parameters
----------
params: dict
Dictionary of parameter names mapped to their values.
Returns
-------
self
"""
for parameter, value in params.items():
setattr(self, parameter, value)
return self
def fit(self, X, y, **kwargs):
"""
Fit the model according to the given training data.
Makes a copy of the un-compiled model definition to use for
compilation and fitting, leaving the original definition
intact.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Training samples where n_samples in the number of samples
and n_features is the number of features.
y : array-like, shape = (n_samples) or (n_samples, n_outputs)
True labels for X.
Returns
-------
history : object
Returns details about the training history at each epoch.
"""
self._compiled_model = copy.deepcopy(self.model.model)
self._compiled_model.compile(optimizer=self.optimizer, loss=self.loss)
history = self._compiled_model.fit(
X, y, batch_size=self.train_batch_size, nb_epoch=self.nb_epoch, verbose=self.verbose,
shuffle=self.shuffle, show_accuracy=self.show_accuracy,
validation_split=self.validation_split, validation_data=self.validation_data,
callbacks=self.callbacks, **kwargs)
self.config_ = self._compiled_model.get_config()
self.weights_ = self._compiled_model.get_weights()
return history
class KerasClassifier(BaseWrapper):
"""
Implementation of the scikit-learn classifier API for Keras.
Parameters
----------
model : object
An un-compiled Keras model object is required to use the scikit-learn wrapper.
optimizer : string
Optimization method used by the model during compilation/training.
loss : string
Loss function used by the model during compilation/training.
"""
def __init__(self,
model,
optimizer='adam',
loss='categorical_crossentropy',
train_batch_size=128,
test_batch_size=128,
nb_epoch=100,
shuffle=True,
show_accuracy=False,
validation_split=0,
validation_data=None,
callbacks=None,
verbose=0,
class_weight=None):
super(KerasClassifier, self).__init__(model,
optimizer,
loss,
train_batch_size,
test_batch_size,
nb_epoch,
shuffle,
show_accuracy,
validation_split,
validation_data,
callbacks,
verbose)
self.classes_ = []
self.class_weight = class_weight
def _ohe_output(function):
"""
Creates a decorator to be used with methods that have as input X, y.
It will transform the y into a categorical variable if y is 1-dim and
the the loss function is 'categorical_crossentropy'
"""
def wrapper(self, X, y, *args, **kwargs):
if len(y.shape) == 1 and self.loss == 'categorical_crossentropy':
y = to_categorical(y)
return function(self, X, y, *args, **kwargs)
return wrapper
@_ohe_output
def fit(self, X, y):
"""
Fit the model according to the given training data.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Training samples where n_samples in the number of samples
and n_features is the number of features.
y : array-like, shape = (n_samples) or (n_samples, n_outputs)
True labels for X.
Returns
-------
history : object
Returns details about the training history at each epoch.
"""
if len(y.shape) == 1:
self.classes_ = list(np.unique(y))
else:
self.classes_ = np.arange(0, y.shape[1])
# If 'balanced', it balances the dataset with weights that are the inverse
# of the class frequency.
if self.class_weight == 'balanced':
if len(y.shape)>1:
inverse_freqs = float(y.sum())/y.sum(axis=0)
class_weight = dict((i, inverse_freq) for i, inverse_freq in enumerate(inverse_freqs))
elif len(y.shape)==1:
unique, counts = np.unique(y, return_counts=True)
inverse_freqs = float(counts.sum())/counts
class_weight = dict(zip(unique, inverse_freqs))
else:
class_weight = self.class_weight
return super(KerasClassifier, self).fit(X, y, class_weight=class_weight)
def get_params(self, deep=True):
"""
Get parameters for this estimator.
Parameters
----------
deep: boolean, optional
An empty placeholder, for compatibility with sklearn.
Returns
-------
params : dict
Dictionary of parameter names mapped to their values.
"""
params = super(KerasClassifier, self).get_params(deep=deep)
params['class_weight'] = self.class_weight
return params
def predict(self, X):
"""
Returns the class predictions for the given test data.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Test samples where n_samples in the number of samples
and n_features is the number of features.
Returns
-------
preds : array-like, shape = (n_samples)
Class predictions.
"""
return self._compiled_model.predict_classes(
X, batch_size=self.test_batch_size, verbose=self.verbose)
def predict_proba(self, X):
"""
Returns class probability estimates for the given test data.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Test samples where n_samples in the number of samples
and n_features is the number of features.
Returns
-------
proba : array-like, shape = (n_samples, n_outputs)
Class probability estimates.
"""
return self._compiled_model.predict_proba(
X, batch_size=self.test_batch_size, verbose=self.verbose)
@_ohe_output
def score(self, X, y):
"""
Returns the mean accuracy on the given test data and labels.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Test samples where n_samples in the number of samples
and n_features is the number of features.
y : array-like, shape = (n_samples) or (n_samples, n_outputs)
True labels for X.
Returns
-------
score : float
Mean accuracy of predictions on X wrt. y.
"""
loss, accuracy = self._compiled_model.evaluate(
X, y, batch_size=self.test_batch_size, show_accuracy=True, verbose=self.verbose)
return accuracy
class KerasRegressor(BaseWrapper):
"""
Implementation of the scikit-learn regressor API for Keras.
Parameters
----------
model : object
An un-compiled Keras model object is required to use the scikit-learn wrapper.
optimizer : string
Optimization method used by the model during compilation/training.
loss : string
Loss function used by the model during compilation/training.
"""
def __init__(self, model, optimizer='adam', loss='mean_squared_error', **kwargs):
super(KerasRegressor, self).__init__(model, optimizer, loss, **kwargs)
def predict(self, X):
"""
Returns predictions for the given test data.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Test samples where n_samples in the number of samples
and n_features is the number of features.
Returns
-------
preds : array-like, shape = (n_samples)
Predictions.
"""
return self._compiled_model.predict(
X, batch_size=self.test_batch_size, verbose=self.verbose).ravel()
def score(self, X, y):
"""
Returns the mean accuracy on the given test data and labels.
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Test samples where n_samples in the number of samples
and n_features is the number of features.
y : array-like, shape = (n_samples)
True labels for X.
Returns
-------
score : float
Loss from predictions on X wrt. y.
"""
loss = self._compiled_model.evaluate(
X, y, batch_size=self.test_batch_size, show_accuracy=False, verbose=self.verbose)
return loss