hyperopt / hyperopt-sklearn

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

TypeError: __init__() got an unexpected keyword argument 'n_iter' #133

Open rbankosegger opened 5 years ago

rbankosegger commented 5 years ago

Hi! I tried to run the very simple example from the Usage section:

from hpsklearn import HyperoptEstimator, any_classifier
from sklearn.datasets import load_iris
from hyperopt import tpe
import numpy as np

# Download the data and split into training and test sets

iris = load_iris()

X = iris.data
y = iris.target

test_size = int(0.2 * len(y))
np.random.seed(13)
indices = np.random.permutation(len(X))
X_train = X[indices[:-test_size]]
y_train = y[indices[:-test_size]]
X_test = X[indices[-test_size:]]
y_test = y[indices[-test_size:]]

# Instantiate a HyperoptEstimator with the search space and number of evaluations

estim = HyperoptEstimator(classifier=any_classifier('my_clf'),
                          preprocessing=any_preprocessing('my_pre'),
                          algo=tpe.suggest,
                          max_evals=100,
                          trial_timeout=120)

# Search the hyperparameter space based on the data

estim.fit(X_train, y_train)

# Show the results

print(estim.score(X_test, y_test))
# 1.0

print( estim.best_model() )
# {'learner': ExtraTreesClassifier(bootstrap=False, class_weight=None, criterion='gini',
#           max_depth=3, max_features='log2', max_leaf_nodes=None,
#           min_impurity_decrease=0.0, min_impurity_split=None,
#           min_samples_leaf=1, min_samples_split=2,
#           min_weight_fraction_leaf=0.0, n_estimators=13, n_jobs=1,
#           oob_score=False, random_state=1, verbose=False,
#           warm_start=False), 'preprocs': (), 'ex_preprocs': ()}

However, this does not work for me. I get the following output:

100%|██████████| 1/1 [00:00<00:00,  6.31it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  2.05it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  1.98it/s, best loss: 0.04166666666666663]
  0%|          | 0/1 [00:00<?, ?it/s, best loss: ?]
/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:594: RuntimeWarning: overflow encountered in exp
  (estimator_weight < 0)))

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:171: RuntimeWarning: invalid value encountered in true_divide
  sample_weight /= sample_weight_sum

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

/usr/local/lib/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:593: RuntimeWarning: invalid value encountered in greater
  ((sample_weight > 0) |

100%|██████████| 1/1 [00:00<00:00,  3.34it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  5.78it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  5.34it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  5.39it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  9.02it/s, best loss: 0.04166666666666663]
100%|██████████| 1/1 [00:00<00:00,  9.96it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  1.70it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  3.16it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  4.83it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  3.98it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  8.85it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  1.30it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  1.74it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  4.86it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  9.18it/s, best loss: 0.0]
100%|██████████| 1/1 [00:00<00:00,  7.38it/s, best loss: 0.0]
  0%|          | 0/1 [00:00<?, ?it/s, best loss: ?]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-ea577ada0a61> in <module>
     29 # Search the hyperparameter space based on the data
     30 
---> 31 estim.fit(X_train, y_train)
     32 
     33 # Show the results

/usr/local/lib/python3.7/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:

/usr/local/lib/python3.7/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)
    655                               #    so we notice them.
    656                               catch_eval_exceptions=False,
--> 657                               return_argmin=False, # -- in case no success so far
    658                              )
    659             else:

~/Downloads/hyperopt/hyperopt/hyperopt/fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar)
    386             catch_eval_exceptions=catch_eval_exceptions,
    387             return_argmin=return_argmin,
--> 388             show_progressbar=show_progressbar,
    389         )
    390 

~/Downloads/hyperopt/hyperopt/hyperopt/base.py in fmin(self, fn, space, algo, max_evals, rstate, verbose, pass_expr_memo_ctrl, catch_eval_exceptions, return_argmin, show_progressbar)
    637             catch_eval_exceptions=catch_eval_exceptions,
    638             return_argmin=return_argmin,
--> 639             show_progressbar=show_progressbar)
    640 
    641 

~/Downloads/hyperopt/hyperopt/hyperopt/fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar)
    405                     show_progressbar=show_progressbar)
    406     rval.catch_eval_exceptions = catch_eval_exceptions
--> 407     rval.exhaust()
    408     if return_argmin:
    409         return trials.argmin

~/Downloads/hyperopt/hyperopt/hyperopt/fmin.py in exhaust(self)
    260     def exhaust(self):
    261         n_done = len(self.trials)
--> 262         self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
    263         self.trials.refresh()
    264         return self

~/Downloads/hyperopt/hyperopt/hyperopt/fmin.py in run(self, N, block_until_done)
    225                     else:
    226                         # -- loop over trials and do the jobs directly
