Load Pretrained glove vectors in python

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

    import os
    import numpy as np
    
    # store all the pre-trained word vectors
    print('Loading word vectors...')
    word2vec = {}
    with open(os.path.join('glove/glove.6B.%sd.txt' % EMBEDDING_DIM)) as f: #enter the path where you unzipped the glove file
      # is just a space-separated text file in the format:
      # word vec[0] vec[1] vec[2] ...
        for line in f:
            values = line.split()
            word = values[0]
            vec = np.asarray(values[1:], dtype='float32')
            word2vec[word] = vec
    print('Found %s word vectors.' % len(word2vec))
    

提交回复
热议问题