GithubHelp home page GithubHelp logo

Comments (5)

david-cortes avatar david-cortes commented on August 19, 2024

I'm unable to reproduce the error. Does the following work for you?

import numpy as np, pickle
from sklearn.linear_model import SGDClassifier
from contextualbandits.online import AdaptiveGreedy
X = np.random.normal(size=(1000, 20))
a = np.random.randint(5, size=1000)
r = (np.random.random(size=1000) >= 0.4).astype('int64')
m = BootstrappedUCB(SGDClassifier(loss="log"), nchoices=5)
m.partial_fit(X, a, r)
pickle.dump(m, open("cb_pickle.p", "wb"))

Then loading with

import pickle
pickle.load(open("cb_pickle.p", "rb"))

What Python version are you using? Are you trying to load it on a different Python version (or virtual environment) than the one you saved the model in?

from contextualbandits.

kaustubrao avatar kaustubrao commented on August 19, 2024

In the code you shared, batch_train has not been set to True. The pickling and loading works fine, but when I try to use partial_fit on new data points, I get the following error -

AttributeError: '_ArrBSClassif' object has no attribute 'beta_counters'

If I set batch_train to True, and execute the same code, then the pickled object does not load and I get the error -

AttributeError: 'SGDClassifier' object has no attribute '_robust_predict'

This error occurs even if I pickle and load in the same notebook, so I don't think it is due to differences in python version (Python 3.6.4 is the version I'm using).

from contextualbandits.

david-cortes avatar david-cortes commented on August 19, 2024

In the code you shared, batch_train has not been set to True. The pickling and loading works fine, but when I try to use partial_fit on new data points, I get the following error -

AttributeError: '_ArrBSClassif' object has no attribute 'beta_counters'

If I set batch_train to True, and execute the same code, then the pickled object does not load and I get the error -

AttributeError: 'SGDClassifier' object has no attribute '_robust_predict'

This error occurs even if I pickle and load in the same notebook, so I don't think it is due to differences in python version (Python 3.6.4 is the version I'm using).

Ok, got the error now, will try to fix it later today. Thanks for reporting the problem!

from contextualbandits.

david-cortes avatar david-cortes commented on August 19, 2024

After taking a deeper look at it, I think making it work with pickle would imply modifying a large fraction of the code in order not to break other things, which I probably won’t do in the near future, so in the meantime I suggest you use dill instead of pickle, which doesn’t have problems serializing bound methods.

from contextualbandits.

kaustubrao avatar kaustubrao commented on August 19, 2024

I tried using dill. Seems to be working fine. Thanks for the tip!

from contextualbandits.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.