【问题标题】:IndexError when trying to update gensim's LdaModel尝试更新 gensim 的 LdaModel 时出现 IndexError
【发布时间】:2018-10-17 07:13:13
【问题描述】:

我在尝试更新我的 gensim 的 LdaModel 时遇到以下错误:

IndexError:索引 6614 超出轴 1 的范围,大小为 6614

我检查了为什么其他人在 this thread 上遇到了这个问题,但我从头到尾都使用同一个字典,这是他们的错误。

由于我有一个大数据集,我正在逐块加载它(使用 pickle.load)。由于这段代码,我以这种方式迭代地构建字典:

 

 fr_documents_lda = open("documents_lda_40_rails_30_ruby_full.dat", 'rb')
 dictionary = Dictionary()
 chunk_no = 0
 while 1:
     try:
         t0 = time()
         documents_lda = pickle.load(fr_documents_lda)
         chunk_no += 1
         dictionary.add_documents(documents_lda)
         t1 = time()
         print("Chunk number {0} took {1:.2f}s".format(chunk_no, t1-t0))
     except EOFError:
         print("Finished going through pickle")
         break

为整个数据集构建后,我以相同的方式迭代训练模型:

fr_documents_lda = open("documents_lda_40_rails_30_ruby_full.dat", 'rb')
first_iter = True
chunk_no = 0
lda_gensim = None
while 1:
    try:
        t0 = time()
        documents_lda = pickle.load(fr_documents_lda) 
        chunk_no += 1
        corpus = [dictionary.doc2bow(text) for text in documents_lda]
        if first_iter:
            first_iter = False
            lda_gensim = LdaModel(corpus, num_topics=no_topics, iterations=100, offset=50., random_state=0, alpha='auto')
        else:
            lda_gensim.update(corpus)
        t1 = time()
        print("Chunk number {0} took {1:.2f}s".format(chunk_no, t1-t0))
    except EOFError:
        print("Finished going through pickle")
        break

我还尝试在每个块更新字典,即

dictionary.add_documents(documents_lda)

就在之前

corpus = [dictionary.doc2bow(text) for text in documents_lda]

在最后一段代码中。最后,我尝试将 doc2bow 的 allow_update 参数设置为 True。没有任何效果。

仅供参考,我最终字典的大小是 85k。仅从第一个块构建的字典的大小为 10k。错误发生在第二次迭代中,当它传入 else 条件时,调用 update 方法时。

错误是由expElogbetad = self.expElogbeta[:, ids] 行引发的 ,由gamma, sstats = self.inference(chunk, collect_sstats=True)调用,自身由gammat = self.do_estep(chunk, other)调用,自身由lda_gensim.update(corpus)调用。

有没有人知道如何解决这个问题,或者发生了什么?

提前谢谢你。

【问题讨论】:

    标签: python-3.x gensim lda topic-modeling index-error


    【解决方案1】:

    解决方案是简单地使用参数id2word = dictionary 初始化 LdaModel。

    如果您不这样做,它会假定您的词汇量是您训练它的第一组文档的词汇量,并且无法更新它。实际上,它会将其num_terms 值设置为id2word 的长度一次there,之后就不再更新它(您可以在update 函数中验证)。

    【讨论】:

    • 我遇到了与 OP 相同的问题 - 我迭代地构建了一个字典,然后尝试更新我的模型,该模型是用 id2word = dictionary 初始化的。这个问题有新的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2019-07-12
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多