Search code examples
pythonmachine-learningartificial-intelligencegensim

KeyedVectors\' object has no attribute \'wv for gensim 4.1.2


i have migrated from gensim 3.8.3 to 4.1.2 and i am using this

claim = [token for token in claim_text if token in w2v_model.wv.vocab]

reference = [token for token in ref_text if token in w2v_model.wv.vocab]

i am not sure how to replace w2v_model.wv.vocab to newer attribute and i am getting this error

KeyedVectors' object has no attribute 'wv' can anyone please help.


Solution

  • You only use the .wv property to fetch the KeyedVectors object from another more complete algorithmic model, like a full Word2Vec model (which contains a KeyedVectors in its .wv attribute).

    If you're already working with just-the-vectors, there's no need to request the word-vectors subcomponent. Whatever you were going to do, you just do to the KeyedVectors directly.

    However, you're also using the .vocab attribute, which has been replaced. See the migration FAQ for more details:

    https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4#4-vocab-dict-became-key_to_index-for-looking-up-a-keys-integer-index-or-get_vecattr-and-set_vecattr-for-other-per-key-attributes

    (Mainly: instead of doing an in w2v_model.wv.vocab, you may only need to do in kv_model or in kv_model.key_to_index.)