Skip to main content
 首页 » 编程设计

python之如何将word2vec转为glove格式

2024年12月31日9mfryf

我做了一些研究,发现 gensim 有一个脚本可以将 glove 转换为 word2vec GLove2Wrod2Vec .我希望做相反的事情。

是否有使用gensim或任何其他库进行转换的简单方法

请您参考如下方法:

手套矢量文件格式和word2vec文件格式之间的唯一区别是.txt开头的一行的 word2vec 格式有

<num words> <num dimensions>

否则向量以相同的方式表示。我们不需要更改向量来更改格式。

引用您在问题中链接的页面:

Both files are 
presented in text format and almost identical except that word2vec includes 
number of vectors and its dimension which is only difference regard to GloVe. 
Notes 
----- 
GloVe format (real example can be founded `on Stanford size <https://nlp.stanford.edu/projects/glove/>`_) :: 
    word1 0.123 0.134 0.532 0.152 
    word2 0.934 0.412 0.532 0.159 
    word3 0.334 0.241 0.324 0.188 
    ... 
    word9 0.334 0.241 0.324 0.188 
Word2Vec format (real example can be founded `on w2v old repository <https://code.google.com/archive/p/word2vec/>`_) :: 
    9 4 
    word1 0.123 0.134 0.532 0.152 
    word2 0.934 0.412 0.532 0.159 
    word3 0.334 0.241 0.324 0.188 
    ... 
    word9 0.334 0.241 0.324 0.188 

在上面的例子中,word2vec的第一行9 4告诉我们词汇表中有 9 个词,每个词有 4 个维度。

长话短说 因此,要从 w2v 转换-> glove : 删除 <num words> <num dimensions>来自 w2v 的行.无论如何,您都可以从文件中推断出它。

从 glove 转换-> w2v : 添加 <num words> <num dimensions>线到 glove .

您可以手动完成,但 gensim 提供了一种从一个到另一个的方法。