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

10,684

Solution 1

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

Solution 2

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)
Share:
10,684
Rishabh Rusia
Author by

Rishabh Rusia

Updated on June 25, 2022

Comments

  • Rishabh Rusia
    Rishabh Rusia almost 2 years

    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 ?

  • Rishabh Rusia
    Rishabh Rusia about 7 years
    I am new to gensim and using 1.0.0rc2. I tried using KeyedVector: import KeyedVectors KeyedVectors.load_word2vec_format("GoogleNews-vectors-negati‌​ve300.bin.gz", binary=True) but still I am getting the error. I am using Python 2.7, should I upgrade to Anaconda as some packages works good there
  • gojomo
    gojomo about 7 years
    How did you install gensim? I don't believe pip install gensim would get you that pre-release version. What actual error are you getting after trying to use KeyedVector? (It can't possible be the same error, and the exact messages matter for solving.)