-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathestimator.py
More file actions
449 lines (393 loc) · 16.2 KB
/
estimator.py
File metadata and controls
449 lines (393 loc) · 16.2 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
"""
import cPickle
import copy
from functools import partial
from multiprocessing import Process, Pipe
import time
import numpy as np
import hyperopt
import scipy.sparse
from . import components
# Constants for partial_fit
# The partial_fit method will not be run if there is less than
# timeout * timeout_buffer number of seconds left before timeout
timeout_buffer = 0.05
# The minimum number of iterations of the partial_fit method that must be run
# before early stopping can kick in is min_n_iters
min_n_iters = 7
# After best_loss_cutoff_n_iters iterations have occured, the training can be
# stopped early if the validation scores are far from the best scores
best_loss_cutoff_n_iters = 35
# Early stopping can occur when the best validation score of the earlier runs is
# greater than that of the later runs, tipping_pt_ratio determines the split
tipping_pt_ratio = 0.6
# Retraining will be done with all training data for retrain_fraction
# multiplied by the number of iterations used to train the original classifier
retrain_fraction = 1.2
class NonFiniteFeature(Exception):
"""
"""
def _cost_fn(argd, Xfit, yfit, Xval, yval, info, timeout,
_conn, best_loss=None):
try:
t_start = time.time()
if 'classifier' in argd:
classifier = argd['classifier']
preprocessings = argd['preprocessing']
else:
classifier = argd['model']['classifier']
preprocessings = argd['model']['preprocessing']
untrained_classifier = copy.deepcopy( classifier )
# -- N.B. modify argd['preprocessing'] in-place
for pp_algo in preprocessings:
info('Fitting', pp_algo, 'to X of shape', Xfit.shape)
pp_algo.fit(Xfit)
info('Transforming fit and Xval', Xfit.shape, Xval.shape)
Xfit = pp_algo.transform(Xfit)
Xval = pp_algo.transform(Xval)
# np.isfinite() does not work on sparse matrices
if not (scipy.sparse.issparse(Xfit) or scipy.sparse.issparse(Xval)):
if not (
np.all(np.isfinite(Xfit))
and np.all(np.isfinite(Xval))):
# -- jump to NonFiniteFeature handler below
raise NonFiniteFeature(pp_algo)
info('Training classifier', classifier,
'on X of dimension', Xfit.shape)
def should_stop(scores):
#TODO: possibly extend min_n_iters based on how close the current
# score is to the best score, up to some larger threshold
if len(scores) < min_n_iters:
return False
tipping_pt = int(tipping_pt_ratio * len(scores))
early_scores = scores[:tipping_pt]
late_scores = scores[tipping_pt:]
if max(early_scores) >= max(late_scores):
return True
#TODO: make this less confusing and possibly more accurate
if len(scores) > best_loss_cutoff_n_iters and \
max(scores) < 1 - best_loss and \
3 * ( max(late_scores) - max(early_scores) ) < \
1 - best_loss - max(late_scores):
info("stopping early due to best_loss cutoff criterion")
return True
return False
n_iters = 0 # Keep track of the number of training iterations
best_classifier = None
if hasattr( classifier, "partial_fit" ):
if timeout is not None:
timeout_tolerance = timeout * timeout_buffer
rng = np.random.RandomState(6665)
train_idxs = rng.permutation(Xfit.shape[0])
validation_scores = []
while timeout is not None and \
time.time() - t_start < timeout - timeout_tolerance:
n_iters += 1
rng.shuffle(train_idxs)
classifier.partial_fit(Xfit[train_idxs], yfit[train_idxs],
classes=np.unique( yfit ))
validation_scores.append(classifier.score(Xval, yval))
if max(validation_scores) == validation_scores[-1]:
best_classifier = copy.deepcopy(classifier)
if should_stop(validation_scores):
break
info('VSCORE', validation_scores[-1])
classifier = best_classifier
else:
classifier.fit( Xfit, yfit )
if classifier is None:
t_done = time.time()
rval = {
'status': hyperopt.STATUS_FAIL,
'failure': 'Not enough time to train anything',
'duration': t_done - t_start,
}
rtype = 'return'
else:
info('Scoring on Xval of shape', Xval.shape)
loss = 1.0 - classifier.score(np.asarray(Xval),
np.asarray(yval))
# -- squared standard error of mean
lossvar = (loss * (1 - loss)) / max(1, len(yval) - 1)
info('OK trial with accuracy %.1f +- %.1f' % (
100 * (1.0 - loss),
100 * np.sqrt(lossvar)))
t_done = time.time()
rval = {
'loss': loss,
'loss_variance': lossvar,
'classifier': untrained_classifier,
'preprocs': preprocessings,
'status': hyperopt.STATUS_OK,
'duration': t_done - t_start,
'iterations': n_iters,
}
rtype = 'return'
except (NonFiniteFeature,), exc:
print 'Failing trial due to NaN in', str(exc)
t_done = time.time()
rval = {
'status': hyperopt.STATUS_FAIL,
'failure': str(exc),
'duration': t_done - t_start,
}
rtype = 'return'
except (ValueError,), exc:
if ('k must be less than or equal'
' to the number of training points') in str(exc):
t_done = time.time()
rval = {
'status': hyperopt.STATUS_FAIL,
'failure': str(exc),
'duration': t_done - t_start,
}
rtype = 'return'
elif 'overflow' in str(exc):
t_done = time.time()
rval = {
'status': hyperopt.STATUS_FAIL,
'failure': str(exc),
'duration': t_done - t_start,
}
rtype = 'return'
else:
rval = exc
rtype = 'raise'
except (MemoryError,), exc:
t_done = time.time()
rval = {
'status': hyperopt.STATUS_FAIL,
'failure': str(exc),
'duration': t_done - t_start,
}
rtype = 'return'
except (AttributeError,), exc:
print 'Failing due to k_means_ weirdness'
if "'NoneType' object has no attribute 'copy'" in str(exc):
# -- sklearn/cluster/k_means_.py line 270 raises this sometimes
t_done = time.time()
rval = {
'status': hyperopt.STATUS_FAIL,
'failure': str(exc),
'duration': t_done - t_start,
}
rtype = 'return'
else:
rval = exc
rtype = 'raise'
except Exception, exc:
rval = exc
rtype = 'raise'
# -- return the result to calling process
_conn.send((rtype, rval))
class hyperopt_estimator(object):
def __init__(self,
preprocessing=None,
classifier=None,
space=None,
algo=None,
max_evals=100,
verbose=0,
trial_timeout=None,
fit_increment=1,
fit_increment_dump_filename=None,
seed=None,
):
"""
Parameters
----------
preprocessing: pyll.Apply node
This should evaluate to a list of sklearn-style preprocessing
modules (may include hyperparameters).
classifier: pyll.Apply node
This should evaluates to sklearn-style classifier (may include
hyperparameters).
algo: hyperopt suggest algo (e.g. rand.suggest)
max_evals: int
Fit() will evaluate up to this-many configurations. Does not apply
to fit_iter, which continues to search indefinitely.
trial_timeout: float (seconds), or None for no timeout
Kill trial evaluations after this many seconds.
fit_increment: int
Every this-many trials will be a synchronization barrier for
ongoing trials, and the hyperopt Trials object may be
check-pointed. (Currently evaluations are done serially, but
that might easily change in future to allow e.g. MongoTrials)
fit_increment_dump_filename : str or None
Periodically dump self.trials to this file (via cPickle) during
fit() Saves after every `fit_increment` trial evaluations.
"""
self.max_evals = max_evals
self.verbose = verbose
self.trial_timeout = trial_timeout
self.fit_increment = fit_increment
self.fit_increment_dump_filename = fit_increment_dump_filename
if space is None:
if classifier is None:
classifier = components.any_classifier('classifier')
if preprocessing is None:
preprocessing = components.any_preprocessing('preprocessing')
self.space = hyperopt.pyll.as_apply({
'classifier': classifier,
'preprocessing': preprocessing,
})
else:
assert classifier is None
assert preprocessing is None
self.space = hyperopt.pyll.as_apply(space)
if algo is None:
self.algo=hyperopt.rand.suggest
else:
self.algo = algo
if seed is not None:
self.rstate = np.random.RandomState(seed)
else:
self.rstate = np.random.RandomState()
def info(self, *args):
if self.verbose:
print ' '.join(map(str, args))
def fit_iter(self, X, y, weights=None, increment=None):
"""Generator of Trials after ever-increasing numbers of evaluations
"""
assert weights is None
increment = self.fit_increment if increment is None else increment
# len does not work on sparse matrices, so using shape[0] instead
# shape[0] does not work on lists, so using len() for those
if scipy.sparse.issparse(X):
data_length = X.shape[0]
else:
data_length = len(X)
if type(X) is list:
X = np.array(X)
if type(y) is list:
y = np.array(y)
p = np.random.RandomState(123).permutation( data_length )
n_fit = int(.8 * data_length)
Xfit = X[p[:n_fit]]
yfit = y[p[:n_fit]]
Xval = X[p[n_fit:]]
yval = y[p[n_fit:]]
self.trials = hyperopt.Trials()
self._best_loss = float('inf')
fn=partial(_cost_fn,
Xfit=Xfit, yfit=yfit,
Xval=Xval, yval=yval,
info=self.info,
timeout=self.trial_timeout)
self._best_loss = float('inf')
def fn_with_timeout(*args, **kwargs):
conn1, conn2 = Pipe()
kwargs['_conn'] = conn2
th = Process(target=partial(fn, best_loss=self._best_loss),
args=args, kwargs=kwargs)
th.start()
if conn1.poll(self.trial_timeout):
fn_rval = conn1.recv()
th.join()
else:
print 'TERMINATING DUE TO TIMEOUT'
th.terminate()
th.join()
fn_rval = 'return', {
'status': hyperopt.STATUS_FAIL,
'failure': 'TimeOut'
}
assert fn_rval[0] in ('raise', 'return')
if fn_rval[0] == 'raise':
raise fn_rval[1]
# -- remove potentially large objects from the rval
# so that the Trials() object below stays small
# We can recompute them if necessary, and it's usually
# not necessary at all.
if fn_rval[1]['status'] == hyperopt.STATUS_OK:
fn_loss = float(fn_rval[1].get('loss'))
fn_preprocs = fn_rval[1].pop('preprocs')
fn_classif = fn_rval[1].pop('classifier')
fn_iters = fn_rval[1].pop('iterations')
if fn_loss < self._best_loss:
self._best_preprocs = fn_preprocs
self._best_classif = fn_classif
self._best_loss = fn_loss
self._best_iters = fn_iters
return fn_rval[1]
while True:
new_increment = yield self.trials
if new_increment is not None:
increment = new_increment
hyperopt.fmin(fn_with_timeout,
space=self.space,
algo=self.algo,
trials=self.trials,
max_evals=len(self.trials.trials) + increment,
rstate=self.rstate,
# -- let exceptions crash the program,
# so we notice them.
catch_eval_exceptions=False,
return_argmin=False, # -- in case no success so far
)
def retrain_best_model_on_full_data(self, X, y, weights=None):
for pp_algo in self._best_preprocs:
pp_algo.fit(X)
X = pp_algo.transform(X * 1) # -- * 1 avoids read-only copy bug
if hasattr(self._best_classif, 'partial_fit'):
rng = np.random.RandomState(6665)
train_idxs = rng.permutation(X.shape[0])
for i in xrange(int(self._best_iters * retrain_fraction)):
rng.shuffle(train_idxs)
self._best_classif.partial_fit(X[train_idxs], y[train_idxs],
classes=np.unique(y))
else:
self._best_classif.fit(X,y)
def fit(self, X, y, weights=None):
"""
Search the space of classifiers and preprocessing steps for a good
predictive model of y <- X. Store the best model for predictions.
"""
filename = self.fit_increment_dump_filename
fit_iter = self.fit_iter(X, y,
weights=weights,
increment=self.fit_increment)
fit_iter.next()
while len(self.trials.trials) < self.max_evals:
increment = min(self.fit_increment,
self.max_evals - len(self.trials.trials))
fit_iter.send(increment)
if filename is not None:
with open(filename, 'wb') as dump_file:
self.info('---> dumping trials to', filename)
cPickle.dump(self.trials, dump_file)
self.retrain_best_model_on_full_data(X, y, weights)
def predict(self, X):
"""
Use the best model found by previous fit() to make a prediction.
"""
# -- copy because otherwise np.utils.check_arrays sometimes does not
# produce a read-write view from read-only memory
if scipy.sparse.issparse(X):
X = scipy.sparse.csr_matrix(X)
else:
X = np.array(X)
for pp in self._best_preprocs:
self.info("Transforming X of shape", X.shape)
X = pp.transform(X)
self.info("Predicting X of shape", X.shape)
return self._best_classif.predict(X)
def score( self, X, y ):
"""
Return the accuracy of the classifier on a given set of data
"""
# -- copy because otherwise np.utils.check_arrays sometimes does not
# produce a read-write view from read-only memory
X = np.array(X)
for pp in self._best_preprocs:
self.info("Transforming X of shape", X.shape)
X = pp.transform(X)
self.info("Classifying X of shape", X.shape)
return self._best_classif.score(X, y)
def best_model( self ):
"""
Returns the best model found by the previous fit()
"""
return {'classifier': self._best_classif,
'preprocs': self._best_preprocs}