LDA程序使用的是JGibbLDA
读theta文件,构造theta二维矩阵。
读phi文件,构造phi二维矩阵。
读tassign文件,得到每篇文本的词数Nd,继而得到测试集的总词数Nsum;并根据tassign文件构造每篇文本所有词组成的词表。
编程实现,根据tassign文件构造测试集每篇文本中所有词组成的词表,根据该词表(Map形式,存储的是每篇文本所有出现的词的词id(词不排重)),得到每一篇文本的词id(index),从而根据此前构造好的theta二维矩阵、phi二维矩阵,分别得到double dt=theta[m][k]、double tw=phi[k][index]。则,一篇文本中第i个词wi的概率为pwi。 一篇文本的所有词的概率为pwd。构造困惑度的分子,即所有文本的词的概率的 log值pwM 。
因此,可以得到LDA模型的困惑度 perp = Math.exp(-pwM / Nsum)。
但是,依据上述编程思想得到的困惑度 perp值,随主题数的增加而增大(主题数设置的是20、40、60、80、100、120),并没有随主题数的增加产生抛物线或者减小。不知道是哪里出了错,望大神指教。
//计算困惑度
public double computePerplexity() throws IOException{
testFileLDAtassignpath="."+File.separator+"testFile"+File.separator+"LDA的文件"+File.separator+"测试文本集LDA格式.txt.model-final.tassign";
testFileLDAthetapath="."+File.separator+"testFile"+File.separator+"LDA的文件"+File.separator+"测试文本集LDA格式.txt.model-final.theta";
testFileLDAphipath="."+File.separator+"testFile"+File.separator+"LDA的文件"+File.separator+"测试文本集LDA格式.txt.model-final.phi";
testFileLDAotherspath="."+File.separator+"testFile"+File.separator+"LDA的文件"+File.separator+"测试文本集LDA格式.txt.model-final.others";
String testFileLDAperppath="."+File.separator+"testFile"+File.separator+"LDA的文件"+File.separator+"测试文本集困惑度.txt";
double perp=0;//困惑度
BufferedReader br=null;
BufferedReader br1=null;
BufferedReader br2=null;
BufferedReader br3=null;
BufferedWriter bw=null;
String line=null;
int Nsum=0; //词数
int K=0; //主题数
int M=0; //文章篇数
int ids=0; //词id个数
HashMap userWords=new HashMap();//每篇文章中出现的所有词的词ID
try {
//得到每篇测试文本的类型
br=new BufferedReader(new FileReader(new File(testFileLDAtassignpath)));
br1=new BufferedReader(new FileReader(new File(testFileLDAthetapath)));
br2=new BufferedReader(new FileReader(new File(testFileLDAphipath)));
br3=new BufferedReader(new FileReader(new File(testFileLDAotherspath)));
//读others文件获取 主题数K,文章篇数M,词ID个数ids
while((line=br3.readLine())!=null){
if(line.contains("ntopics=")){
String[] str=line.split("=");
K=Integer.parseInt(str[1]);
if(line.contains("ndocs=")){
String[] str=line.split("=");
M=Integer.parseInt(str[1]);
if(line.contains("nwords=")){
String[] str=line.split("=");
ids=Integer.parseInt(str[1]);
//读theta文件,构造theta二维矩阵
double theta[][]=new double[M][K];
int i1=0;
while((line=br1.readLine())!=null){
String[] str=line.split(" ");
if(str.length==K){
for(int j1=0;j1<str.length;j1++){
theta[i1][j1]=Double.parseDouble(str[j1]);
else{
throw new Exception("读取的主题数不正确!");
i1++;
//读phi文件,构造phi二维矩阵
double phi[][]=new double[K][ids];
int i2=0;
while((line=br2.readLine())!=null){
String[] str=line.split(" ");
if(str.length==ids){
for(int j2=0;j2<str.length;j2++){
phi[i2][j2]=Double.parseDouble(str[j2]);
else{
throw new Exception("读取的词id个数不正确!");
i2++;
//读tassign文件,得到每篇文本的词数,继而得到测试集的总词数Nsum;以及构造测试集每篇文本中所有词组成的词表
int docNo=0;
while((line=br.readLine())!=null){
String[] str=line.split(" ");
int Nd=str.length; //每篇文本的词数
Nsum=Nsum+Nd; //测试集的总词数Nsum
List<Integer> word_list=new ArrayList<Integer>();
for(int i=0;i<str.length;i++){
String[] word_topic=str[i].split(":");
word_list.add(Integer.parseInt(word_topic[0]));
userWords.put(docNo, word_list);
docNo++;
double pwM=0;
int m = 0;//统计文章篇数
Iterator iterator = userWords.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
ArrayList<Integer> list = (ArrayList<Integer>) entry.getValue();
double pwd = 0;
for(int j = 0; j < list.size(); j++){
double pwi = 0;
int index = list.get(j);
for (int k = 0; k < K; k++){
double tw=phi[k][index];
double dt=theta[m][k];
pwi += tw*dt; //一篇文本中第i个词的概率
pwd += Math.log(pwi);//一篇文本的所有词的概率
pwM += pwd;//构造困惑度的分子,所有文本的词的概率的log值
perp = Math.exp(-pwM / Nsum);
System.out.println("perp:"+ perp );
bw=new BufferedWriter(new FileWriter(new File(testFileLDAperppath)));
bw.write("perp: "+perp);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(br!=null){
br.close();
if(br1!=null){
br1.close();
if(br2!=null){
br2.close();
if(br3!=null){
br3.close();
if(bw!=null){
bw.close();
return perp;
LDA程序使用的是JGibbLDA读theta文件,构造theta二维矩阵。读phi文件,构造phi二维矩阵。读tassign文件,得到每篇文本的词数Nd,继而得到测试集的总词数Nsum;并根据tassign文件构造每篇文本所有词组成的词表。编程实现,根据tassign文件构造测试集每篇文本中所有词组成的词表,根据该词表(Map形式,存储的是每篇文本所有出现的词的词id(词不排重)),得到...
为了
计算
LDA
的
困惑度
,费劲千辛万苦,终于有所收获,以此记录。
本篇文章主要介绍perplexity的
计算
方式,并未涉及过多的
困惑度
原理,想了解更多原理部分,请移步perplexity介绍
本文主要是对Perplexity per word进行
困惑度
计算
,公式:
对于
LDA
模型
,最常用的两个评价方法
困惑度
(Perplexity)、相似度(Corre)。
其中
困惑度
可以理解为对于一篇文章d,所训练出来的
模型
对文档d属于哪个主题有多不确定,这个不确定成都就是
困惑度
。
困惑度
越低,说明聚类的效果越好。
计算
公式 分母是测试集中所有单词之和,即测试集的总长度,不用排重。其中p(w)指的是测试集中每个单词
出现
的概率,
计算
公式...
利用较少的标记数据来进一步利用大规模的无标记数据进行半监督/自监督学习
用teacher
模型
生成伪标签训练studen
模型
,并通过加入噪声使student
模型
由于teacher
模型
,迭代此过程以得到更优的
模型
基于self-training的teacher-student框架
用标记数据训练teacher
模型
用teacher
模型
对大规模的无标记数据生成伪标签
用labelled data和具有伪标签的unlabelled data共同训练student
模型
新的stu
LDA
主题模型
困惑度
Perplexity
计算
导入gensim库
计算
困惑度
perplexity是一种信息理论的测量方法,b的perplexity值定义为基于b的熵的能量(b可以是一个概率分布,或者概率
模型
),通常用于概率
模型
的比较。
该部分内容可参考Perplexity(
困惑度
)、python下进行
lda
主题挖掘(三)——
计算
困惑度
perplexity
可搜索到的资料都通过编程实现了
困惑度
的
计算
,不过gensim库其实自带了perplexity的
计算
模块,稍作修改即可返回
模型
困惑度
。
导入gensim库
lda
topic model需要确定从每篇文章中提取多少个关键词,最简单的就是折肘法+
困惑度
的方法。
补充:还是懒得说背景!以后想起来再补充!还是电脑硬盘坏过,代码忘参考谁的了!原作者发现可以联系我!立马改参考!
LDA
模型
中需要评估的选项一般是主题数量,而主题数量需要根据具体任务进行调整,即通过评估不同主题数
模型
的
困惑度
来选择最优的
模型
主题数。本课题中,通过
计算
困惑度
perplexity来衡量主题数量:
其中,M是测试
在 Machine Learning 中,
LDA
是两个常用
模型
的简称: Linear Discriminant Analysis 和 Latent Dirichlet Allocation,
在这篇文章中我们主要八卦的是后者。
LDA
是一个在文本建模中很著名的
模型
,类似于 SVD, PLSA 等
模型
, 可以用于浅层语义分析,在文本语义分析中是一个很有用的
模型
。很不幸的是,这个
模型
中涉及的数学知识有点多,
包括 Gamma 函数, Dirichlet 分布, Dirichlet-Multinomial 共轭, Gibbs Sampling,
Variational Inference, 贝叶斯文本建模,PLSA 建模, 以及
LDA
文本建模。
这篇文章的主要目标,就是科普在学习理解
LDA
模型
中,需要了解的一些重要的数学知识。
预设的读者是做自然语言处理、机器学习、数据挖掘方向的工程师,
要读懂这篇科普,需要的数学基础知识基本上不超过陈希孺先生的《概率论与数理统计》这本书。
你好,关于
计算
LDA
主题模型
的
困惑度
,我可以回答你。在Python中,可以使用gensim库来
计算
LDA
主题模型
的
困惑度
。具体实现方法可以参考以下代码:
```python
from gensim.models.
lda
model import
Lda
Model
from gensim.models.coherencemodel import CoherenceModel
from gensim.corpora.dictionary import Dictionary
# 假设已经有了文本集合corpus和词典dictionary
# 假设
LDA
模型
的主题数为num_topics
# 训练
LDA
模型
lda
_model =
Lda
Model(corpus=corpus, id2word=dictionary, num_topics=num_topics)
#
计算
困惑度
coherence_model_
lda
= CoherenceModel(model=
lda
_model, texts=corpus, dictionary=dictionary, coherence='perplexity')
perplexity = coherence_model_
lda
.get_coherence()
print("
LDA
主题模型
的
困惑度
为:", perplexity)
希望这个回答能够帮到你。