--> 227                         self.serial_evaluate()
    228 
    229                     try:

~/Downloads/hyperopt/hyperopt/hyperopt/fmin.py in serial_evaluate(self, N)
    139                 ctrl = base.Ctrl(self.trials, current_trial=trial)
    140                 try:
--> 141                     result = self.domain.evaluate(spec, ctrl)
    142                 except Exception as e:
    143                     logger.info('job exception: %s' % str(e))

~/Downloads/hyperopt/hyperopt/hyperopt/base.py in evaluate(self, config, ctrl, attach_attachments)
    841                 self.expr,
    842                 memo=memo,
--> 843                 print_node_on_error=self.rec_eval_print_node_on_error)
    844             rval = self.fn(pyll_rval)
    845 

~/Downloads/hyperopt/hyperopt/hyperopt/pyll/base.py in rec_eval(expr, deepcopy_inputs, memo, max_program_len, memo_gc, print_trace, print_node_on_error)
    911 
    912             try:
--> 913                 rval = scope._impls[node.name](*args, **kwargs)
    914 
    915             except Exception as e:

/usr/local/lib/python3.7/site-packages/hpsklearn/components.py in sklearn_SGDClassifier(*args, **kwargs)
     89 @scope.define
     90 def sklearn_SGDClassifier(*args, **kwargs):
---> 91     return sklearn.linear_model.SGDClassifier(*args, **kwargs)
     92 
     93 @scope.define

TypeError: __init__() got an unexpected keyword argument 'n_iter'

Here is some info about my system:

macOS Mojave (v10.14.3)
MacBook Pro (Retina, 15-inch, Mid 2014)
Processor -- 2,5 GHz Intel Core i7
Memory -- 16 GB 1600 MHz DDR3
Graphics -- Intel Iris Pro 1536 MB

My hyperopt version is 0.2, installed via pip.

When running above import statements, I also get the following error:

WARN: OMP_NUM_THREADS=None =>
... If you are using openblas if you are using openblas set OMP_NUM_THREADS=1 or risk subprocess calls hanging indefinitely

Apart from that, I have a three python jobs running (Scikit-learn' CV-Gridsearch) and a bunch of other jupyter notebooks open.

Thanks for the help!

bjkomer commented 5 years ago

It looks like you are using an older version of hyperopt-sklearn that is only compatible with an older version of scikit-learn. This was fixed in #87 but looks like it isn't in the current pip version.

Your best bet would be to install the version on master directly. You can either clone it and install, or install directly with a command like this:

pip install git+https://github.com/hyperopt/hyperopt-sklearn.git
oszust002 commented 5 years ago

Even when I'm doing install from git I'm getting this error

PhanindraPanthagani commented 4 years ago

I am also facing similar error of broken pipe, Please help us solve this @bjkomer

dragomir44 commented 4 years ago

I'm getting the same error.

bjkomer commented 4 years ago

@dragomir44 which line is causing the error? Are you using master?

From searching through the code, the only place where n_iter is used as an argument is with BernoulliRBM, which according to the sklearn docs does indeed have that parameter.

Kelvinli5354 commented 4 years ago

@bjkomer Hi I'm also getting a similar issue

~\Anaconda3\lib\site-packages\hpsklearn\components.py in sklearn_SGDClassifier(*args, **kwargs)
     89 def sklearn_DecisionTreeClassifier(*args, **kwargs):
     90     return sklearn.tree.DecisionTreeClassifier(*args, **kwargs)
---> 91 
     92 @scope.define
     93 def sklearn_Lasso(*args, **kwargs):

TypeError: __init__() got an unexpected keyword argument 'n_iter'

On line 91 of components.py. I cloned a copy of the repository but I can't find out where n_iter is located that's causing the problem.

shoaib03339770257 commented 4 years ago

TypeError: init() got an unexpected keyword argument 'n_iter'

gentrexha commented 4 years ago

@bjkomer Hi I'm also getting a similar issue

~\Anaconda3\lib\site-packages\hpsklearn\components.py in sklearn_SGDClassifier(*args, **kwargs)
     89 def sklearn_DecisionTreeClassifier(*args, **kwargs):
     90     return sklearn.tree.DecisionTreeClassifier(*args, **kwargs)
---> 91 
     92 @scope.define
     93 def sklearn_Lasso(*args, **kwargs):

TypeError: __init__() got an unexpected keyword argument 'n_iter'

On line 91 of components.py. I cloned a copy of the repository but I can't find out where n_iter is located that's causing the problem.

Did you mange to fix this error?

nils-fl commented 4 years ago

For me pip install git+https://github.com/hyperopt/hyperopt-sklearn.git did the trick. Thanks!