Open DagonArises opened 2 years ago
it maybe the hyperopt version problem,please choose other version of hyperopt pip install hyperopt==0.2.5
it maybe the hyperopt version problem,please choose other version of hyperopt pip install hyperopt==0.2.5
Thank you! It's starting to train now. However there is an additional KeyError issue with 'val_acc'. I will open another issue, please have a look at that :)
please use 'val_accuracy' replace this 'val_acc' such as this code: history['val_acc'] ---> history['val_accuracy']
The further issue with 'val_accuracy' is moved here.
When I coding on Kaggle, I got a same problem, and also solved by !pip install hyperopt==0.2.5
Thank you!
I have the same problem, even after installing version 0.2.5.
My code (jupyter lab):
!pip install hyperas
!pip install hyperopt==0.2.5
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform
import tensorflow as tf
from keras import backend
import numpy as np
from tensorflow.keras.layers import Dense, Input, GlobalMaxPooling1D, LSTM, GRU, Conv1D, MaxPooling1D
def rmse(y_true, y_pred):
return backend.sqrt(backend.mean(backend.square(y_pred - y_true)))
def data():
return np.load('Xtrain.npy'), np.load('Ytrain.npy'), np.load('Xval.npy'), np.load('Yval.npy')
def model(X_train, Y_train, X_val, Y_val):
model = Sequential()
model.add(Dense({{choice([16,32,64,128,256,512,1024])}}, input_shape=(784,)))
model.add(Activation({{choice(['relu', 'sigmoid'] )}}))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([16,32,64,128,256,512,1024])}}))
model.add(Activation({{choice(['relu', 'sigmoid'] )}}))
model.add(Dropout({{uniform(0, 1)}}))
if conditional({{choice(['two', 'three'])}}) == 'three':
model.add(Dense({{choice([16,32,64,128,256,512,1024])}}))
model.add(Activation({{choice(['relu', 'sigmoid'] )}}))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense(1))
adam = keras.optimizers.Adam(lr={{choice([10**-3, 10**-2, 10**-1])}})
rmsprop = keras.optimizers.RMSprop(lr={{choice([10**-3, 10**-2, 10**-1])}})
sgd = keras.optimizers.SGD(lr={{choice([10**-3, 10**-2, 10**-1])}})
choiceval = {{choice(['adam', 'sgd', 'rmsprop'])}}
if choiceval == 'adam':
optim = adam
elif choiceval == 'rmsprop':
optim = rmsprop
else:
optim = sgd
model.compile(loss=rmse, metrics=['val_loss'],optimizer=optim)
model.fit(X_train, Y_train,
batch_size={{choice([128,256,512])}},
nb_epoch=20,
verbose=2,
validation_data=(X_val, Y_val))
score, loss = model.evaluate(X_val, Y_val, verbose=0)
print('Test loss:', loss)
return {'loss': loss, 'status': STATUS_OK, 'model': model}
X_train, Y_train, X_val, Y_val = data()
best_run, best_model = optim.minimize(model=model,
data=data,
algo=tpe.suggest,
max_evals=30,
trials=Trials(),
notebook_name='Untitled1')
My error:
AttributeError Traceback (most recent call last)
Input In [8], in <module>
1 X_train, Y_train, X_val, Y_val = data()
----> 2 best_run, best_model = optim.minimize(model=model,
3 data=data,
4 algo=tpe.suggest,
5 max_evals=30,
6 trials=Trials(),
7 notebook_name='Untitled1')
File C:\Python310\lib\site-packages\hyperas\optim.py:59, in minimize(model, data, algo, max_evals, trials, functions, rseed, notebook_name, verbose, eval_space, return_space, keep_temp)
20 def minimize(model,
21 data,
22 algo,
(...)
30 return_space=False,
31 keep_temp=False):
32 """
33 Minimize a keras model for given data and implicit hyperparameters.
34
(...)
57 If `return_space` is True: The pair of best result and corresponding keras model, and the hyperopt search space
58 """
---> 59 best_run, space = base_minimizer(model=model,
60 data=data,
61 functions=functions,
62 algo=algo,
63 max_evals=max_evals,
64 trials=trials,
65 rseed=rseed,
66 full_model_string=None,
67 notebook_name=notebook_name,
68 verbose=verbose,
69 keep_temp=keep_temp)
71 best_model = None
72 for trial in trials:
File C:\Python310\lib\site-packages\hyperas\optim.py:133, in base_minimizer(model, data, functions, algo, max_evals, trials, rseed, full_model_string, notebook_name, verbose, stack, keep_temp)
129 except TypeError:
130 pass
132 return (
--> 133 fmin(keras_fmin_fnct,
134 space=get_space(),
135 algo=algo,
136 max_evals=max_evals,
137 trials=trials,
138 rstate=np.random.RandomState(rseed),
139 return_argmin=True),
140 get_space()
141 )
File C:\Python310\lib\site-packages\hyperopt\fmin.py:540, in fmin(fn, space, algo, max_evals, timeout, loss_threshold, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar, early_stop_fn, trials_save_file)
537 fn = __objective_fmin_wrapper(fn)
539 if allow_trials_fmin and hasattr(trials, "fmin"):
--> 540 return trials.fmin(
541 fn,
542 space,
543 algo=algo,
544 max_evals=max_evals,
545 timeout=timeout,
546 loss_threshold=loss_threshold,
547 max_queue_len=max_queue_len,
548 rstate=rstate,
549 pass_expr_memo_ctrl=pass_expr_memo_ctrl,
550 verbose=verbose,
551 catch_eval_exceptions=catch_eval_exceptions,
552 return_argmin=return_argmin,
553 show_progressbar=show_progressbar,
554 early_stop_fn=early_stop_fn,
555 trials_save_file=trials_save_file,
556 )
558 if trials is None:
559 if os.path.exists(trials_save_file):
File C:\Python310\lib\site-packages\hyperopt\base.py:671, in Trials.fmin(self, fn, space, algo, max_evals, timeout, loss_threshold, max_queue_len, rstate, verbose, pass_expr_memo_ctrl, catch_eval_exceptions, return_argmin, show_progressbar, early_stop_fn, trials_save_file)
666 # -- Stop-gap implementation!
667 # fmin should have been a Trials method in the first place
668 # but for now it's still sitting in another file.
669 from .fmin import fmin
--> 671 return fmin(
672 fn,
673 space,
674 algo=algo,
675 max_evals=max_evals,
676 timeout=timeout,
677 loss_threshold=loss_threshold,
678 trials=self,
679 rstate=rstate,
680 verbose=verbose,
681 max_queue_len=max_queue_len,
682 allow_trials_fmin=False, # -- prevent recursion
683 pass_expr_memo_ctrl=pass_expr_memo_ctrl,
684 catch_eval_exceptions=catch_eval_exceptions,
685 return_argmin=return_argmin,
686 show_progressbar=show_progressbar,
687 early_stop_fn=early_stop_fn,
688 trials_save_file=trials_save_file,
689 )
File C:\Python310\lib\site-packages\hyperopt\fmin.py:586, in fmin(fn, space, algo, max_evals, timeout, loss_threshold, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar, early_stop_fn, trials_save_file)
583 rval.catch_eval_exceptions = catch_eval_exceptions
585 # next line is where the fmin is actually executed
--> 586 rval.exhaust()
588 if return_argmin:
589 if len(trials.trials) == 0:
File C:\Python310\lib\site-packages\hyperopt\fmin.py:364, in FMinIter.exhaust(self)
362 def exhaust(self):
363 n_done = len(self.trials)
--> 364 self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
365 self.trials.refresh()
366 return self
File C:\Python310\lib\site-packages\hyperopt\fmin.py:279, in FMinIter.run(self, N, block_until_done)
273 self.trials.refresh()
274 # Based on existing trials and the domain, use `algo` to probe in
275 # new hp points. Save the results of those inspections into
276 # `new_trials`. This is the core of `run`, all the rest is just
277 # processes orchestration
278 new_trials = algo(
--> 279 new_ids, self.domain, trials, self.rstate.integers(2 ** 31 - 1)
280 )
281 assert len(new_ids) >= len(new_trials)
283 if len(new_trials):
AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers'
I believe this will be corrected once this issue is resolved.
For me with hyperopt==0.2.7 I have changed the row 139 of optim.py in hyperas from: rstate=np.random.RandomState(rseed) to rstate=np.random.default_rng(rseed)
@bessembhiri Yes, this solution work for me also Thanks :)
@bessembhiri works for me too.
@bessembhiri works for me thank you!!!!
Should be fixed in #290
When I coding on Kaggle, I got a same problem, and also solved by
!pip install hyperopt==0.2.5
Thank you!
same solution worked for me
The error:
My code: