-
Notifications
You must be signed in to change notification settings - Fork 30
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
Comments
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. |
Thanks . I actually used a workaround described here to get the object to pickle": |
Hi viveksck. I'm having the same problem. Could you please post the code you created for fixing it? Many thanks! |
Hi, Thanks. On Wed, Mar 5, 2014 at 8:00 AM, goite [email protected] wrote:
|
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) |
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.
The text was updated successfully, but these errors were encountered: