1. /*
  2. * Copyright (C) 2007 by
  3. *
  4. * Xuan-Hieu Phan
  5. * hieuxuan@ecei.tohoku.ac.jp or pxhieu@gmail.com
  6. * Graduate School of Information Sciences
  7. * Tohoku University
  8. *
  9. * Cam-Tu Nguyen
  10. * ncamtu@gmail.com
  11. * College of Technology
  12. * Vietnam National University, Hanoi
  13. *
  14. * JGibbsLDA is a free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published
  16. * by the Free Software Foundation; either version 2 of the License,
  17. * or (at your option) any later version.
  18. *
  19. * JGibbsLDA is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with JGibbsLDA; if not, write to the Free Software Foundation,
  26. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  27. */
  28.  
  29. package jgibblda;
  30.  
  31. import java.io.File;
  32. import java.util.Vector;
  33.  
  34. public class Estimator {
  35.  
  36. // output model
  37. protected Model trnModel;
  38. LDACmdOption option;
  39.  
  40. public boolean init(LDACmdOption option){
  41. this.option = option;
  42. trnModel = new Model();
  43.  
  44. if (option.est){
  45. if (!trnModel.initNewModel(option))
  46. return false;
  47. trnModel.data.localDict.writeWordMap(option.dir + File.separator + option.wordMapFileName);
  48. }
  49. else if (option.estc){
  50. if (!trnModel.initEstimatedModel(option))
  51. return false;
  52. }
  53.  
  54. return true;
  55. }
  56.  
  57. public void estimate(){
  58. System.out.println("Sampling " + trnModel.niters + " iteration!");
  59.  
  60. int lastIter = trnModel.liter;
  61. for (trnModel.liter = lastIter + 1; trnModel.liter < trnModel.niters + lastIter; trnModel.liter++){
  62. System.out.println("Iteration " + trnModel.liter + " ...");
  63.  
  64. // for all z_i
  65. for (int m = 0; m < trnModel.M; m++){
  66. for (int n = 0; n < trnModel.data.docs[m].length; n++){
  67. // z_i = z[m][n]
  68. // sample from p(z_i|z_-i, w)
  69. int topic = sampling(m, n);
  70. trnModel.z[m].set(n, topic);
  71. }// end for each word
  72. }// end for each document
  73.  
  74. if (option.savestep > 0){
  75. if (trnModel.liter % option.savestep == 0){
  76. System.out.println("Saving the model at iteration " + trnModel.liter + " ...");
  77. computeTheta();
  78. computePhi();
  79. trnModel.saveModel("model-" + Conversion.ZeroPad(trnModel.liter, 5));
  80. }
  81. }
  82. }// end iterations
  83.  
  84. System.out.println("Gibbs sampling completed!\n");
  85. System.out.println("Saving the final model!\n");
  86. computeTheta();
  87. computePhi();
  88. trnModel.liter--;
  89. trnModel.saveModel("model-final");
  90. }
  91.  
  92. /**
  93. * Do sampling
  94. * @param m document number
  95. * @param n word number
  96. * @return topic id
  97. */
  98. public int sampling(int m, int n){
  99. // remove z_i from the count variable
  100. int topic = trnModel.z[m].get(n);
  101. int w = trnModel.data.docs[m].words[n];
  102.  
  103. trnModel.nw[w][topic] -= 1;
  104. trnModel.nd[m][topic] -= 1;
  105. trnModel.nwsum[topic] -= 1;
  106. trnModel.ndsum[m] -= 1;
  107.  
  108. double Vbeta = trnModel.V * trnModel.beta;
  109. double Kalpha = trnModel.K * trnModel.alpha;
  110.  
  111. //do multinominal sampling via cumulative method
  112. for (int k = 0; k < trnModel.K; k++){
  113. trnModel.p[k] = (trnModel.nw[w][k] + trnModel.beta)/(trnModel.nwsum[k] + Vbeta) *
  114. (trnModel.nd[m][k] + trnModel.alpha)/(trnModel.ndsum[m] + Kalpha);
  115. }
  116.  
  117. // cumulate multinomial parameters
  118. for (int k = 1; k < trnModel.K; k++){
  119. trnModel.p[k] += trnModel.p[k - 1];
  120. }
  121.  
  122. // scaled sample because of unnormalized p[]
  123. double u = Math.random() * trnModel.p[trnModel.K - 1];
  124.  
  125. for (topic = 0; topic < trnModel.K; topic++){
  126. if (trnModel.p[topic] > u) //sample topic w.r.t distribution p
  127. break;
  128. }
  129.  
  130. // add newly estimated z_i to count variables
  131. trnModel.nw[w][topic] += 1;
  132. trnModel.nd[m][topic] += 1;
  133. trnModel.nwsum[topic] += 1;
  134. trnModel.ndsum[m] += 1;
  135.  
  136. return topic;
  137. }
  138.  
  139. public void computeTheta(){
  140. for (int m = 0; m < trnModel.M; m++){
  141. for (int k = 0; k < trnModel.K; k++){
  142. trnModel.theta[m][k] = (trnModel.nd[m][k] + trnModel.alpha) / (trnModel.ndsum[m] + trnModel.K * trnModel.alpha);
  143. }
  144. }
  145. }
  146.  
  147. public void computePhi(){
  148. for (int k = 0; k < trnModel.K; k++){
  149. for (int w = 0; w < trnModel.V; w++){
  150. trnModel.phi[k][w] = (trnModel.nw[w][k] + trnModel.beta) / (trnModel.nwsum[k] + trnModel.V * trnModel.beta);
  151. }
  152. }
  153. }
  154. }

