【问题标题】:How to monitor convergence of Gensim LDA model?如何监控 Gensim LDA 模型的收敛性?
【发布时间】:2016-09-30 22:35:57
【问题描述】:

我似乎找不到它,或者我的统计知识及其术语可能是这里的问题,但我想实现类似于LDA lib from PyPI 底部页面上的图表并观察一致性/收敛性的行。如何使用Gensim LDA 实现这一目标?

【问题讨论】:

    标签: python lda gensim convergence


    【解决方案1】:

    您希望绘制模型拟合的收敛性是对的。 不幸的是,Gensim 似乎并没有把这件事做得很直截了当。

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

    【讨论】:

    • 绘图是否有助于确定通过次数?传递和迭代之间有什么区别?谢谢!
    • @VictorWang 也许这会有所帮助:“passes 控制我们在整个语料库上训练模型的频率。passes 的另一个词可能是“epochs”。迭代有点技术性,但本质上它控制着我们多久训练一次。在每个文档上重复一个特定的循环。将“通过”和“迭代”的数量设置得足够高“参考:radimrehurek.com/gensim/auto_examples/tutorials/run_lda.html
    猜你喜欢
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2023-04-06
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多