Skip to content

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

Description

@DaleMYChen

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)
    

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions