Saving the model for production
There is one more thing we need to do before actually using the model. We need to save the following objects for future use:
- The dictionary list
- The model
- The BoW object
- The TF-IDF object
Let’s start by saving the dictionary list.
Gensim has a utility function called datapath
. This is where we can specify the physical location of the file. Then, we can save the dictionary using the .save()
function. Here is the code for it:
from gensim.test.utils import datapathdict_file = datapath(path + “/gensim_dictionary_AGnews”) gensim_dictionary.save(dict_file)
Save the model as follows:
lsi_model.save(path + “/ag_news_lsi_model”)
Save the BoW object using pickle
:
import picklefile = open(path + “/BoW_AGnews_corpus.pkl”, ‘wb’) pickle.dump(bow_corpus, file) file.close()
Save the TF-IDF object using pickle
:
import picklefile = open(path + “/tfidf_AGnews_corpus...