Giter Club home page Giter Club logo

Comments (7)

meownoid avatar meownoid commented on June 29, 2024 9

NMSLibAlternatingLeastSquares is also unpickable because of error can't pickle nmslib.dist.FloatIndex objects.

from implicit.

benfred avatar benfred commented on June 29, 2024 6

This isn't something that is explicitly supported - but should work for all the models except for the AnnoyAlternatingLeastSquares class (since annoy doesn't support pickle).

from implicit.

hashAI avatar hashAI commented on June 29, 2024 5

In case of NMSLibAlternatingLeastSquares, index and model can be saved/pickled separately. Following code works for me.

class NMSLibAlternatingLeastSquaresRecommender(implicit.approximate_als.NMSLibAlternatingLeastSquares):
    def save(self, dirpath):
        pkl_file = os.path.join(dirpath, 'model.pickle')
        index_file = os.path.join(dirpath, 'search_index')
        self.recommend_index.saveIndex(filename=index_file, save_data=True)
        self.recommend_index = None
        with open(pkl_file, 'wb') as pickle_out:
            pickle.dump(self, pickle_out)

def load_model(dirpath):
    pkl_file = os.path.join(dirpath, 'model.pickle')
    index_file = os.path.join(dirpath, 'search_index')
    with open(pkl_file, 'rb') as pickle_in:
        model = pickle.load(pickle_in)
    model.recommend_index = nmslib.init(method="hnsw", space="cosinesimil")
    model.recommend_index.loadIndex(filename=index_file, load_data=True)
    return model

from implicit.

singularperturbation avatar singularperturbation commented on June 29, 2024

With #47 , you can save the AnnoyAlternatingLeastSquares model using annoy's serialization methods.

from implicit.

Oberonhive avatar Oberonhive commented on June 29, 2024

Cant pickle FaissAlternatingLeastSquares model too.
error: can't pickle SwigPyObject objects

from implicit.

chanansh avatar chanansh commented on June 29, 2024

@benfred can you please add save and load as suggested above?

from implicit.

chanansh avatar chanansh commented on June 29, 2024

In case of NMSLibAlternatingLeastSquares, index and model can be saved/pickled separately. Following code works for me.

class NMSLibAlternatingLeastSquaresRecommender(implicit.approximate_als.NMSLibAlternatingLeastSquares):
    def save(self, dirpath):
        pkl_file = os.path.join(dirpath, 'model.pickle')
        index_file = os.path.join(dirpath, 'search_index')
        self.recommend_index.saveIndex(filename=index_file, save_data=True)
        self.recommend_index = None
        with open(pkl_file, 'wb') as pickle_out:
            pickle.dump(self, pickle_out)

def load_model(dirpath):
    pkl_file = os.path.join(dirpath, 'model.pickle')
    index_file = os.path.join(dirpath, 'search_index')
    with open(pkl_file, 'rb') as pickle_in:
        model = pickle.load(pickle_in)
    model.recommend_index = nmslib.init(method="hnsw", space="cosinesimil")
    model.recommend_index.loadIndex(filename=index_file, load_data=True)
    return model

I get

class NMSLibAlternatingLeastSquaresRecommender(NMSLibAlternatingLeastSquares):
TypeError: function() argument 'code' must be code, not str
python-BaseException

since NMSLibAlternatingLeastSquares is actually a function?

from implicit.

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.