AttributeError:类型对象'Word2Vec'没有属性'load_word2vec_format'

时间:2017-02-21 09:49:34

标签: python nlp gensim word2vec

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

  

AttributeError:类型对象'Word2Vec'没有属性'load_word2vec_format'

以下是代码:

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

请让我知道这个问题?

2 个答案:

答案 0 :(得分:1)

你是如何安装gensim的,以及安装了什么版本的?

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

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

如果故意在此API更改时使用更高版本的gensim,则应使用:

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

答案 1 :(得分:0)

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 file_smart_open(parsed_uri.uri_path,mode)

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

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

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

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