【发布时间】: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