2
votes

I want to use gensim to train a word2vec model
python 3.5.3
gensim 2.1.0
numpy 1.12.1+mkl
scipy 0.19.0

import gensim
import codecs
class MySentences(object):
    def __init__(self,filename):
        self.filename=filename
    def __iter__(self):
        with codecs.open(self.filename) as f:
            for line in f.readlines():
                wordlist=list()
                for word in line:
                    wordlist.append(word)
                yield wordlist

sentences=MySentences('D:/Documents/Data/icwb2-data-processed/pku_training.rmspace.utf8')
model=gensim.models.Word2Vec(sentences)
model.save('w.model')

I run this code, and i cause the error:

AttributeError: module 'gensim' has no attribute 'models'

I make this error due to i named this file 'gensim.py'
thank @BurhanKhalid !!!

2
Try from gensim.models import Word2Vec, then replace model=gensim.models.Word2Vec(sentences) with model=Word2Vec(sentences) does it work then? - Lomtrur
Don't save your file as gensim.py. - Burhan Khalid
@BurhanKhalid you are right!thank you! - nomadlx

2 Answers

2
votes

In python, it may cause an error if your code module's name writing by yourself is the same as library module name.

So you shouldn't name your training file as "gensim.py" which would override the gensim library file: "gensim.py"

1
votes

You have to import models from gensim.

https://radimrehurek.com/gensim/