ray-project / tune-sklearn

A drop-in replacement for Scikit-Learn’s GridSearchCV / RandomizedSearchCV -- but with cutting edge hyperparameter tuning techniques.
https://docs.ray.io/en/master/tune/api_docs/sklearn.html
Apache License 2.0
465 stars 52 forks source link

TuneError. Any idea? #48

Closed echavezh closed 4 years ago

echavezh commented 4 years ago

I'm getting an error as soon as I try calling the fit method:

TuneError                                 Traceback (most recent call last)
<ipython-input-141-9038f3bc751b> in <module>
     37     kernel_initializer=kernel_initializer)
     38 grid = TuneGridSearchCV(estimator=model, param_grid=param_grid)
---> 39 grid_result = grid.fit(X, y)
     40 print(grid_result.best_params_)
     41 print(grid_result.cv_results_)

~\Anaconda3\envs\PythonGPU\lib\site-packages\tune_sklearn\tune_basesearch.py in fit(self, X, y, groups, **fit_params)
    366                 ray.init(ignore_reinit_error=True, configure_logging=False)
    367 
--> 368             result = self._fit(X, y, groups, **fit_params)
    369 
    370             if not ray_init and ray.is_initialized():

~\Anaconda3\envs\PythonGPU\lib\site-packages\tune_sklearn\tune_basesearch.py in _fit(self, X, y, groups, **fit_params)
    320 
    321         self._fill_config_hyperparam(config)
--> 322         analysis = self._tune_run(config, resources_per_trial)
    323 
    324         self.cv_results_ = self._format_results(self.n_splits, analysis)

~\Anaconda3\envs\PythonGPU\lib\site-packages\tune_sklearn\tune_gridsearch.py in _tune_run(self, config, resources_per_trial)
    203                 config=config,
    204                 checkpoint_at_end=True,
--> 205                 resources_per_trial=resources_per_trial)
    206 
    207         return analysis

~\Anaconda3\envs\PythonGPU\lib\site-packages\ray\tune\tune.py in run(run_or_experiment, name, stop, config, resources_per_trial, num_samples, local_dir, upload_dir, trial_name_creator, loggers, sync_to_cloud, sync_to_driver, checkpoint_freq, checkpoint_at_end, sync_on_checkpoint, keep_checkpoints_num, checkpoint_score_attr, global_checkpoint_period, export_formats, max_failures, fail_fast, restore, search_alg, scheduler, with_server, server_port, verbose, progress_reporter, resume, queue_trials, reuse_actors, trial_executor, raise_on_failed_trial, return_trials, ray_auto_init)
    347     if incomplete_trials:
    348         if raise_on_failed_trial:
--> 349             raise TuneError("Trials did not complete", incomplete_trials)
    350         else:
    351             logger.error("Trials did not complete: %s", incomplete_trials)

TuneError: ('Trials did not complete', [_Trainable_edcaa_00000, _Trainable_edcaa_00001, _Trainable_edcaa_00002, _Trainable_edcaa_00003, _Trainable_edcaa_00004, _Trainable_edcaa_00005, _Trainable_edcaa_00006, _Trainable_edcaa_00007])
inventormc commented 4 years ago

Can you post the code you ran as well?

echavezh commented 4 years ago

Sure, I basically just copied what was posted on Reddit's post. X and y is a simple numpy array:

"""
An example training a Keras model, performing
grid search using TuneGridSearchCV.
"""

from keras.layers import Dense, Activation, Dropout
from keras.models import Sequential
from keras.utils import np_utils
from keras.wrappers.scikit_learn import KerasClassifier
from tune_sklearn import TuneGridSearchCV

def create_model(optimizer='adam',dropout=0.1):
    model = keras.Sequential()
    model.add(keras.layers.Dense(20,activation='relu'))
    model.add(keras.layers.Dropout(dropout))
    model.add(keras.layers.Dense(1,activation='sigmoid'))
    model.compile(loss='binary_crossentropy',optimizer=optimizer,
    metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=create_model)
optimizers = ["rmsprop", "adam"]
kernel_initializer = ["glorot_uniform", "normal"]
epochs = [5, 10]
param_grid = dict(
    optimizer=optimizers,
    nb_epoch=epochs,
    kernel_initializer=kernel_initializer)
grid = TuneGridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(X, y)
print(grid_result.best_params_)
print(grid_result.cv_results_)
anthonyhsyu commented 4 years ago

Hi @echavezh, thanks for posting the issue! There are a couple things that could be causing problems, and fixing these should let it fit properly!

  1. Your create_model function is missing a parameter for kernel_initializer which you provided in param_grid, so when it's fitting it can't find a way to pass in the kernel_initializer parameter you wanted to tune. It looks like you wanted to parameterize dropout, so you can also add dropout in your param_grid if you want to test more hyperparameter configurations.
  2. You've imported individual components from keras but not the whole keras itself (via import keras), so you can just say Sequential(), Dense(20, ...) and so on :)

Hope this helps, and let us know if you have more questions!

anthonyhsyu commented 4 years ago

For example, you could do:

def create_model(optimizer='adam',dropout=0.1):
    model = Sequential()
    model.add(Dense(20,activation='relu'))
    model.add(Dropout(dropout))
    model.add(Dense(1,activation='sigmoid'))
    model.compile(loss='binary_crossentropy',optimizer=optimizer,
    metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=create_model)
optimizers = ["rmsprop", "adam"]
dropout = [0.1, 0.2]
epochs = [5, 10]
param_grid = dict(
    optimizer=optimizers,
    nb_epoch=epochs,
    dropout=dropout)
grid = TuneGridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(X_train, y_train)
inventormc commented 4 years ago

Looks like this is fixed now. Going to close the issue for now