from gensim.test.utils import datapath
from gensim import utils
class MyCorpus(object):
"""An interator that yields sentences (lists of str)."""
def __iter__(self):
corpus_path = datapath('lee_background.cor')
i = 1
print(str(i))
for line in open(corpus_path):
# assume there's one document per line, tokens separated by whitespace
yield utils.simple_preprocess(line)
import gensim.models
sentences = MyCorpus()
model = gensim.models.Word2Vec(sentences=sentences, iter=1)
这是https://radimrehurek.com/gensim/auto_examples/tutorials/run_word2vec.html中的genism的文档代码。
我对iter参数有2个问题:
1设置为1时,为什么两次执行print(str(i))?
2)当“ iter = 10”时,“ simple_preprocess”执行11次。如果我自己定制的“预处理”非常繁琐,这会很慢吗?在使用genism word2vec时如何避免这种预处理重复?
Word2Vec
类需要遍历您的语料库一次才能发现完整的词汇表,然后需要iter
次进行训练。这样您将看到您的语料库可迭代使用iter + 1
次。