Discussion:
[gensim:10910] error - model is not defined
Sharan Basappa
2018-04-14 06:54:14 UTC
Permalink
I am trying out a simple example to compare sentences available on
stackoverflow.

I get the following error when I run this:

= RESTART: D:\my_data\backup3\backup3\tech\ML\programs\cosine_comparison.py
=
Warning (from warnings module):
File
"D:\Users\sharanb\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gensim\utils.py",
line 1197
warnings.warn("detected Windows; aliasing chunkize to chunkize_serial")
UserWarning: detected Windows; aliasing chunkize to chunkize_serial
Traceback (most recent call last):
File "D:\my_data\backup3\backup3\tech\ML\programs\cosine_comparison.py",
line 9, in <module>
index2word_set = set(model.wv.index2word)
NameError: name 'model' is not defined
The following is the program I am trying to run:

# Imports
import numpy as np
from scipy import spatial
import gensim
from gensim import models
from gensim.models import Word2Vec
index2word_set = set(model.wv.index2word)
def avg_feature_vector(sentence, model, num_features, index2word_set):
words = sentence.split()
feature_vec = np.zeros((num_features,),dtype='float32')
n_words = 0
for word in words:
if word in index2word_set:
n_words += 1
feature_vec = np.add(feature_vec,model[word])
if (n_words > 0):
feature_vec = np.divide(feature_vec,n_words)
return feature_vec
s1_afv = avg_feature_vector('this is a
sentence',model=model,num_features=300,index2word_set=index2word_set)
s2_afv = avg_feature_vector('this is a
sentence',model=model,num_features=300,index2word_set=index2word_set)
sim = 1-spatial.distance.cosine(s1_afv,s2_afv)
print(sim)

Any guidance is highly appreciated ...
--
You received this message because you are subscribed to the Google Groups "gensim" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gensim+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ivan Menshikh
2018-04-16 01:39:03 UTC
Permalink
Firstly, you should train Word2Vec model here (this part is missing here),
the trained model should be a "model" variable here.
Post by Sharan Basappa
I am trying out a simple example to compare sentences available on
stackoverflow.
D:\my_data\backup3\backup3\tech\ML\programs\cosine_comparison.py =
File
"D:\Users\sharanb\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gensim\utils.py",
line 1197
warnings.warn("detected Windows; aliasing chunkize to chunkize_serial")
UserWarning: detected Windows; aliasing chunkize to chunkize_serial
File "D:\my_data\backup3\backup3\tech\ML\programs\cosine_comparison.py",
line 9, in <module>
index2word_set = set(model.wv.index2word)
NameError: name 'model' is not defined
# Imports
import numpy as np
from scipy import spatial
import gensim
from gensim import models
from gensim.models import Word2Vec
index2word_set = set(model.wv.index2word)
words = sentence.split()
feature_vec = np.zeros((num_features,),dtype='float32')
n_words = 0
n_words += 1
feature_vec = np.add(feature_vec,model[word])
feature_vec = np.divide(feature_vec,n_words)
return feature_vec
s1_afv = avg_feature_vector('this is a
sentence',model=model,num_features=300,index2word_set=index2word_set)
s2_afv = avg_feature_vector('this is a
sentence',model=model,num_features=300,index2word_set=index2word_set)
sim = 1-spatial.distance.cosine(s1_afv,s2_afv)
print(sim)
Any guidance is highly appreciated ...
--
You received this message because you are subscribed to the Google Groups "gensim" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gensim+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...