Reputation: 49
I am using Word2Vec and using a wiki trained model that gives out the most similar words. I ran this before and it worked but now it gives me this error even after rerunning the whole program. I tried to take off return_path=True
but im still getting the same error
print(api.load('glove-wiki-gigaword-50', return_path=True))
model.most_similar("glass")
#ERROR:
/Users/me/gensim-data/glove-wiki-gigaword-50/glove-wiki-gigaword-50.gz
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-153-3bf32168d154> in <module>
1 print(api.load('glove-wiki-gigaword-50', return_path=True))
----> 2 model.most_similar("glass")
AttributeError: 'Word2Vec' object has no attribute 'most_similar'
#MODEL this is the model I used
print(
'%s (%d records): %s' % (
model_name,
model_data.get('num_records', -1),
model_data['description'][:40] + '...',
)
)
Edit: here is my gensim download & output
!python -m pip install -U gensim
OUTPUT:
Requirement already satisfied: gensim in ./opt/anaconda3/lib/python3.8/site-packages (4.0.1)
Requirement already satisfied: numpy>=1.11.3 in ./opt/anaconda3/lib/python3.8/site-packages (from gensim) (1.20.1)
Requirement already satisfied: smart-open>=1.8.1 in ./opt/anaconda3/lib/python3.8/site-packages (from gensim) (5.1.0)
Requirement already satisfied: scipy>=0.18.1 in ./opt/anaconda3/lib/python3.8/site-packages (from gensim) (1.6.2)
Upvotes: 5
Views: 17497
Reputation: 54243
Your shown code...
print(api.load('glove-wiki-gigaword-50', return_path=True))
model.most_similar("glass")
...doesn't assign anything into model
. (Was it assigned earlier?)
And, using return_path=True
there means the api.load()
will only return a string path to the datafile. That'd only be interesting if you were going to use that string to then do your own loading of the data into a model.
That api.load()
call without return_path=True
likely returns an instance of KeyedVectors
, which is a set of vectors. That's different from a full Word2Vec
model, but would still support a .most_similar()
method. However, if you're just print()
ing that returned path, or returned model, it's not going to be in the model
variable for your later .most_similar()
operation.
So you may want:
kv_model = api.load('glove-wiki-gigaword-50')
similars = kv_model.most_similar('glass')
print(similars)
(Personally, I don't like the opaque magic, & running of new downloaded code, that api.load()
does. I think it's a better habit to download the raw data files yourself, from a known source, so that you know what files have arrived, to which directories, on your own machine. Then use a dataset-specific load method to load that data, so that you learn what library methods work with which kinds of files.)
If your model
variable does in fact include a full Word2Vec
model, from some unshown other code, then it will also contain a set of vectors in its .wv
(for word-vectors) property:
similars = model.wv.most_similar('glass')
print(similars)
Upvotes: 0
Reputation: 16738
You are probably looking for <MODEL>.wv.most_similar
, so please try:
model.wv.most_similar("glass")
Upvotes: 18