Word2Vec Problem

2,010 views
Skip to first unread message

sandip sarkar

unread,
Jun 22, 2016, 4:49:12 AM6/22/16
to gensim
I have install gensim from this link


Corpora and Vectors are working well. But When I am working with word2vec then it gives this type of error 

model = Word2Vec(sentences, size=100, window=5, min_count=5, workers=4)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Word2Vec' is not defined


But when I am  executing this command 

>>> # import modules and set up logging
>>> from gensim.models import word2vec
>>> import logging
>>> logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
>>> # load up unzipped corpus from http://mattmahoney.net/dc/text8.zip
>>> sentences = word2vec.Text8Corpus('/tmp/text8')
>>> # train the skip-gram model; default window=5
>>> model = word2vec.Word2Vec(sentences, size=200)
>>> # ... and some hours later... just as advertised...
>>> model.most_similar(positive=['woman', 'king'], negative=['man'], topn=1)
[('queen', 0.5359965)]

then it gives correct ans.


How to resolve NameError: name 'Word2Vec' is not defined Problem

Gordon Mohr

unread,
Jun 22, 2016, 10:32:26 AM6/22/16
to gensim
Where your code is working, you first...

    from gensim.models import word2vec

...then use that imported name `word2vec` to reach the `Word2Vec` class, as `word2vec.Word2Vec`. (Just using `Word2Vec` wouldn't work – that unqualified name hasn't been imported.) 

In your code that doesn't work, you haven't shown what imports are in effect. You try to name the `Word2Vec` class directly. Was `Word2Vec` ever imported?

- Gordon

Mona Jalal

unread,
Oct 4, 2016, 5:48:25 PM10/4/16
to gensim
Hi Gordon,

I followed your suggestion but I get this error. Do you know why?

jalal@klein:~/computer_vision/image_retrieval$ python  gensim_test.py

Traceback (most recent call last):
  File "gensim_test.py", line 3, in <module>
    model = word2vec.Word2Vec(sentences)
NameError: name 'sentences' is not defined

Mona Jalal

unread,
Oct 4, 2016, 6:24:17 PM10/4/16
to gen...@googlegroups.com
fixed the problem by downloading and extracting the text8

sentences = word2vec.Text8Corpus("/home/jalal/text8")
model = word2vec.Word2Vec(sentences)

--
You received this message because you are subscribed to a topic in the Google Groups "gensim" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gensim/qIEVM9KJ_wM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gensim+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages