Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers' #284

Open
DagonArises opened this issue Dec 14, 2021 · 13 comments

Comments

@DagonArises
Copy link

DagonArises commented Dec 14, 2021

The error:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-54024aaf20f0> in <module>()
      3                                         algo= tpe.suggest, max_evals= 5,
      4                                         trials= Trials(),
----> 5                                         notebook_name='Deep learning GridSearch')
      6     xtr, ytr, xte, yte= data()
      7 

~/anaconda3/lib/python3.6/site-packages/hyperas/optim.py in minimize(model, data, algo, max_evals, trials, functions, rseed, notebook_name, verbose, eval_space, return_space, keep_temp)
     67                                      notebook_name=notebook_name,
     68                                      verbose=verbose,
---> 69                                      keep_temp=keep_temp)
     70 
     71     best_model = None

~/anaconda3/lib/python3.6/site-packages/hyperas/optim.py in base_minimizer(model, data, functions, algo, max_evals, trials, rseed, full_model_string, notebook_name, verbose, stack, keep_temp)
    137              trials=trials,
    138              rstate=np.random.RandomState(rseed),
--> 139              return_argmin=True),
    140         get_space()
    141     )

~/anaconda3/lib/python3.6/site-packages/hyperopt/fmin.py 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)
    553             show_progressbar=show_progressbar,
    554             early_stop_fn=early_stop_fn,
--> 555             trials_save_file=trials_save_file,
    556         )
    557 

~/anaconda3/lib/python3.6/site-packages/hyperopt/base.py in 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)
    686             show_progressbar=show_progressbar,
    687             early_stop_fn=early_stop_fn,
--> 688             trials_save_file=trials_save_file,
    689         )
    690 

~/anaconda3/lib/python3.6/site-packages/hyperopt/fmin.py 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)
    584 
    585     # next line is where the fmin is actually executed
--> 586     rval.exhaust()
    587 
    588     if return_argmin:

~/anaconda3/lib/python3.6/site-packages/hyperopt/fmin.py in 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

~/anaconda3/lib/python3.6/site-packages/hyperopt/fmin.py in run(self, N, block_until_done)
    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)

AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers'

My code:

def data():
    (xtr, ytr), (xte, yte)= mnist.load_data()
    xtr= xtr.reshape(60000, 784); xtr= xtr.astype('float32')
    xte= xte.reshape(10000, 784); xte= xte.astype('float32')
    xtr/= 255; xte/= 255
    nb_classes= 10
    ytr= np_utils.to_categorical(ytr, nb_classes)
    yte= np_utils.to_categorical(yte, nb_classes)
    return xtr, ytr, xte, yte

def create_model(xtr, ytr, xte, yte):
    # returns a dictionary of loss, status and model
    model= Sequential()
    # 1st layer:
    model.add(Dense(512, input_shape= (784,), activation= 'relu'))  # equivalently input_dim= 784    
    model.add(Dropout({{uniform(0,1)}}))
    
    # 2nd layer:
    # hyperparameter = {{choice([...])}}
    model.add(Dense(units= {{choice([256,5125,1024])}}, 
                    activation= {{choice(['relu', 'sigmoid'])}}))
    model.add(Dropout({{uniform(0,1)}}))
    
    # 3rd layer:
    if {{choice(['three','four'])}} == 'four':
        model.add(Dense(100))
        # choice between 2 different types of Dense(100) layers:
        model.add({{choice([Dropout(0.5), Activation('linear')])}})
        model.add(Activation('relu'))
    
    # 4th layer:
    model.add(Dense(10, activation= 'softmax'))
    
    model.compile(loss='categorical_crossentropy', 
                  optimizer= {{choice(['rmsprop', 'adam', 'SGD'])}},
                 metrics=['accuracy'])
    
    # Model fit:
    result= model.fit(xtr, ytr, batch_size= {{choice([64, 128])}},
                     epochs= 2, verbose= 2, validation_split= 0.1)
    validation_acc= np.amax(result.history['val_acc'])
    print('Best validation accuracy of epoch:', validation_acc)
    return {'Loss:', -validation_acc, 'status:', STATUS_OK, 'model:',model}

if __name__ == '__main__':
    best_run, best_model= optim.minimize(model= create_model, data= data,
                                        algo= tpe.suggest, max_evals= 5,
                                        trials= Trials(),
                                        notebook_name='Deep learning GridSearch')
    xtr, ytr, xte, yte= data()
    
    print('Evaluation of best performing model:')
    print(best_model.evaluate(xte, yte))
    
    print('Optimal hyperparameter choice:')
    print(best_run)
    

@xxs980
Copy link

xxs980 commented Dec 15, 2021

it maybe the hyperopt version problem,please choose other version of hyperopt
pip install hyperopt==0.2.5

@DagonArises
Copy link
Author

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 :)

@xxs980
Copy link

xxs980 commented Dec 15, 2021

please use 'val_accuracy' replace this 'val_acc'
such as this code:
history['val_acc'] ---> history['val_accuracy']

@DagonArises
Copy link
Author

DagonArises commented Dec 17, 2021

The further issue with 'val_accuracy' is moved here.

@JulianLee310514065
Copy link

When I coding on Kaggle, I got a same problem, and also solved by !pip install hyperopt==0.2.5
Thank you!

@zora-no
Copy link

zora-no commented May 4, 2022

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'

@SatoshiSakamori
Copy link

I believe this will be corrected once this issue is resolved.

@bessembhiri
Copy link

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)

@VikasNatuskar
Copy link

@bessembhiri Yes, this solution work for me also
Thanks :)

@nbrochec
Copy link

@bessembhiri works for me too.

@rottentomato13
Copy link

@bessembhiri works for me thank you!!!!

@JonnoFTW
Copy link
Collaborator

JonnoFTW commented Jan 5, 2023

Should be fixed in #290

@yustiks
Copy link

yustiks commented May 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests