代码之家  ›  专栏  ›  技术社区  ›  V. Déhaye

尝试更新gensim的LdaModel时的索引器错误

  •  3
  • V. Déhaye  · 技术社区  · 7 年前

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

    索引器:索引6614超出大小为6614的轴1的界限

    我查过为什么其他人有这个问题 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\u update参数设置为True。什么都不管用。

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

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

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

    提前谢谢你。

    1 回复  |  直到 7 年前
        1
  •  2
  •   V. Déhaye    7 年前

    解决方案只是用参数初始化LdaModel id2word = dictionary

    如果不这样做,它会假设您的词汇量是您培训它的第一组文档的词汇量,并且无法更新它。事实上 num_terms 值设置为id2word的长度一次 there ,以后再也不会更新它(您可以在 update 函数)。

    推荐文章