Load Pretrained glove vectors in python

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

    You can do it much faster with pandas:

    import pandas as pd
    import csv
    
    words = pd.read_table(glove_data_file, sep=" ", index_col=0, header=None, quoting=csv.QUOTE_NONE)
    

    Then to get the vector for a word:

    def vec(w):
      return words.loc[w].as_matrix()
    

    And to find the closest word to a vector:

    words_matrix = words.as_matrix()
    
    def find_closest_word(v):
      diff = words_matrix - v
      delta = np.sum(diff * diff, axis=1)
      i = np.argmin(delta)
      return words.iloc[i].name
    

提交回复
热议问题