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

showstopper issue - AttributeError: 'hyperopt_estimator' object has no attribute 'classifier' #168

Open
snaraya7 opened this issue Feb 27, 2021 · 12 comments

Comments

@snaraya7
Copy link

snaraya7 commented Feb 27, 2021

Thank You for hyperopt-sklearn it is a great package.

Recently, even this basic code fails:

from hpsklearn import HyperoptEstimator, any_classifier, any_preprocessing
from hyperopt import tpe


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

print(estim)
 File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\base.py", line 260, in __repr__
    repr_ = pp.pformat(self)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\Lib\pprint.py", line 144, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\Lib\pprint.py", line 161, in _format
    rep = self._repr(object, context, level)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\Lib\pprint.py", line 393, in _repr
    self._depth, level)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\_pprint.py", line 181, in format
    changed_only=self._changed_only)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\_pprint.py", line 425, in _safe_repr
    params = _changed_params(object)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\_pprint.py", line 91, in _changed_params
    params = estimator.get_params(deep=False)
  File "C:\Users\ncshr\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\base.py", line 195, in get_params
    value = getattr(self, key)
AttributeError: 'hyperopt_estimator' object has no attribute 'classifier'

Windows 10 machine, Python 3.7.1, hpsklearn 0.1.0, networkx-2.5
Please suggest

@bjkomer
Copy link
Member

bjkomer commented Mar 2, 2021

My guess would be you have an older version of hpsklearn. Have you tried installing the one from master? The version I get when I do that is 0.0.3 which is different than yours.

@snaraya7
Copy link
Author

snaraya7 commented Mar 2, 2021

Thank you for your suggestion. But installation from a git clone failed with

Cloning into 'hyperopt-sklearn'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists. 

Tried both a) cloning master from https://github.com/hyperopt/hyperopt-sklearn

and b) pip install hpsklearn==0.0.3 and still getting the same AttributeError: 'hyperopt_estimator' object has no attribute 'classifier' error. Further, my IDE is showing an option to upgrade to the latest 0.1.0, and that currently, now I have is 0.0.3

@mgbckr
Copy link

mgbckr commented Mar 14, 2021

Hi @snaraya7, for cloning master, maybe try via https rather than ssh (git clone https://github.com/hyperopt/hyperopt-sklearn.git). That worked for me.

@snaraya7
Copy link
Author

snaraya7 commented Mar 14, 2021

@mgbckr Thank You! Unfortunately receiving the same error. I am using Python 3.7.1. Tried installing as well as referring the package separately in IDE, but neither works.

@mgbckr
Copy link

mgbckr commented Mar 14, 2021

@snaraya7 Just started playing with it, too, and I actually get the same issue :D

So the issue lies within the __init__ method which does not abide by the sklearn rules asking for "dumb" constructor that just copies the parameters to class variables. Specifically, classifier (among others) is not copied and thus when calling get_params things break since the corresponding sklearn functions looks for the local variables specified by the constructor.

@snaraya7
Copy link
Author

Yay! thanks for debugging, please let me know if you have a fix. Looking into your explanation.

@mgbckr
Copy link

mgbckr commented Mar 15, 2021

Tried to come up with a solution. Please test if you get a chance.

@snaraya7
Copy link
Author

snaraya7 commented Mar 15, 2021

Yes, not fully resolved. Are you able to run (estimate) a best model for some dataset ? Getting newer issues with components and freeze_support().

estim = HyperoptEstimator( classifier=any_sparse_classifier('clf'), 
                            preprocessing=[tfidf('tfidf')],
                            algo=tpe.suggest, trial_timeout=300)

estim.fit( X_train, y_train )

print( estim.score( X_test, y_test ) )
print( estim.best_model() )

@mgbckr
Copy link

mgbckr commented Mar 15, 2021

Works fine for me it seems. If you can provide a minimal example and an error message, I'd try to have a look if I can find the time.

from hpsklearn import HyperoptEstimator, any_classifier, any_preprocessing
from hyperopt import tpe
import sklearn.datasets

e = HyperoptEstimator(
    classifier=any_classifier('my_clf'),
  preprocessing=any_preprocessing('my_pre'),
  algo=tpe.suggest,
  max_evals=2,
  trial_timeout=5)
# e.get_params()

X, y = sklearn.datasets.make_classification()
e.fit(X,y)
e.predict(X)
print(e.score(X, y))
print(e.best_model())

@bjkomer
Copy link
Member

bjkomer commented Mar 16, 2021

Had some time to look into this a bit more, and it seems the freeze_support error is a windows specific issue. You can try the solution here:
https://stackoverflow.com/questions/24374288/where-to-put-freeze-support-in-a-python-script/24374798#24374798

some answers are saying you may also need to add a freeze_support line explicitly like this:

from multiprocessing import freeze_support

if __name__ == '__main__':
    freeze_support()
    main()

This looks to be the same issue as #72 and #163

@hjort
Copy link

hjort commented Apr 14, 2021

Same issue on Ubuntu Linux with Python 3.6 on Anaconda:

$ python
Python 3.6.7 |Anaconda custom (32-bit)| (default, Oct 23 2018, 19:27:27)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from hpsklearn import HyperoptEstimator, any_classifier, any_preprocessing
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
from hyperopt import tpe

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

print(estim)
Traceback (most recent call last):
File "", line 1, in
File "/opt/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 260, in repr
repr_ = pp.pformat(self)
File "/opt/anaconda3/lib/python3.6/pprint.py", line 144, in pformat
self._format(object, sio, 0, 0, {}, 0)
File "/opt/anaconda3/lib/python3.6/pprint.py", line 161, in _format
rep = self._repr(object, context, level)
File "/opt/anaconda3/lib/python3.6/pprint.py", line 393, in _repr
self._depth, level)
File "/opt/anaconda3/lib/python3.6/site-packages/sklearn/utils/_pprint.py", line 181, in format
changed_only=self._changed_only)
File "/opt/anaconda3/lib/python3.6/site-packages/sklearn/utils/_pprint.py", line 425, in _safe_repr
params = _changed_params(object)
File "/opt/anaconda3/lib/python3.6/site-packages/sklearn/utils/_pprint.py", line 91, in _changed_params
params = estimator.get_params(deep=False)
File "/opt/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 195, in get_params
value = getattr(self, key)
AttributeError: 'hyperopt_estimator' object has no attribute 'classifier'

I've installed it through:
pip install git+https://github.com/hyperopt/hyperopt-sklearn

Any thoughts on how to solve this?

@mgbckr
Copy link

mgbckr commented Apr 14, 2021

Checkout my pull request mentioned above #169 . That works for me at least. Maybe you can give it a try.

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

4 participants