Load Pretrained glove vectors in python

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

    Here's a one liner if all you want is the embedding matrix

    np.loadtxt(path, usecols=range(1, dim+1), comments=None)

    where path is path to your downloaded GloVe file and dim is the dimension of the word embedding.

    If you want both the words and corresponding vectors you can do

    glove = np.loadtxt(path, dtype='str', comments=None)

    and seperate the words and vectors as follows

    words = glove[:, 0]
    vectors = glove[:, 1:].astype('float')
    

提交回复
热议问题