Gensim: 'Word2Vec' object has no attribute 'trainables'

Created on 26 Mar 2018  路  14Comments  路  Source: RaRe-Technologies/gensim

from gensim.models import word2vec
model = word2vec.Word2Vec.load('myModel')

Description

I got this:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/gensim/models/word2vec.py", line 975, in load
    return super(Word2Vec, cls).load(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/gensim/models/base_any2vec.py", line 629, in load
    model = super(BaseWordEmbeddingsModel, cls).load(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/gensim/models/base_any2vec.py", line 278, in load
    return super(BaseAny2VecModel, cls).load(fname_or_handle, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/gensim/utils.py", line 426, in load
    obj._load_specials(fname, mmap, compress, subname)
  File "/usr/local/lib/python3.5/dist-packages/gensim/utils.py", line 469, in _load_specials
    setattr(self, attrib, val)
  File "/usr/local/lib/python3.5/dist-packages/gensim/utils.py", line 1398, in new_func1
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/gensim/models/base_any2vec.py", line 380, in syn1neg
    self.trainables.syn1neg = value
AttributeError: 'Word2Vec' object has no attribute 'trainables'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "word.py", line 164, in <module>
    X_vector, Y_vector = XY_vector(X, Y)
  File "word.py", line 116, in XY_vector
    model = gensim.models.word2vec.Word2Vec.load('./word_vector/Word60.model')
  File "/usr/local/lib/python3.5/dist-packages/gensim/models/word2vec.py", line 979, in load
    return load_old_word2vec(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/gensim/models/deprecated/word2vec.py", line 195, in load_old_word2vec
    new_model.min_alpha_yet_reached = old_model.min_alpha_yet_reached
AttributeError: 'Word2Vec' object has no attribute 'min_alpha_yet_reached'

Versions

I used gensim version 3.4.0, python version 3.5.2

Please share your model (very important for reproducing your error)

What should I do for this?

What's a python and OS?

ubuntu16.04LTS

bug

Most helpful comment

@menshikh-iv https://drive.google.com/open?id=1h_ybqS3ozjRqquesd5I7MHbwirXzDXzr
The whole model. Hope it is useful.

All 14 comments

Thanks for report @narrowsnap, what I need now

  1. Gensim version that was used for training (not for loading)
  2. myModel* files (please add to google-drive or any file-sharing service and post a link here)

I have solved this problem and it is caused by the gensim version being too high. When I lower the gensim version, this error no longer appears

I used other tutorial's trained model. And it's worked when I use gensim version 3.0.0.

@menshikh-iv @manneshiva @janpom there are many reported errors with Word2Vec compatibility. What is going on? Is it because of the refactoring鈥攁re our tests insufficient?

@narrowsnap share model please, this is very important for us

@piskvorky I think so (but I'm not 100% sure because nobody shares a model yet, this is important for reproducing & fixing). Most of this report exactly similar (i.e. this is 1-2 bugs).

Testing is definitely insufficient, because:

  • [IMO main problem] We have some "lazy" initialized attributes (i.e. not defined in __init__) -> this is really hard to test because this attribute happens only in a specific situation (for example, call concrete methods or pass concrete argument to __init__ that launch initialization). If we missed any of this situation - we'll have the backward compatibility problem
  • We have no "strict" backward-compatibility period for models. Our tests covert only several previous version (to be honest, always different). We should define concrete periods for backward-compatibility for models and based on it work on tests
  • We definitely need to re-think how models can be tested in future for save(old)/load(new). This is the serious problem that potentially can be solved in one general way (problem always with missing attributes, no more)

This points not only about w2v-based models, this is about all models that we provided.

@narrowsnap unfortunately, this not a full model, you need to share also Word60.model.* files (like Word60.model.syn0.npy and others).

@menshikh-iv https://drive.google.com/open?id=1h_ybqS3ozjRqquesd5I7MHbwirXzDXzr
The whole model. Hope it is useful.

Still having this issue with:
Debian
python 3.6.3
gensim 3.4.0
loading these models: http://data.dws.informatik.uni-mannheim.de/rdf2vec/models/DBpedia/2015-10/8depth/skipgram/

Here is the traceback:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/gensim/models/word2vec.py", line 975, in load
return super(Word2Vec, cls).load(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/gensim/models/base_any2vec.py", line 629, in load
model = super(BaseWordEmbeddingsModel, cls).load(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/gensim/models/base_any2vec.py", line 278, in load
return super(BaseAny2VecModel, cls).load(fname_or_handle, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/gensim/utils.py", line 426, in load
obj._load_specials(fname, mmap, compress, subname)
File "/usr/local/lib/python3.6/dist-packages/gensim/utils.py", line 469, in _load_specials
setattr(self, attrib, val)
File "/usr/local/lib/python3.6/dist-packages/gensim/utils.py", line 1398, in new_func1
return func(
args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/gensim/models/base_any2vec.py", line 380, in syn1neg
self.trainables.syn1neg = value
AttributeError: 'Word2Vec' object has no attribute 'trainables'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.6/dist-packages/gensim/models/word2vec.py", line 979, in load
return load_old_word2vec(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/gensim/models/deprecated/word2vec.py", line 195, in load_old_word2vec
new_model.min_alpha_yet_reached = old_model.min_alpha_yet_reached
AttributeError: 'Word2Vec' object has no attribute 'min_alpha_yet_reached'

On the other hand, there is no issue on another server with:
Fedora
python 3.6.0
gensim 3.2.0

Thanks @enricopal, this related to #1777 refactoring (for this reason. no error with 3.2.0, but issue with 3.4.0).

As workaround, you should use older gensim version.

@enricopal This bug was fixed in #2012 which was released as a part of Gensim 3.5.0. Please use Gensim version >= 3.5.0, that should solve your issue.

@manneshiva you are partially right: min_alpha_yet_reached fixed, but we have another backward compatibility issue here:

  • pyhon3.6
  • gensim==3.6.0
from gensim.models import Doc2Vec

# load model from http://data.dws.informatik.uni-mannheim.de/rdf2vec/models/DBpedia/2015-10/8depth/skipgram/
m = Doc2Vec.load("DB2Vec_sg_200_5_5_15_4_500", mmap="r")

this code raise

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/models/doc2vec.py in load(cls, *args, **kwargs)
   1091         try:
-> 1092             return super(Doc2Vec, cls).load(*args, **kwargs)
   1093         except AttributeError:

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/models/base_any2vec.py in load(cls, *args, **kwargs)
   1243         """
-> 1244         model = super(BaseWordEmbeddingsModel, cls).load(*args, **kwargs)
   1245         if not hasattr(model, 'ns_exponent'):

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/models/base_any2vec.py in load(cls, fname_or_handle, **kwargs)
    602         """
--> 603         return super(BaseAny2VecModel, cls).load(fname_or_handle, **kwargs)
    604 

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/utils.py in load(cls, fname, mmap)
    422         obj = unpickle(fname)
--> 423         obj._load_specials(fname, mmap, compress, subname)
    424         logger.info("loaded %s", fname)

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/utils.py in _load_specials(self, fname, mmap, compress, subname)
    465 
--> 466             setattr(self, attrib, val)
    467 

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/utils.py in new_func1(*args, **kwargs)
   1421                 )
-> 1422                 return func(*args, **kwargs)
   1423 

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/models/base_any2vec.py in syn1neg(self, value)
    804     def syn1neg(self, value):
--> 805         self.trainables.syn1neg = value
    806 

AttributeError: 'Word2Vec' object has no attribute 'trainables'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-2-82e29c955240> in <module>
----> 1 m = Doc2Vec.load("DB2Vec_sg_200_5_5_15_4_500", mmap="r")

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/models/doc2vec.py in load(cls, *args, **kwargs)
   1094             logger.info('Model saved using code from earlier Gensim Version. Re-loading old model in a compatible way.')
   1095             from gensim.models.deprecated.doc2vec import load_old_doc2vec
-> 1096             return load_old_doc2vec(*args, **kwargs)
   1097 
   1098     def estimate_memory(self, vocab_size=None, report=None):

~/.virtualenvs/p369/lib/python3.6/site-packages/gensim/models/deprecated/doc2vec.py in load_old_doc2vec(*args, **kwargs)
     86     params = {
     87         'dm_mean': old_model.__dict__.get('dm_mean', None),
---> 88         'dm': old_model.dm,
     89         'dbow_words': old_model.dbow_words,
     90         'dm_concat': old_model.dm_concat,

AttributeError: 'Word2Vec' object has no attribute 'dm'

@enricopal in your case, using 3.2.0 are best workaround for now.

@menshikh-iv I do not think that the model that @enricopal is trying to load ( http://data.dws.informatik.uni-mannheim.de/rdf2vec/models/DBpedia/2015-10/8depth/skipgram/) is a Doc2Vec model. Looking at his traceback in this comment, it starts with

File "/usr/local/lib/python3.6/dist-packages/gensim/models/word2vec.py", line 975, in load
return super(Word2Vec, cls).load(*args, **kwargs)

which confirms that he is trying to load a word2vec model.

Also, just to note, in #2012 , we have added tests to load every saved word2vec and doc2vec model starting from Gensim version 0.12.0. That should ensure we get no such errors for version >=0.12.0.

@manneshiva nice catch, you are correct!

ivan@P50:~/Downloads$ hexdump -C DB2Vec_sg_200_5_5_15_4_500 | head
00000000  80 02 63 67 65 6e 73 69  6d 2e 6d 6f 64 65 6c 73  |..cgensim.models|
00000010  2e 77 6f 72 64 32 76 65  63 0a 57 6f 72 64 32 56  |.word2vec.Word2V|
00000020  65 63 0a 71 01 29 81 71  02 7d 71 03 28 55 0b 74  |ec.q.).q.}q.(U.t|
00000030  72 61 69 6e 5f 63 6f 75  6e 74 71 04 4b 01 55 0b  |rain_countq.K.U.|
00000040  62 61 74 63 68 5f 77 6f  72 64 73 71 05 4d 10 27  |batch_wordsq.M.'|
00000050  55 09 63 62 6f 77 5f 6d  65 61 6e 71 06 4b 01 55  |U.cbow_meanq.K.U|
00000060  06 72 61 6e 64 6f 6d 71  07 63 6e 75 6d 70 79 2e  |.randomq.cnumpy.|
00000070  72 61 6e 64 6f 6d 0a 5f  5f 52 61 6e 64 6f 6d 53  |random.__RandomS|
00000080  74 61 74 65 5f 63 74 6f  72 0a 71 08 29 52 71 09  |tate_ctor.q.)Rq.|
00000090  28 55 07 4d 54 31 39 39  33 37 71 0a 63 6e 75 6d  |(U.MT19937q.cnum|
from gensim.models import Word2Vec

m = Word2Vec.load("DB2Vec_sg_200_5_5_15_4_500", mmap='r')
print(m.wv['dbr:Rodier'])

example above works correctly, no issues here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sairampillai picture sairampillai  路  3Comments

menshikh-iv picture menshikh-iv  路  3Comments

shubhvachher picture shubhvachher  路  4Comments

k0nserv picture k0nserv  路  3Comments

simonm3 picture simonm3  路  3Comments