主题模型:具有对数似然或困惑的交叉验证

 mobiledu2502886833 发布于 2023-01-31 13:29

我正在使用主题建模来集群文档.我需要提出最佳的主题数字.因此,我决定使用主题10,20,... 60进行十倍交叉验证.

我已将我的语料库分成十批,并留出一批用于保留集.我使用9个批次(总共180个文档)运行潜在的dirichlet分配(LDA),主题为10到60.现在,我必须计算保持集的困惑或记录可能性.

我从CV的一个讨论会上找到了这段代码.我真的不明白下面的几行代码.我有dtm矩阵使用holdout set(20个文档).但我不知道如何计算这个坚持集的困惑或记录可能性.


问题:

    任何人都可以向我解释seq(2,100,by = 1)在这里意味着什么?那么,AssociatedPress [21:30]是什么意思?这里有什么功能(k)?

    best.model <- lapply(seq(2, 100, by=1), function(k){ LDA(AssociatedPress[21:30,], k) })
    

    如果我想计算称为dtm的保持集的困惑或记录可能性,是否有更好的代码?我知道有perplexity()logLik()函数但是因为我是新手我无法弄清楚如何使用我的保持矩阵(称为dtm)来实现它.

    如何使用包含200个文档的语料库进行十倍交叉验证?是否存在我可以调用的现有代码?我找到caret了这个目的,但也无法弄明白.

Ben.. 22

我在CV上写了你所提到的答案,这里有一些细节:

    seq(2, 100, by =1)只需创建一个从2到100的数字序列,所以2,3,4,5,... 100.这些是我想在模型中使用的主题数.一个模型有2个主题,另一个有3个主题,另一个有4个主题,等等100个主题.

    AssociatedPress[21:30]只是topicmodels包中内置数据的一个子集.我只是在该示例中使用了一个子集,以便它运行得更快.

关于最佳主题数的一般问题,我现在按照Martin Ponweiser关于谐波均值模型选择的例子(他的论文中的4.3.3,在这里:http://epub.wu.ac.at/3558/1) /main.pdf).我现在就是这样做的:

library(topicmodels)
#
# get some of the example data that's bundled with the package
#
data("AssociatedPress", package = "topicmodels")

harmonicMean <- function(logLikelihoods, precision=2000L) {
  library("Rmpfr")
  llMed <- median(logLikelihoods)
  as.double(llMed - log(mean(exp(-mpfr(logLikelihoods,
                                       prec = precision) + llMed))))
}

# The log-likelihood values are then determined by first fitting the model using for example
k = 20
burnin = 1000
iter = 1000
keep = 50

fitted <- LDA(AssociatedPress[21:30,], k = k, method = "Gibbs",control = list(burnin = burnin, iter = iter, keep = keep) )

# where keep indicates that every keep iteration the log-likelihood is evaluated and stored. This returns all log-likelihood values including burnin, i.e., these need to be omitted before calculating the harmonic mean:

logLiks <- fitted@logLiks[-c(1:(burnin/keep))]

# assuming that burnin is a multiple of keep and

 harmonicMean(logLiks)

所以在一系列具有不同主题数量的主题模型上执行此操作...

# generate numerous topic models with different numbers of topics
sequ <- seq(2, 50, 1) # in this case a sequence of numbers from 1 to 50, by ones.
fitted_many <- lapply(sequ, function(k) LDA(AssociatedPress[21:30,], k = k, method = "Gibbs",control = list(burnin = burnin, iter = iter, keep = keep) ))

# extract logliks from each topic
logLiks_many <- lapply(fitted_many, function(L)  L@logLiks[-c(1:(burnin/keep))])

# compute harmonic means
hm_many <- sapply(logLiks_many, function(h) harmonicMean(h))

# inspect
plot(sequ, hm_many, type = "l")

# compute optimum number of topics
sequ[which.max(hm_many)]
## 6

在此输入图像描述 这是输出,沿着x轴有多个主题,表明6个主题是最佳的.

主题模型的交叉验证在包中附带的文档中有详细记录,请参见此处:http://cran.r-project.org/web/packages/topicmodels/vignettes/topicmodels.pdf给出一个尝试然后再回答一个关于使用主题模型编写CV的更具体的问题.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有