gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:06:51 24 4
gpt4 key购买 nike

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

最佳答案

您希望绘制模型拟合的收敛曲线是对的。不幸的是,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()

关于python - 如何监控 Gensim LDA 模型的收敛性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37570696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com