keras-team / keras-tuner

A Hyperparameter Tuning Library for Keras
https://keras.io/keras_tuner/
Apache License 2.0
2.86k stars 396 forks source link

TypeError using BayesianOptimization #671

Open codecypher opened 2 years ago

codecypher commented 2 years ago

Describe the bug

My code works fine with RandomSearch but generates an error using kt.BayesianOptimization, so not sure about the source of the error. The code was working fine in previous version of KerasTuner but the API changed so much in recent versions that I had to rewrite the code.

tuner = kt.BayesianOptimization(
    hypermodel=build_model,
    hyperparameters=hp,
    objective=params.objective,
    max_trials=params.max_trials,
    executions_per_trial=5,
    # optimizer=keras.optimizers.Adam(params.lr),
    # loss=params.loss,
    # metrics=["sparse_categorical_crossentropy",],
    # tune_new_entries=False,  # prevent unlisted parameters from being tuned
    overwrite=True,
    directory=path_ckpt,
    project_name=project_name,
)

Best val_loss So Far: 3021.8214111328125 Total elapsed time: 00h 20m 26s Traceback (most recent call last): File "./stock_keras_tuner.py", line 570, in main(params, ticker_list) File "./stock_keras_tuner.py", line 480, in main results_df = tune(params) File "./stock_keras_tuner.py", line 388, in tune tuner.search( File "/Users/name/stock/lib/python3.8/site-packages/keras_tuner/engine/base_tuner.py", line 169, in search trial = self.oracle.create_trial(self.tuner_id) File "/Users/name/stock/lib/python3.8/site-packages/keras_tuner/engine/oracle.py", line 189, in create_trial response = self.populate_space(trial_id) File "/Users/name/stock/lib/python3.8/site-packages/keras_tuner/tuners/bayesian.py", line 232, in populate_space if result.fun[0] < optimal_val: TypeError: 'float' object is not subscriptable

Expected behavior

No errors.

Would you like to help us fix it? Yes.

brydon commented 2 years ago

What version of keras-tuner are you using? Because this sounds familiar to an issue fixed in #662 that should have been resolved with version 1.1.1

codecypher commented 2 years ago

Keras Tuner v 1.1.0

I updated to v 1.1.1 and it seems to be running. Will let you know if it completes.

Thanks, really didn't want to switch back to using Optuna (seems much slower).

cjc77 commented 2 years ago

Any idea when the version of Keras Tuner will be updated on Conda? Currently, v 1.1.0 (the version with this bug) is the latest version available.

codecypher commented 2 years ago

I have been using the setting pip_interop_enabled: true in .condarc and I switched to mambaforge since it performs much better than anaconda. Then you should be install keras-tuner using pip and things should stay in sync with conda. It has been working for me anyway.