Gibs抽样的更多相关文章

  1. MCMC 、抽样算法与软件实现

    一.MCMC 简介 1. Monte Carlo 蒙特卡洛 蒙特卡洛方法(Monte Carlo)是一种通过特定分布下的随机数(或伪随机数)进行模拟的方法.典型的例子有蒲丰投针.定积分计算等等,其基础 ...

  2. 《BI那点儿事》数据流转换——百分比抽样、行抽样

    百分比抽样和行抽样可以从数据源中随机选择一组数据.这两种task都可以产生两组输出,一组是随机选择的,另一组是没有被选择的.可以将这些选择出的数据发送到开发或者测试服务器上.这个Task的最合适的应用 ...

  3. [hive小技巧]使用limit查询变成抽样,而不是全盘扫描

    将set hive.limit.optimize.enable=true 时,limit限制数据时就不会全盘扫,而是根据限制的数量进行抽样. 同时还有两个配置项需要注意: 1.hive.limit.r ...

  4. alias sample method——运行时间复杂度为O(1)的抽样算法

    根据离散离散概率分布抽样是一个常见的问题.这篇文章将介绍运行时间复杂度为O(1)的 alias method 抽样算法思想. 下面举例说明: 比如 a,b,c,d 的概率分别为 0.1,0.2,0.3 ...

  5. Reservoir Sampling - 蓄水池抽样

    问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...

  6. [大牛翻译系列]Hadoop(7)MapReduce:抽样(Sampling)

    4.3 抽样(Sampling) 用基于MapReduce的程序来处理TB级的数据集,要花费的时间可能是数以小时计.仅仅是优化代码是很难达到良好的效果. 在开发和调试代码的时候,没有必要处理整个数据集 ...

  7. 二、MLlib统计指标之关联/抽样/汇总

    汇总统计[Summary statistics]: Summary statistics提供了基于列的统计信息,包括6个统计量:均值.方差.非零统计量个数.总数.最小值.最大值. import org ...

  8. 蓄水池抽样(原理&实现)

    前言: 蓄水池抽样:从N个元素中随机的等概率的抽取k个元素,其中N无法确定. 适用场景: 模式识别等概率抽样,抽样查看渐增的log日志(无法先保存整个数据流然后再从中选取,而是期望有一种将数据流遍历一 ...

  9. top-N 抽样

        1, 使用hive标记random:(如果是mr,就自己标记random值) use ps; set mapred.job.priority=VERY_HIGH; set mapred.job ...

随机推荐

  1. SQL创建删除索引

    --创建唯一聚集索引create unique clustered index pk_table1 on table1 (column1) --创建唯一非聚集索引create unique noncl ...

  2. 管道分隔符Split

    string[] areaID = area1Id.Split(new char[] { ',' });

  3. swift - 快速代码块 - 创建 tableview等一些控件 基本属性

    1.创建tableview private lazy var cellId = "cellId" fileprivate lazy var tv : UITableView = { ...

  4. webpack(二) 根据模板生成简单的html文件

    (一)使用webpack 根据模板生成HTML,首先需要安装插件 html-webpack-plugin. 在工程文件夹安装插件 命令如下: npm install html-webpack-plug ...

  5. Java_7.1 ArrayList应用点名器

    1.ArrayList同样可以添加自定义的类 将学生类添加到ArrayList集合中,其中学生类包括学生姓名,年龄 自定义学生类 package demo1; public class Student ...

  6. log4j 产生的日志位置设置和catalina.home、catalina.base

    方法一. 解决的办法自然是用相对路径代替绝对路径,其实log4j的FileAppender本身就有这样的机制,如:log4j.appender.logfile.File=${WORKDIR}/logs ...

  7. js计算日期增加

    <div class="time"> <i class="visa_icon prev"></i><span id=& ...

  8. 操作系统的发展史 day36

    什么是操作系统       可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...

  9. Vs2015 c# 诊断工具查看程序的占用情况

    windbg用着还不熟悉,dottrace  还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...

  10. Oracle_PL/SQL(5) 包

    包1.定义:包用于逻辑组合相关的PL/SQL类型,项和子程序,由包规范和包体组成 建立包规范:包规范是包与应用程序之间的接口,用于定义包的公用组件, 包括常量,变量,游标,过程,函数等 建立包体:用于 ...