Load Pretrained glove vectors in python

前端 未结 10 669
眼角桃花
眼角桃花 2021-01-29 22:12

I have downloaded pretrained glove vector file from the internet. It is a .txt file. I am unable to load and access it. It is easy to load and access a word vector binary file u

10条回答
  •  误落风尘
    2021-01-29 22:45

    I suggest using gensim to do everything. You can read the file, and also benefit from having a lot of methods already implemented on this great package.

    Suppose you generated GloVe vectors using the C++ program and that your "-save-file" parameter is "vectors". Glove executable will generate you two files, "vectors.bin" and "vectors.txt".

    Use glove2word2vec to convert GloVe vectors in text format into the word2vec text format:

    from gensim.scripts.glove2word2vec import glove2word2vec
    glove2word2vec(glove_input_file="vectors.txt", word2vec_output_file="gensim_glove_vectors.txt")
    

    Finally, read the word2vec txt to a gensim model using KeyedVectors:

    from gensim.models.keyedvectors import KeyedVectors
    glove_model = KeyedVectors.load_word2vec_format("gensim_glove_vectors.txt", binary=False)
    

    Now you can use gensim word2vec methods (for example, similarity) as you'd like.

提交回复
热议问题