AttributeError: type object 'Word2Vec' has no attribute 'load_word2vec_format'

AttributeError: type object 'Word2Vec' has no attribute 'load_word2vec_format'

我正在尝试实现 word2vec 模型并出现属性错误

AttributeError: type object 'Word2Vec' has no attribute 'load_word2vec_format'

下面是代码:

wv = Word2Vec.load_word2vec_format("GoogleNews-vectors-negative300.bin.gz", binary=True)
wv.init_sims(replace=True)

请让我知道这个问题?

你是怎么安装gensim的,安装的是什么版本?

API gensim 1.0 中的更改(当前预发布)将 load_word2vec_format() 移动到名为 KeyedVectors 的助手 class。

此时(2017 年 2 月)您可能不想使用预发布版本,除非您是经验丰富的 gensim 用户并密切关注发行说明 [CHANGELOG.md][1]

如果有意使用具有此 API 更改的更高版本的 gensim,您将改用:

KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin.gz", binary=True)
gojomo的回答是对的

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

尝试升级gensim的所有依赖(如smart_open),如果仍然报错如下

pip install --upgrade gensim

文件“/home/liangn/PythonProjects/DeepRecommendation/Algorithm/Word2Vec.py”,第 18 行,在 init 中 self.model = gensim.models.KeyedVectors.load_word2vec_format(w2v_path, binary=True)

文件“/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/gensim/models/keyedvectors.py”,第 191 行,在 load_word2vec_format 中,utils.smart_open(fname) 作为 fin:

文件“/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/smart_open/smart_open_lib.py”,第 138 行,在 smart_open return 文件_smart_open(parsed_uri.uri_path, 模式)

文件“/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/smart_open/smart_open_lib.py”,第 642 行,在 file_smart_open 中 return compression_wrapper(打开(fname, 模式), fname, 模式)

文件“/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/smart_open/smart_open_lib.py”,第 630 行,在 compression_wrapper return make_closing(GzipFile)(file_obj, 模式)

文件“/usr/lib64/python2.7/gzip.py”,第 94 行,在 init 中 fileobj = self.myfileobj = 内置.open(文件名,模式或'rb')

TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到文件