josephreisinger / vowpal_porpoise

lightweight python wrapper for vowpal wabbit
http://josephreisinger.github.io/vowpal_porpoise/
Other
166 stars 30 forks source link

Unable to pickle the estimator VW_Classifier #4

Open viveksck opened 11 years ago

viveksck commented 11 years ago

Steps to reproduce: Use example_sklearn.py.

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

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 commented 11 years ago

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 commented 11 years ago

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 commented 10 years ago

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

viveksck commented 10 years ago

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 notifications@github.com 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/josephreisinger/vowpal_porpoise/issues/4#issuecomment-36739494 .

viveksck commented 10 years ago

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)