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

Unable to pickle the estimator VW_Classifier #4

Open
viveksck opened this issue Sep 4, 2013 · 5 comments
Open

Unable to pickle the estimator VW_Classifier #4

viveksck opened this issue Sep 4, 2013 · 5 comments

Comments

@viveksck
Copy link
Contributor

viveksck commented Sep 4, 2013

Steps to reproduce:
Use example_sklearn.py.

Try to pickle the best_estimator returned after the GridSearchCV: (pickle gs.best_estimator_)

You will get the error:
Traceback (most recent call last):
File "example_sklearn1.py", line 66, in
main()
File "example_sklearn1.py", line 63, in main
joblib.dump(estimator, "m.pkl")
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 367, in dump
pickler.dump(value)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 249, in save
return Pickler.save(self, obj)
File "/usr/lib/python2.7/pickle.py", line 331, in save
self.save_reduce(obj=obj, _rv)
File "/usr/lib/python2.7/pickle.py", line 419, in save_reduce
save(state)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 249, in save
return Pickler.save(self, obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 681, in _batch_setitems
save(v)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 249, in save
return Pickler.save(self, obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 725, in save_inst
save(stuff)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 249, in save
return Pickler.save(self, obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 681, in _batch_setitems
save(v)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 249, in save
return Pickler.save(self, obj)
File "/usr/lib/python2.7/pickle.py", line 331, in save
self.save_reduce(obj=obj, rv)
File "/usr/lib/python2.7/pickle.py", line 396, in save_reduce
save(cls)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 249, in save
return Pickler.save(self, obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 748, in save_global
(obj, module, name))
_pickle.PicklingError: Can't pickle <type 'instancemethod'>: it's not found as builtin.instancemethod

Exception raised <bound method VW.push_instance_stdin of <vowpal_porpoise.vw.VW instance at 0x9dc714c>>
My debugging led me to think the following object is not picklable and hence the issue.

@kolesman
Copy link

kolesman commented Sep 7, 2013

Indeed, Pickle is not able to serialize instance method. There are few non-trivial methods to come over this.

But I would reccomnd you to Pickle whole scikit-learn classifier object. Then you can UnPickle it and call estimator.

@viveksck
Copy link
Contributor Author

Thanks . I actually used a workaround described here to get the object to pickle":
http://stackoverflow.com/questions/14306683/cpickle-ignore-stuff-it-cant-serialize-instead-of-raising-an-exception

@goite
Copy link

goite commented Mar 5, 2014

Hi viveksck. I'm having the same problem. Could you please post the code you created for fixing it? Many thanks!

@viveksck
Copy link
Contributor Author

viveksck commented Mar 7, 2014

Hi,
I will give you the diff tonight. I was away for a couple of days. Sorry
about that.

Thanks.
Vivek.

On Wed, Mar 5, 2014 at 8:00 AM, goite [email protected] wrote:

Hi viveksck. I'm having the same problem. Could you please post the code
you created for fixing it? Many thanks!

Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-36739494
.

@viveksck
Copy link
Contributor Author

viveksck commented Mar 8, 2014

The workaround I used is pasted below. Paste this code in the file where you would like to pickle the model. Hope this helps.

import pickle

# Code to pickle a VW model
import copy_reg
from types import FunctionType, FileType, MethodType
def stub_pickler(obj):
    return stub_unpickler, ()
def stub_unpickler():
    return "STUB"

copy_reg.pickle(MethodType, stub_pickler, stub_unpickler)
copy_reg.pickle(FileType,   stub_pickler, stub_unpickler)
copy_reg.pickle(FunctionType, stub_pickler, stub_unpickler)

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

3 participants