如何监控 Gensim LDA 模型的收敛性?
How to monitor convergence of Gensim LDA model?
我似乎找不到它,或者我的统计知识及其术语可能是这里的问题,但我想实现类似于 LDA lib from PyPI and observe the uniformity/convergence of the lines. How can I achieve this with 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()
我似乎找不到它,或者我的统计知识及其术语可能是这里的问题,但我想实现类似于 LDA lib from PyPI and observe the uniformity/convergence of the lines. How can I achieve this with 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()