Gensim: AttributeError: 'Word2Vec' object has no attribute 'most_similar'

Created on 19 Jan 2021  Â·  5Comments  Â·  Source: RaRe-Technologies/gensim

Problem description

When I was trying to use a trained word2vec model to find the similar word, it showed that 'Word2Vec' object has no attribute 'most_similar'.

I haven't seen that what are changed of the 'most_similar' attribute from gensim 4.0. When I was using the gensim in Earlier versions, most_similar() can be used as:

model_hasTrain=word2vec.Word2Vec.load(saveBinPath)
y=model_hasTrain.most_similar('price',topn=100)

I don't know that are most_similar() removed or changed? Or do I need to reinstall the gensim? Thank you for solving my problem. Thanks very much.

Versions

Please provide the output of:

Windows-2012Server-6.2.9200-SP0
Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]
Bits 64
NumPy 1.18.5
SciPy 1.5.0
gensim 4.0.0beta
FAST_VERSION 1
bug documentation

All 5 comments

most_similar() is now a part of KeyedVectors:

model_hasTrain = word2vec.Word2Vec.load(saveBinPath)
y = model_hasTrain.wv.most_similar('price', topn=100)  # <=== note the .wv

@gojomo looking at the migration guide, I see no mention of most_similar. This is a change we introduced in 4.0.0, right?

Thank you very much! Now it is working! Thanks!

@gojomo looking at the migration guide, I see no mention of most_similar. This is a change we introduced in 4.0.0, right?

@piskvorky Yes - prior to 4.0, using most_similar on the containing model already generated a "Method will be removed in 4.0.0, use self.wv.most_similar() instead" deprecation warning:

https://github.com/RaRe-Technologies/gensim/blob/e889fa3d45a406cabbc7e180fa9a8ee3f76ac6f0/gensim/models/base_any2vec.py#L1375-L1380

It was removed per that warning before 4.0.0beta - but yes, it should also be mentioned in the migration guide, so I'll add a note about this & related methods there.

Thanks. I also extended the docstrings in word2vec to make this point clearer – will push as part of the final clean-up work (event hooks, model versioning etc).

See added subsection at: https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4#7-methods-like-most_similar-wmdistance-doesnt_match-similarity--others-should-now-be-called-only-on-sets-of-word-vectors-not-containing-word2vecdoc2vecfasttext-full-models

Was this page helpful?
5 / 5 - 1 ratings

Related issues

franciscojavierarceo picture franciscojavierarceo  Â·  3Comments

dancinghui picture dancinghui  Â·  4Comments

mmunozm picture mmunozm  Â·  3Comments

sairampillai picture sairampillai  Â·  3Comments

jeradf picture jeradf  Â·  4Comments