5

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')
Namitha K
  • 51
  • 1
  • 5

2 Answers2

2
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)
Palak Bansal
  • 810
  • 12
  • 26
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)))
Akash Kandpal
  • 3,126
  • 28
  • 25