hyperopt / hyperopt-sklearn

Hyper-parameter optimization for sklearn
hyperopt.github.io/hyperopt-sklearn
Other
1.57k stars 270 forks source link

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

Closed john-zeng112 closed 3 months ago

john-zeng112 commented 2 years ago

I encountered a AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers' at the hyperopt/fmin.py in run(self, N, block_until_done). My numpy and sklearn version are 1.19.2 and 1.0.1, respectively.

taylor-schneider commented 2 years ago

@john-zeng112 I am seeing the same issue on 1.19.5 and 1.0.1. My stack trace is as follows:

best_hyperparameters = hyperopt.fmin(
  fn = foobar,
  space = space,
  algo = hyperopt.tpe.suggest,
  max_evals = 200,
  trials = spark_trials,
  loss_threshold = 0.05,
  rstate = numpy.random.RandomState(42))

==================================================

Total Trials: 10: 10 succeeded, 0 failed, 0 cancelled.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-71c168d86476> in <module>
      6   trials = spark_trials,
      7   loss_threshold = 0.05,
----> 8   rstate= numpy.random.RandomState(42))

c:\program files\python36\lib\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 

c:\program files\python36\lib\site-packages\hyperopt\spark.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)
    259         except BaseException as e:
    260             logger.debug("fmin thread exits with an exception raised.")
--> 261             raise e
    262         else:
    263             logger.debug("fmin thread exits normally.")

c:\program files\python36\lib\site-packages\hyperopt\spark.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)
    255                 show_progressbar=show_progressbar,
    256                 early_stop_fn=early_stop_fn,
--> 257                 trials_save_file="",  # not supported
    258             )
    259         except BaseException as e:

c:\program files\python36\lib\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:

c:\program files\python36\lib\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

c:\program files\python36\lib\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'

I had a look through the hyperopt search code. The SparkTrials behaves differently than the normal base.Trials object. The SparkTrials object is expecting an instance of numpy.random.default_rng(SEED)). We can see an example of this in hyperopt/mix.py on line 28 - 34.

I adjusted my code and saw success with:

best_hyperparameters = hyperopt.fmin(
  fn = foobar,
  space = space,
  algo = hyperopt.tpe.suggest,
  max_evals = 200,
  trials = spark_trials,
  loss_threshold = 0.05,
  rstate = numpy.random.default_rng(42))

I guess we can either mark this as solved or ask @jaberg if the difference between the interfaces of the Trials objects was intentional. I would argue that the same type of seed should be used for ALL classes of Trials. If there is a technical limitation then I'd suggest documenting SparkTrials as the black sheep and providing an example.

aspfohl commented 2 years ago

Seeing the same issue with hpsklearn=0.1.0, numpy=1.21.4, hyperopt=0.2.7, Python 3.9.6. I'm running something very simple, straight from the main readme:

from hpsklearn import HyperoptEstimator, svc
estim = HyperoptEstimator(classifier=svc('mySVC'))
estim.fit(X_train, Y_train)

Stack trace:

AttributeError                            Traceback (most recent call last)
      1 from hpsklearn import HyperoptEstimator, svc
      2 estim = HyperoptEstimator(classifier=svc('mySVC'))
----> 3 estim.fit(X_train, Y_train)

~/.venv/lib/python3.9/site-packages/hpsklearn/estimator.py in fit(self, X, y, EX_list, valid_size, n_folds, cv_shuffle, warm_start, random_state, weights)
    744             increment = min(self.fit_increment,
    745                             adjusted_max_evals - len(self.trials.trials))
--> 746             fit_iter.send(increment)
    747             if filename is not None:
    748                 with open(filename, 'wb') as dump_file:

~/.venv/lib/python3.9/site-packages/hpsklearn/estimator.py in fit_iter(self, X, y, EX_list, valid_size, n_folds, cv_shuffle, warm_start, random_state, weights, increment)
    646             #       latest hyperopt.fmin() on master does not match PyPI
    647             if 'rstate' in inspect.getargspec(hyperopt.fmin).args:
--> 648                 hyperopt.fmin(fn_with_timeout,
    649                               space=self.space,
    650                               algo=self.algo,

~/.venv/lib/python3.9/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)
    538 
    539     if allow_trials_fmin and hasattr(trials, "fmin"):
--> 540         return trials.fmin(
    541             fn,
    542             space,

~/.venv/lib/python3.9/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)
    669         from .fmin import fmin
    670 
--> 671         return fmin(
    672             fn,
    673             space,

~/.venv/lib/python3.9/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:

~/.venv/lib/python3.9/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

~/.venv/lib/python3.9/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'

Having install requirements would make debugging this a lot easier (https://github.com/hyperopt/hyperopt-sklearn/issues/180)

B0Gec commented 2 years ago

For me this issue was solved by changing hyperopt to version 0.2.5 (pip install hyperopt==0.2.5) as suggested in the issue https://github.com/hyperopt/hyperopt/issues/829

My (linux) setup: Python 3.9.2 hpsklearn 0.0.3 hyperopt 0.2.5 numpy 1.22.0

For me it worked in both ways of installing: with pip install git+url (newest version) and pip install hpsklearn (2017 version)

rottentomato13 commented 1 year ago

For me this issue was solved by changing hyperopt to version 0.2.5 (pip install hyperopt==0.2.5) as suggested in the issue hyperopt/hyperopt#829

My (linux) setup: Python 3.9.2 hpsklearn 0.0.3 hyperopt 0.2.5 numpy 1.22.0

For me it worked in both ways of installing: with pip install git+url (newest version) and pip install hpsklearn (2017 version)

when I pip install git+url it said Invalid requirement: 'git+url' donno why

Preranapagar commented 5 months ago

Facing same issue for hyperopt (0.2.7)

z4668640 commented 3 months ago

The problem still exists

mandjevant commented 3 months ago

This is an issue with hyperopt. Not hyperopt-sklearn.

Downgrading to hyperopt v0.2.5 solves it for most.

https://github.com/hyperopt/hyperopt/issues/829

MrVtR commented 3 months ago

This is an issue with hyperopt. Not hyperopt-sklearn.

Downgrading to hyperopt v0.2.5 solves it for most.

hyperopt/hyperopt#829

This didn't work with sparse data, instead of: AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers'

Now I have: ValueError: zero-dimensional arrays cannot be concatenated

This error is mentioned in #105 as completed, but I'm with this same error on hyperopt 0.2.5