Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用gensim库进行内存有效的LDA训练_Python_Nlp_Gensim_Lda_Topic Modeling - Fatal编程技术网

Python 使用gensim库进行内存有效的LDA训练

Python 使用gensim库进行内存有效的LDA训练,python,nlp,gensim,lda,topic-modeling,Python,Nlp,Gensim,Lda,Topic Modeling,今天我刚开始写一个脚本,使用gensim库在大型语料库(至少3000万句)上训练LDA模型。 以下是我正在使用的当前代码: from gensim import corpora, models, similarities, matutils def train_model(fname): logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) dict

今天我刚开始写一个脚本,使用gensim库在大型语料库(至少3000万句)上训练LDA模型。 以下是我正在使用的当前代码:

from gensim import corpora, models, similarities, matutils

def train_model(fname):
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    dictionary = corpora.Dictionary(line.lower().split() for line in open(fname))
    print "DOC2BOW"
    corpus = [dictionary.doc2bow(line.lower().split()) for line in open(fname)]

    print "running LDA"
    lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=100, update_every=1, chunksize=10000, asses=1)
在一个小语料库(200万个句子)上运行这个脚本,我意识到它需要大约7GB的RAM。 当我尝试在更大的语料库上运行它时,由于内存问题,它失败了。 问题显然是由于我正在使用以下命令加载语料库:

corpus = [dictionary.doc2bow(line.lower().split()) for line in open(fname)]
但是,我认为没有其他方法,因为我需要它来调用LdaModel()方法:

我寻找这个问题的解决办法,但找不到任何有用的。 我认为这应该是一个常见的问题,因为我们主要在非常大的语料库(通常是维基百科文档)上训练模型。因此,它应该已经是一个解决方案


关于这个问题和解决方案有什么想法吗?

考虑将您的
语料库包装为一个iterable,并传递它而不是列表(生成器将无法工作)

发件人:

此外,Gensim还提供了几种不同的语料库格式,可以在中找到。你可以考虑使用<代码>文本语料库,它已经很好地符合你的格式:

corpus = gensim.corpora.TextCorpus(fname)
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, 
                                      id2word=corpus.dictionary, # TextCorpus can build the dictionary for you
                                      num_topics=100,
                                      update_every=1,
                                      chunksize=10000,
                                      passes=1)

相关教程的正确链接是:谢谢,@Carsten!
class MyCorpus(object):
    def __iter__(self):
       for line in open(fname):
            # assume there's one document per line, tokens separated by whitespace
            yield dictionary.doc2bow(line.lower().split())

corpus = MyCorpus()
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, 
                                      id2word=dictionary,
                                      num_topics=100,
                                      update_every=1,
                                      chunksize=10000,
                                      passes=1)
corpus = gensim.corpora.TextCorpus(fname)
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, 
                                      id2word=corpus.dictionary, # TextCorpus can build the dictionary for you
                                      num_topics=100,
                                      update_every=1,
                                      chunksize=10000,
                                      passes=1)