implicit icon indicating copy to clipboard operation
implicit copied to clipboard

Pickling

Open martingms opened this issue 8 years ago • 7 comments
trafficstars

Are the models pickle-able? If so close this question, if not -- consider it a feature request!

martingms avatar Jul 13 '17 13:07 martingms

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).

benfred avatar Jul 13 '17 18:07 benfred

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

singularperturbation avatar Oct 04 '17 05:10 singularperturbation

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

meownoid avatar Jan 15 '18 14:01 meownoid

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

Oberonhive avatar Jan 30 '20 13:01 Oberonhive

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

hashAI avatar Mar 10 '21 09:03 hashAI

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

chanansh avatar Mar 14 '22 17:03 chanansh

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?

chanansh avatar Mar 14 '22 20:03 chanansh