导入此时遇到此错误
from gensim.models import KeyedVectors
错误是
File "c:Usersfraseeg1.py", line 11, in <module>
from gensim.models import KeyedVectors
File "C:UsersfraseAppDataLocalProgramsPythonPython39libsite-packagesgensim__init__.py", line 11, in <module>
from gensim import parsing, corpora, matutils, interfaces, models, similarities, utils # noqa:F401
File "C:UsersfraseAppDataLocalProgramsPythonPython39libsite-packagesgensimcorpora__init__.py", line 6, in <module>
from .indexedcorpus import IndexedCorpus # noqa:F401 must appear before the other classes
File "C:UsersfraseAppDataLocalProgramsPythonPython39libsite-packagesgensimcorporaindexedcorpus.py", line 14, in <module>
from gensim import interfaces, utils
File "C:UsersfraseAppDataLocalProgramsPythonPython39libsite-packagesgensiminterfaces.py", line 19, in <module>
from gensim import utils, matutils
File "C:UsersfraseAppDataLocalProgramsPythonPython39libsite-packagesgensimmatutils.py", line 1024, in <module>
from gensim._matutils import logsumexp, mean_absolute_difference, dirichlet_expectation
File "gensim/_matutils.pyx", line 1, in init gensim._matutils
#!/usr/bin/env cython
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject
我以前曾使用KeyedVectors在模型中加载,但当我安装"TensorFlow"时,这种情况就开始出现了。
我查阅了这个问题,并将Numpy的版本降低到1.19.2(这是Tensorflow的最大值(,但这并没有奏效。我也试过简单地卸载和重新安装,但也没有成功。
发生这种情况的原因是什么?
用于的代码示例(注意:我无法测试它,因此可能存在错误(:
model = KeyedVectors.load_word2vec_format(
'Word2vecGoogleNews-vectors-negative300.bin.gz', binary=True)
# create the embedding matrix
embedding_matrix = np.zeros((vocab_size, 300)) # 300 is dim size
for word, vector in model.key_to_vectors():
if(word in tokenizer.word_index):
idx = tokenizer.word_index[word]
embedding_matrix[idx] = np.array(vector, dtype=np.float32)
解决方案。
-
将python环境降级到3.8/3.7
-
卸载Numpy并升级到最新的Numpy版本
引用-值错误:numpy.ndarray大小已更改,可能表示二进制不兼容。预期88来自C标头,得到80来自PyObject