如何监控Gensim LDA模型的收敛性?

2024-05-15 23:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎找不到它,或者我的统计学知识和它的术语是这里的问题,但我想实现类似于LDA lib from PyPI底页上的图形的东西,并观察线条的一致性/收敛性。如何使用Gensim LDA实现这一点?在


Tags: frompypi图形lib线条一致性术语lda
1条回答
网友
1楼 · 发布于 2024-05-15 23:36:57

您希望绘制模型拟合的收敛性是正确的。 不幸的是,根西姆似乎并没有把这件事说得很直截了当。在

  1. 以能够分析模型拟合函数的输出的方式运行模型。我想设置一个日志文件。在

    import logging
    logging.basicConfig(filename='gensim.log',
                        format="%(asctime)s:%(levelname)s:%(message)s",
                        level=logging.INFO)
    
  2. LdaModel中设置eval_every参数。该值越低,绘图的分辨率就越好。然而,计算这种困惑会减慢你的拟合速度!

    lda_model = 
    LdaModel(corpus=corpus,
             id2word=id2word,
             num_topics=30,
             eval_every=10,
             pass=40,
             iterations=5000)
    
  3. 分析日志文件并绘制绘图。在

    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()
    

相关问题 更多 >