Latent Dirichlet Allocation(LDA)主题模型算法实现及源码解析
来源:互联网 发布:excel中数据透视表 编辑:程序博客网 时间:2024/05/29 17:36
Latent Dirichlet Allocation(LDA)主题模型算法实现及源码解析
变量说明:
代码解析
1 读入文档: int len; //文档长度 int *id; //单词id数组 double *cnt;//各单词出现次数数组 fprintf(stderr, "lda:: cannot open training data.\n"); exit(1); int nclass, int nlex //单词列表长度, int dlenmax//文档最大长度, int emmax //最大迭代次数, int demmax //inference迭代次数, double epsilon //收敛阈值 alpha[i] = RANDOM; z += alpha[i]; alpha[i] = alpha[i] / z; (int (*)(const void *, const void *))doublecmp); for (j = 0; j < nclass; j++) beta[i][j] = (double) 1 / nlex; fprintf(stderr, "lda_learn:: cannot allocate gammas.\n"); return; fprintf(stderr, "lda_learn:: cannot allocate betas.\n"); return; fprintf(stderr, "lda_learn:: cannot allocate q.\n"); return; fprintf(stderr, "lda_learn:: cannot allocate gamma.\n"); return; fprintf(stderr, "lda_learn:: cannot allocate ap.\n"); return; fprintf(stderr, "lda_learn:: cannot allocate nt.\n"); return; fprintf(stderr, "lda_learn:: cannot allocate pnt.\n"); return; printf("iteration %d/%d..\t", t + 1, emmax); fflush(stdout); //VB-E step for (dp = data, i = 0; (dp->len) != -1; dp++, i++) { vbem(dp, gamma, q, nt, pnt, ap,alpha, (const double **)beta, dp->len, nclass, demmax); accum_gammas(gammas, gamma, i, nclass); accum_betas(betas, q, nclass, dp); } //VB-M step //对beta的估计实际上就是规范化的一个过程,而对alpha的估计是使用牛顿迭代法的一个过程 newton_alpha(alpha, gammas, n, nclass, 0); //规范化矩阵的列,也就是求列的和,然后每个元素除以该和 normalize_matrix_col(beta, betas, nlex, nclass); lik = lda_lik(data, beta, gammas, n, nclass);
整个程序步骤如下图
代码解析
1 读入文档:
首先要读入语料库中的文档。每个文件行,开头是一个数字,代表有多少单词,接着是id:count的形式,id代表单词的id号,count代表文档中这个单词出现的次数。读入的文档集合保存在document结构体中。
typedef struct {
} document;
main()函数中打开文件:
if ((data = feature_matrix(argv[optind], &nlex, &dlenmax)) == NULL) {
}
2 调用lda_learn开始学习
void lda_learn (document *data, double *alpha, double **beta,
{ …… }
1)首先随机规范初始化alpha,规范初始化beta.规范化也就是使得行或列之和为1
for (i = 0; i < nclass; i++)
for (i = 0, z = 0; i < nclass; i++)
for (i = 0; i < nclass; i++)
qsort(alpha, nclass, sizeof(double), // 为alpha排序
//初始化beta
for (i = 0; i < nlex; i++)
2)初始化充分统计量,variational inference中需要用到的变量等
gammas和betas相当于gamma和beta的和,gamma和beta代表的是某一个文档,而gammas和betas代表的是所有文档
if ((gammas = dmatrix(n, nclass)) == NULL) {
}
if ((betas = dmatrix(nlex, nclass)) == NULL) {
}
//initialize buffers
if ((q = dmatrix(dlenmax, nclass)) == NULL) {
}
if ((gamma = (double *)calloc(nclass, sizeof(double))) == NULL)
{
}
if ((ap = (double *)calloc(nclass, sizeof(double))) == NULL) {
}
if ((nt = (double *)calloc(nclass, sizeof(double))) == NULL) {
}
if ((pnt = (double*)calloc(nclass, sizeof(double))) == NULL) {
}
3)开始EM迭代,如果迭代次数超过设定值则跳出
for (t = 0; t < emmax; t++)
{
//需要把得到的gamma和phi(变量q)的值累加到gammas和betas中,因为gamma和beta代表的只是其中一个文档,而gammas和betas代表的是所有文档,用来对alpha和beta进行参数估计
4)计算似然函数,判断是否收敛
参考来源:
http://blog.sina.com.cn/s/blog_8eee7fb60101d06p.html
- Latent Dirichlet Allocation(LDA)主题模型算法实现及源码解析
- Latent Dirichlet Allocation(LDA)主题模型算法实现及源码解析
- LDA(Latent Dirichlet Allocation)主题模型算法
- LDA(Latent Dirichlet Allocation)主题模型算法
- LDA(Latent Dirichlet Allocation)主题模型算法
- LDA(Latent Dirichlet Allocation)主题模型算法
- LDA(Latent Dirichlet Allocation)主题模型算法
- 一个LDA(Latent Dirichlet Allocation)主题模型的Java实现
- Latent Dirichlet Allocation(LDA)主题模型理论
- Latent Dirichlet Allocation(LDA)主题模型理论
- LDA(Latent Dirichlet Allocation)主题模型
- Latent Dirichlet Allocation 主题模型算法
- LDA 主题模型(latent dirichlet allocation) 介绍
- Introduction to Latent Dirichlet Allocation (LDA主题模型入门)
- latent Dirichlet allocation (LDA)
- Latent Dirichlet Allocation(LDA)
- Latent Dirichlet Allocation (LDA)
- Latent Dirichlet Allocation (LDA)
- memcpy内存复制函数的使用
- iOS应用程序状态切换相关
- 编译遇到的错误
- Ubuntu下用egg安装networkX
- overloadByRet
- Latent Dirichlet Allocation(LDA)主题模型算法实现及源码解析
- ABAP按照销售办事处或业务员将每月总销售额写入自建表
- 2013年1月15工作感悟
- ios程序出错的一些解决办法(持续更新)
- HTTP 协议详解
- setjmp和longjmp的使用
- 约瑟夫环问题
- android 使用vcard示例
- viewdidLoad viewillappear