How to locally save gensim glove pretrained vectors?

Viewed 327

I have downloaded the wikipedia glove vectors using the gensim API. I want to save it locally so I don't have to call the API everytime to download it. How can I do this? I have looked but I am not sure if this is the right way to save them.

import gensim.downloader as api

vectors = api.load('glove-wiki-gigaword-50')
vectors.save('vectors.bin')
1 Answers

The code you've shown in your question likely worked to save the vectors.

You can check the class type of the object that the api.load() gave you with code like:

print(type(vectors))

I think that will be shown to be an instance of KeyedVectors.

If so, then reloading the file you've already saved should be as simple as:

from gensim.models import KeyedVectors
reloaded_vectors = KeyedVectors.load('vectors.bin')

Note that the native Gensim .save(), for any vectors of significant size, will usually be split over multiple files that all start with vectors.bin – so if you then want to move the model elsewhere, move all those files together.

You can also save/load vectors in a simpler format with the .save_word2vec_format() & .load_word2vec_format() methods. EG:

vectors.save_word2vec_format('vectors.txt', binary=False)
vectors_reloaded = KeyedVectors.load_word2vec_format('vectors.txt', binary=False)
Related