piskvorky / gensim

Topic Modelling for Humans
https://radimrehurek.com/gensim
GNU Lesser General Public License v2.1
15.67k stars 4.38k forks source link

'Word2VecKeyedVectors' object has no attribute 'vectors' #1994

Open lzfelix opened 6 years ago

lzfelix commented 6 years ago

I have a word2vec model which I was able to load into Gensim, normalize and retrieve word vectors with model.init_sims(replace=True) and model[word] previously. After updating to gensim 3.4.0 I'm receiving the following error messages for these operations:

Traceback (most recent call last):
  File "/Users/lzfelix/anaconda3/envs/mirror/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-13-4a9edd1d9ae5>", line 1, in <module>
    vsm.init_sims()
  File "/Users/lzfelix/anaconda3/envs/mirror/lib/python3.5/site-packages/gensim/models/keyedvectors.py", line 1048, in init_sims
    self.vectors_norm = (self.vectors / sqrt((self.vectors ** 2).sum(-1))[..., newaxis]).astype(REAL)
AttributeError: 'Word2VecKeyedVectors' object has no attribute 'vectors'

and

Traceback (most recent call last):
  File "/Users/lzfelix/anaconda3/envs/mirror/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-14-b955394cb092>", line 1, in <module>
    vsm['oi']
  File "/Users/lzfelix/anaconda3/envs/mirror/lib/python3.5/site-packages/gensim/models/keyedvectors.py", line 169, in __getitem__
    return self.get_vector(entities)
  File "/Users/lzfelix/anaconda3/envs/mirror/lib/python3.5/site-packages/gensim/models/keyedvectors.py", line 277, in get_vector
    return self.word_vec(word)
  File "/Users/lzfelix/anaconda3/envs/mirror/lib/python3.5/site-packages/gensim/models/keyedvectors.py", line 269, in word_vec
    result = self.vectors[self.vocab[word].index]
AttributeError: 'Word2VecKeyedVectors' object has no attribute 'vectors'

This seems related to the issues caused by the vector models re-implementation #1777, similarly to #1882 and #1952. Unfortunately I can't provide the binary file.

Thank you, Luiz Felix

menshikh-iv commented 6 years ago

Hello @lzfelix, can you say, what's gensim version you used before for training/saving of your model?t Also, if possible, please provide code that you used for training/saving.

lzfelix commented 6 years ago

Hi @menshikh-iv, a colleague of mine has trained the model, so I'm not completely sure about the details. I know that it was trained using word2vec C implementation from Google and then I converted it to gensim binary format using gensim 3.1.0.

menshikh-iv commented 6 years ago

@lzfelix thanks for the info

lzfelix commented 6 years ago

@menshikh-iv, sure! Anything that I can help!

menshikh-iv commented 6 years ago

related issue #2000

lzfelix commented 6 years ago

Thanks a lot, @menshikh-iv, I'll start tracking the other issue, which already has a pull request referencing it. Maybe you can close this issue for now just to keep things in one place.

menshikh-iv commented 6 years ago

PR with the fix - #2012, @lzfelix I'll close issues when a fix will be merged to develop.

manneshiva commented 6 years ago

@menshikh-iv This issue may or may not be related to #2012. @lzfelix Can you please outline the steps to reproduce this issue? More specifically, provide the file/similar file that you are trying to load and the code using which you are loading the file. Thanks.

lzfelix commented 6 years ago

Hi @manneshiva, after loading a word2vec binary (details on this later), I get the following error message if I try to pre-compute vectors similarity with model.init_sims(replace=True) or even retrieve a word-vector with model[word]. Namely:

model = KeyedVectors.load(constants.DEFAULT_WORD2VEC_PATH)
model.init_sims(replace=True)   # < error happens here
vector = model['banana']        # < or here if the previous line is not used.

Details on the model: This binary file was obtained from a model trained with Google's word2vec C code and then converted to Gensim format. Since it's being used for a private project, I might only be able to share the model with you through some private mean, ie: Google Drive link via e-mail.

menshikh-iv commented 6 years ago

@lzfelix please sent me a link to ivan@rare-technologies.com (don't worry, we will not share your file).

lzfelix commented 6 years ago

HI @menshikh-iv, thank you. I have just sent the email.

menshikh-iv commented 6 years ago

@lzfelix reproduced, thanks! We'll fix it.

lzfelix commented 6 years ago

Great! Thanks a lot.

rachhitgarg commented 6 years ago

what if i want to update the model loaded with syntax (gensim.models.KeyedVectors.load_word2vec_format) by new sentences I tried : showing error

model.build_vocab(more_sentences, update=True) AttributeError: 'Word2VecKeyedVectors' object has no attribute 'build_vocab'

menshikh-iv commented 6 years ago

@rachhitgarg load_word2vec_format returns only vectors (not full model), you can't continue training, see https://radimrehurek.com/gensim/models/keyedvectors.html

nikita-smetanin commented 5 years ago

Any progress on this bug?

lzfelix commented 5 years ago

Not that I am aware of.