implicit
implicit copied to clipboard
Pickling
Are the models pickle-able? If so close this question, if not -- consider it a feature request!
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).
With #47 , you can save the AnnoyAlternatingLeastSquares model using annoy's serialization methods.
NMSLibAlternatingLeastSquares is also unpickable because of error can't pickle nmslib.dist.FloatIndex objects.
Cant pickle FaissAlternatingLeastSquares model too.
error: can't pickle SwigPyObject objects
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
@benfred can you please add save and load as suggested above?
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?