Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and load Glove models?

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')
like image 701
Namitha K Avatar asked Oct 17 '22 22:10

Namitha K


2 Answers

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)
like image 143
Palak Bansal Avatar answered Nov 15 '22 10:11

Palak Bansal


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)))
like image 39
Akash Kandpal Avatar answered Nov 15 '22 10:11

Akash Kandpal