Load Pretrained glove vectors in python

前端 未结 10 715
眼角桃花
眼角桃花 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:32

    glove model files are in a word - vector format. You can open the textfile to verify this. Here is a small snippet of code you can use to load a pretrained glove file:

    import numpy as np
    
    def loadGloveModel(File):
        print("Loading Glove Model")
        f = open(File,'r')
        gloveModel = {}
        for line in f:
            splitLines = line.split()
            word = splitLines[0]
            wordEmbedding = np.array([float(value) for value in splitLines[1:]])
            gloveModel[word] = wordEmbedding
        print(len(gloveModel)," words loaded!")
        return gloveModel
    

    You can then access the word vectors by simply using the gloveModel variable.

    print gloveModel['hello']

提交回复
热议问题