I am using Python 3.5 to do my research. I want to make use of Glove word embeddings. How can I save and load my Glove model after glove.fit? I have coded it like this
glove.fit(corpus.matrix,epochs=1,no_threads=4,verbose=True)
glove.save('glove.model')
from gensim.models import KeyedVectors
# load the Stanford GloVe model
model = KeyedVectors.load_word2vec_format(filename, binary=False)
If your model is contained in the variable 'model'
You can save the model like this:
model.save('model.bin')
You can load the saved model like this:
new_model = KeyedVectors.load('model.bin')
You can now use the loaded model:
result = new_model.most_similar(positive=['woman', 'king'], negative=['man'], topn=1)
Check this here.
Now after getting your data trained, use this :
from gensim.scripts.glove2word2vec import glove2word2vec
glove2word2vec(glove_input_file=file, word2vec_output_file="gensim_glove_vectors.txt")
from gensim.models.keyedvectors import KeyedVectors
model = KeyedVectors.load_word2vec_format("gensim_glove_vectors.txt", binary=False)
Afterwards you can use this just as you use a gensim model. Eg,
print("Similarity between {} and {} is {}".format(word1,word2,model.wv.similarity(word1, word2)))
print("Most similar words to {} are :{}\n".format(word1,model.most_similar(positive=[word1],topn=10)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With