导入GoogleNews-vectors-negative300.bin

时间:2017-09-26 18:51:02

标签: python gensim

我正在使用gensim处理代码,并且很难在我的代码中对ValueError进行故障排除。我终于能够压缩GoogleNews-vectors-negative300.bin.gz文件了,所以我可以在我的模型中实现它。我也试过gzip,结果不成功。代码中的错误发生在最后一行。我想知道如何解决错误。有没有解决方法?最后,有一个我可以参考的网站吗?

谢谢你的帮助!

import gensim
from keras import backend
from keras.layers import Dense, Input, Lambda, LSTM, TimeDistributed
from keras.layers.merge import concatenate
from keras.layers.embeddings import Embedding
from keras.models import Mode

pretrained_embeddings_path = "GoogleNews-vectors-negative300.bin"
word2vec = 
gensim.models.KeyedVectors.load_word2vec_format(pretrained_embeddings_path, 
binary=True)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-23bd96c1d6ab> in <module>()
  1 pretrained_embeddings_path = "GoogleNews-vectors-negative300.bin"
----> 2 word2vec = 
gensim.models.KeyedVectors.load_word2vec_format(pretrained_embeddings_path, 
binary=True)

C:\Users\green\Anaconda3\envs\py35\lib\site-
packages\gensim\models\keyedvectors.py in load_word2vec_format(cls, fname, 
fvocab, binary, encoding, unicode_errors, limit, datatype)
244                             word.append(ch)
245                     word = utils.to_unicode(b''.join(word), 
encoding=encoding, errors=unicode_errors)
--> 246                     weights = fromstring(fin.read(binary_len), 
dtype=REAL)
247                     add_word(word, weights)
248             else:

ValueError: string size must be a multiple of element size

3 个答案:

答案 0 :(得分:10)

答案 1 :(得分:6)

以下命令有效。

brew install wget

wget -c "https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz"

然后您可以使用以下命令获取 wordVector

from gensim import models

w = models.KeyedVectors.load_word2vec_format(
    '../GoogleNews-vectors-negative300.bin', binary=True)

答案 2 :(得分:1)

尝试-

import gensim.downloader as api

wv = api.load('word2vec-google-news-300')

vec_king = wv['king']

另外,请访问此链接:https://radimrehurek.com/gensim/auto_examples/tutorials/run_word2vec.html#sphx-glr-auto-examples-tutorials-run-word2vec-py