LDA-如何归一化并将平滑常数“添加到原始文档主题分配计数?

问题描述 投票:1回答:1

上下文

谢谢您的帮助。现在,我已经通过Jonathan Chang的“ lda”包(注:这与“ topicmodels”包不同)中的LDA函数运行了数据集。下面是一个可复制的示例,它使用安装和加载“ lda”软件包时自动提供的cora数据集。

library(lda)
data(cora.documents) #list of words contained in each of the 2,410 documents
data(cora.vocab) #vocabulary list of words that occur at least once across all documents

此后,我通过设置不同的参数并运行代码来进行实际的LDA。

#parameters for LDA algorithm
K <- 20 #number of topics to be modelled from the corpus, "K"
G <- 1000 #number of iterations to cover - the higher this number, the more likely the data converges, "G"
alpha <- 0.1 #document-topic distributions, "alpha"
beta <- 0.1 #topic-term distributions, "beta/eta"

#creates an LDA fit based on above parameters
lda_fit <- lda.collapsed.gibbs.sampler(cora.documents, cora.vocab, K = 20, 
                                       num.iterations = G, alpha, beta) 

以下,我们检查LDA模型输出的一个组成部分,称为document_sums。该组件显示分配给20个主题中的每个主题的每个文档包含的单词数(基于我选择的K值)。例如,一个文档可能有分配给主题3的4个单词,分配给主题19的12个单词,在这种情况下该文档已分配给主题19

#gives raw figures for no. of times each document (column) had words allocated to each of 20 topics (rows)
document_sums <- as.data.frame(lda_fit$document_sums)
document_sums[1:20, 1:20] 

问题

但是,我想做的基本上是使用模糊隶属度的原理。我不想将每个文档分配给包含最多单词的主题,而是要提取每个文档分配给每个主题的概率。 document_sums与此非常接近,但是我仍然必须对原始数据进行一些处理。

lda“程序包的创建者乔纳森·张本人在this thread中这样说:

n.b。如果要将矩阵转换为概率,只需对行进行归一化并添加先前的平滑常数即可。这里的函数仅返回上一次Gibbs采样扫描中的原始分配数量。 ()

另外,另一个论坛上的another reply再次确认:

结果document_sums将为您提供测试文档主题的(非规范化)分布。对它们进行归一化,然后计算内部乘积,并乘以RTM系数加权以获得预测的链接概率(或使用预测性链接概率)

因此,我的问题是,如何规范我的[[document_sums和'添加平滑常数'?这些我不确定。

r lda
1个回答
0
投票
根据要求:您需要在计数矩阵前添加先验,然后将每行除以总数。例如

theta <- document_sums + alpha theta <- theta / rowSums(theta)

您需要对将单词与主题相关的计数矩阵做类似的事情。

但是,如果您使用的是LDA,我是否建议您签出textmineR?它为您完成了归一化(以及其他有用的操作)。我最初将其写为“ lda”包的包装器,但此后实现了我自己的Gibbs采样器以启用其他功能。在主题建模中使用它的详细信息在third vignette

© www.soinside.com 2019 - 2024. All rights reserved.