【问题标题】:Sagemaker LDA topic model - how to access the params of the trained model? Also is there a simple way to capture coherenceSagemaker LDA 主题模型 - 如何访问训练模型的参数?还有一种简单的方法来捕捉连贯性
【发布时间】:2020-02-17 18:35:04
【问题描述】:

我是 Sagemaker 的新手,我正在运行一些测试来衡量 AWS 上的 NTM 和 LDA 与 LDA mallet 和原生 Gensim LDA 模型相比的性能。

我想在 Sagemaker 上检查经过训练的模型,并查看诸如哪些词对每个主题的贡献最大之类的内容。并且还可以衡量模型的一致性。

通过下载解压缩输出文件并解压缩以公开 3 个文件 params、symbol.json 和 meta.json,我已经能够成功地获得对 Sagemaker 上 NTM 的每个主题的贡献最大的单词。

但是,当我尝试对 LDA 执行相同的过程时,无法解压缩解压缩的输出文件。

与 NTM 相比,我可能遗漏了一些东西,或者应该为 LDA 做一些不同的事情,但我找不到任何关于此的文档。另外,有人找到了计算模型一致性的简单方法吗?

任何帮助将不胜感激!

【问题讨论】:

    标签: python lda amazon-sagemaker


    【解决方案1】:

    This SageMaker notebook 深入探讨了 LDA 的科学细节,还演示了如何检查模型工件。具体来说,如何获得狄利克雷先验alpha和主题词分布矩阵beta的估计。您可以在标题为“检查训练模型”的部分中找到说明。为方便起见,我将相关代码复制在这里:

    import tarfile
    import mxnet as mx
    
    # extract the tarball
    tarflie_fname = FILENAME_PREFIX + 'model.tar.gz' # wherever the tarball is located
    with tarfile.open(tarfile_fname) as tar:
        tar.extractall()
    
    # obtain the model file (should be the only file starting with "model_")
    model_list = [
        fname
        for fname in os.listdir(FILENAME_PREFIX)
        if fname.startswith('model_')
    ]
    model_fname = model_list[0]
    
    # load the contents of the model file into MXNet arrays
    alpha, beta = mx.ndarray.load(model_fname)
    

    这应该可以为您提供模型数据。请注意,存储为 beta 行的主题不按任何特定顺序显示。

    【讨论】:

    • 感谢这个作品,很痛苦:虽然是测试版,这使得对主题的解释更加困难
    • 出于兴趣,您从哪里读到 beta 行没有任何特定顺序?
    • 这是一篇关于如何从文本文档中提取具有代表性的关键字的论文(我敢肯定其中之一)。 (arxiv.org/pdf/1308.2359.pdf) 或许可以用来解决主题标注问题?
    • 我现在更好地理解了你的问题。矩阵beta 的每一行都是一个主题:词汇表上的概率分布。第 0 个条目是 word_0 在该主题中出现的概率,第一个条目是 word_1 在该主题中出现的概率,依此类推。如果您 argsort 该行/主题中的概率,您可以在词汇表中找到在该主题中代表最多。具体来说,argsort 为您提供单词的索引,然后您将使用您选择的嵌入来查找这些索引。 (例如“猫”-> word_0、“狗”-> word_1、“鸟”-> word_2、...)
    • 我不相信它们是在训练过程中计算出来的,所以你必须设置一个推理端点。或者,我相信您可以通过使用 SageMaker batch transform 来节省启动第二个实例的工作,但我自己没有尝试过。
    【解决方案2】:

    关于连贯性,sagemaker AFAIK 中没有默认实现。

    您可以像这样实现自己的指标:

    from itertools import combinations
    from sklearn.metrics.pairwise import cosine_similarity
    
    def calculate_coherence(topic_vectors):
        similarity_sum = 0.0
        num_combinations = 0
        for pair in combinations(topic_vectors, 2):
            similarity = cosine_similarity([pair[0]], [pair[1]])
            similarity_sum = similarity_sum + similarity
            num_combinations = num_combinations + 1
        return float(similarity_sum / num_combinations)
    

    并获得真实模型的连贯性,例如:

    print(calculate_coherence(beta.asnumpy()))
    

    一些直观的连贯性测试如下:

    predictions = [[0.0, 0.0, 0.0],
                   [0.0, 1.0, 0.0],
                   [0.0, 0.0, 1.0],
                   [1.0, 0.0, 0.0]]
    
    assert calculate_coherence(predictions) == 0.0, "Expected incoherent"
    
    predictions = [[0.0, 1.0, 1.0],
                   [0.0, 1.0, 1.0],
                   [0.0, 1.0, 1.0],
                   [0.0, 1.0, 1.0]]
    
    assert calculate_coherence(predictions) == 1.0, "Expected coherent"
    
    predictions = [[0.0, 0.0, 1.0],
                   [0.0, 0.0, 1.0],
                   [1.0, 0.0, 0.0],
                   [1.0, 0.0, 0.0],
                   [0.0, 1.0, 0.0],
                   [0.0, 1.0, 0.0]]
    assert calculate_coherence(predictions) == 0.2, "Expected partially coherent"
    

    延伸阅读:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2022-06-27
      • 2012-06-25
      • 2023-02-20
      • 1970-01-01
      相关资源
      最近更新 更多