一行代码搞定加载glove预训练词向量

加载glove预训练词向量再也不用glove2word2vec转换啦!

以前加载glove预训练词向量的方法

from gensim.scripts.glove2word2vec import glove2word2vec
glove2word2vec('glove.6B.50d.txt', 'word2vec50d.txt')

其实就是在原来的txt文件前面加上了一行信息,行和列。

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

变成

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

但是,这个处理要等很久。

运行之后会得出这样的一个Warning

Warning (from warnings module):
  File "<pyshell#1>", line 1
DeprecationWarning: Call to deprecated `glove2word2vec` (KeyedVectors.load_word2vec_format(.., binary=False, no_header=True) loads GLoVE text vectors.).
(400000, 50)

在这里插入图片描述
意思就是这个函数过期了,有一个更新的函数,load_word2vec_format加上no_header参数,就可以直接使用glove文件,而不经转换啦!

>>> from gensim.models.keyedvectors import KeyedVectors
>>> wv=KeyedVectors.load_word2vec_format('glove.6B.50d.txt',binary=

你可能感兴趣的:(python,机器学习,人工智能)