Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to implement word2vec model and getting Attribute error

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

Below is the code :

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

Please let me know the issue ?

like image 492
Rishabh Rusia Avatar asked Oct 18 '22 16:10

Rishabh Rusia


2 Answers

gojomo's answer is right

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

try to upgrade all dependencies of gensim(e.g. smart_open), if you still have errors as follows

pip install --upgrade gensim

File "/home/liangn/PythonProjects/DeepRecommendation/Algorithm/Word2Vec.py", line 18, in init self.model = gensim.models.KeyedVectors.load_word2vec_format(w2v_path, binary=True)

File "/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/gensim/models/keyedvectors.py", line 191, in load_word2vec_format with utils.smart_open(fname) as fin:

File "/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/smart_open/smart_open_lib.py", line 138, in smart_open return file_smart_open(parsed_uri.uri_path, mode)

File "/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/smart_open/smart_open_lib.py", line 642, in file_smart_open return compression_wrapper(open(fname, mode), fname, mode)

File "/home/liangn/PythonProjects/venvLiang/lib/python2.7/site-packages/smart_open/smart_open_lib.py", line 630, in compression_wrapper return make_closing(GzipFile)(file_obj, mode)

File "/usr/lib64/python2.7/gzip.py", line 94, in init fileobj = self.myfileobj = builtin.open(filename, mode or 'rb')

TypeError: coercing to Unicode: need string or buffer, file found

like image 169
Nan Liang Avatar answered Oct 21 '22 07:10

Nan Liang


How did you install gensim, and what version is installed?

API changes in (currently pre-release) gensim 1.0 move load_word2vec_format() to a helper class called KeyedVectors.

At this point (February 2017) you probably don't want to be using a pre-release version unless you're an experienced gensim user and closely follow the release-notes [CHANGELOG.md][1].

If intentionally using a later version of gensim with this API change, you would instead use:

KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin.gz", binary=True)
like image 22
gojomo Avatar answered Oct 21 '22 09:10

gojomo