question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)
    

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

6reactions
bessembhiricommented, Jun 27, 2022

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)

4reactions
xxs980commented, Dec 15, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

'numpy.random.mtrand.RandomState' object has no attribute ...
RandomState(SEED), I got the error 'numpy.random.mtrand.RandomState' object has no attribute 'integers' My fmin call looks like this: ...
Read more >
AttributeError: 'int' object has no attribute 'randint' in Hyperopt
Here, rstate parameter of fmin() function needs random number generator which can be provided by np.random.RandomState(seed) .
Read more >
numpy.random.RandomState.random_integers
Return random integers of type np.int_ from the “discrete uniform” distribution in the closed interval [low, high]. If high is None (the default),...
Read more >
Errors in notebooks of Scalable Machine Learning with ...
AttributeError : 'numpy.random.mtrand.RandomState' object has no attribute 'integers'. Detailed Error: AttributeError Traceback (most recent call last) ...
Read more >
numpy.random.mtrand.RandomState
Random seed initializing the pseudo-random number generator. Can be an integer, an array (or other sequence) of integers of any length, or None ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found