我似乎找不到它,或者我的统计知识及其术语可能是这里的问题,但我想实现类似于 LDA lib from PyPI 底部页面上的图表的东西。并观察线条的均匀性/收敛性。如何使用 Gensim LDA 实现此目的?
最佳答案
您希望绘制模型拟合的收敛曲线是对的。 不幸的是,Gensim 似乎并没有使这一点变得非常直接。
以能够分析模型拟合函数输出的方式运行模型。我喜欢设置日志文件。
import logging logging.basicConfig(filename='gensim.log', format="%(asctime)s:%(levelname)s:%(message)s", level=logging.INFO)
在
LdaModel
中设置eval_every
参数。该值越低,您的绘图的分辨率就越高。但是,计算困惑度会大大降低拟合速度!lda_model = LdaModel(corpus=corpus, id2word=id2word, num_topics=30, eval_every=10, pass=40, iterations=5000)
解析日志文件并制作您的绘图。
import re import matplotlib.pyplot as plt p = re.compile("(-*\d+\.\d+) per-word .* (\d+\.\d+) perplexity") matches = [p.findall(l) for l in open('gensim.log')] matches = [m for m in matches if len(m) > 0] tuples = [t[0] for t in matches] perplexity = [float(t[1]) for t in tuples] liklihood = [float(t[0]) for t in tuples] iter = list(range(0,len(tuples)*10,10)) plt.plot(iter,liklihood,c="black") plt.ylabel("log liklihood") plt.xlabel("iteration") plt.title("Topic Model Convergence") plt.grid() plt.savefig("convergence_liklihood.pdf") plt.close()
关于python - 如何监控 Gensim LDA 模型的收敛性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37570696/