Gensim: how to load precomputed word vectors from text file

依然范特西╮ 提交于 2021-02-18 22:17:46

问题


I have a text file with my precomputed word vectors in the following format (example):

word -0.0762464299711 0.0128308048976 ... 0.0712385589283\n’

on each line for every word (with 297 extra floats in place of the ...). I am trying to load these with Gensim as KeyedVectors, because I ultimately would like to compute the cosine similarity, find most similar words, etc. Unfortunately I have not worked with Gensim before and from the documentation it's not quite clear to me how to do this. I have tried the following which I found here:

word_vectors = KeyedVectors.load_word2vec_format('/embeddings/word.vectors', binary=False)

However this gives the following error:

ValueError: invalid literal for int() with base 10: 'the'

'the' is the first word in the text file, so I suspect that the loading function is expecting something to be there that is not. But I can't find any information on what should be there. I would highly appreciate a pointer to such information or any other solution to my problem. Thanks!


回答1:


You can see here an example of Word2Vec format. The first line is supposed to contain the number of words you have in your file followed by the dimension of your vectors. This is probably why your script is returning you an error.

In your example :

1 300
word -0.0762464299711 0.0128308048976 ... 0.0712385589283


来源:https://stackoverflow.com/questions/49750112/gensim-how-to-load-precomputed-word-vectors-from-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!