Gibs抽样
- /*
- * Copyright (C) 2007 by
- *
- * Xuan-Hieu Phan
- * hieuxuan@ecei.tohoku.ac.jp or pxhieu@gmail.com
- * Graduate School of Information Sciences
- * Tohoku University
- *
- * Cam-Tu Nguyen
- * ncamtu@gmail.com
- * College of Technology
- * Vietnam National University, Hanoi
- *
- * JGibbsLDA is a free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation; either version 2 of the License,
- * or (at your option) any later version.
- *
- * JGibbsLDA is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with JGibbsLDA; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
- */
- package jgibblda;
- import java.io.File;
- import java.util.Vector;
- public class Estimator {
- // output model
- protected Model trnModel;
- LDACmdOption option;
- public boolean init(LDACmdOption option){
- this.option = option;
- trnModel = new Model();
- if (option.est){
- if (!trnModel.initNewModel(option))
- return false;
- trnModel.data.localDict.writeWordMap(option.dir + File.separator + option.wordMapFileName);
- }
- else if (option.estc){
- if (!trnModel.initEstimatedModel(option))
- return false;
- }
- return true;
- }
- public void estimate(){
- System.out.println("Sampling " + trnModel.niters + " iteration!");
- int lastIter = trnModel.liter;
- for (trnModel.liter = lastIter + 1; trnModel.liter < trnModel.niters + lastIter; trnModel.liter++){
- System.out.println("Iteration " + trnModel.liter + " ...");
- // for all z_i
- for (int m = 0; m < trnModel.M; m++){
- for (int n = 0; n < trnModel.data.docs[m].length; n++){
- // z_i = z[m][n]
- // sample from p(z_i|z_-i, w)
- int topic = sampling(m, n);
- trnModel.z[m].set(n, topic);
- }// end for each word
- }// end for each document
- if (option.savestep > 0){
- if (trnModel.liter % option.savestep == 0){
- System.out.println("Saving the model at iteration " + trnModel.liter + " ...");
- computeTheta();
- computePhi();
- trnModel.saveModel("model-" + Conversion.ZeroPad(trnModel.liter, 5));
- }
- }
- }// end iterations
- System.out.println("Gibbs sampling completed!\n");
- System.out.println("Saving the final model!\n");
- computeTheta();
- computePhi();
- trnModel.liter--;
- trnModel.saveModel("model-final");
- }
- /**
- * Do sampling
- * @param m document number
- * @param n word number
- * @return topic id
- */
- public int sampling(int m, int n){
- // remove z_i from the count variable
- int topic = trnModel.z[m].get(n);
- int w = trnModel.data.docs[m].words[n];
- trnModel.nw[w][topic] -= 1;
- trnModel.nd[m][topic] -= 1;
- trnModel.nwsum[topic] -= 1;
- trnModel.ndsum[m] -= 1;
- double Vbeta = trnModel.V * trnModel.beta;
- double Kalpha = trnModel.K * trnModel.alpha;
- //do multinominal sampling via cumulative method
- for (int k = 0; k < trnModel.K; k++){
- trnModel.p[k] = (trnModel.nw[w][k] + trnModel.beta)/(trnModel.nwsum[k] + Vbeta) *
- (trnModel.nd[m][k] + trnModel.alpha)/(trnModel.ndsum[m] + Kalpha);
- }
- // cumulate multinomial parameters
- for (int k = 1; k < trnModel.K; k++){
- trnModel.p[k] += trnModel.p[k - 1];
- }
- // scaled sample because of unnormalized p[]
- double u = Math.random() * trnModel.p[trnModel.K - 1];
- for (topic = 0; topic < trnModel.K; topic++){
- if (trnModel.p[topic] > u) //sample topic w.r.t distribution p
- break;
- }
- // add newly estimated z_i to count variables
- trnModel.nw[w][topic] += 1;
- trnModel.nd[m][topic] += 1;
- trnModel.nwsum[topic] += 1;
- trnModel.ndsum[m] += 1;
- return topic;
- }
- public void computeTheta(){
- for (int m = 0; m < trnModel.M; m++){
- for (int k = 0; k < trnModel.K; k++){
- trnModel.theta[m][k] = (trnModel.nd[m][k] + trnModel.alpha) / (trnModel.ndsum[m] + trnModel.K * trnModel.alpha);
- }
- }
- }
- public void computePhi(){
- for (int k = 0; k < trnModel.K; k++){
- for (int w = 0; w < trnModel.V; w++){
- trnModel.phi[k][w] = (trnModel.nw[w][k] + trnModel.beta) / (trnModel.nwsum[k] + trnModel.V * trnModel.beta);
- }
- }
- }
- }
Gibs抽样的更多相关文章
- MCMC 、抽样算法与软件实现
一.MCMC 简介 1. Monte Carlo 蒙特卡洛 蒙特卡洛方法(Monte Carlo)是一种通过特定分布下的随机数(或伪随机数)进行模拟的方法.典型的例子有蒲丰投针.定积分计算等等,其基础 ...
- 《BI那点儿事》数据流转换——百分比抽样、行抽样
百分比抽样和行抽样可以从数据源中随机选择一组数据.这两种task都可以产生两组输出,一组是随机选择的,另一组是没有被选择的.可以将这些选择出的数据发送到开发或者测试服务器上.这个Task的最合适的应用 ...
- [hive小技巧]使用limit查询变成抽样,而不是全盘扫描
将set hive.limit.optimize.enable=true 时,limit限制数据时就不会全盘扫,而是根据限制的数量进行抽样. 同时还有两个配置项需要注意: 1.hive.limit.r ...
- alias sample method——运行时间复杂度为O(1)的抽样算法
根据离散离散概率分布抽样是一个常见的问题.这篇文章将介绍运行时间复杂度为O(1)的 alias method 抽样算法思想. 下面举例说明: 比如 a,b,c,d 的概率分别为 0.1,0.2,0.3 ...
- Reservoir Sampling - 蓄水池抽样
问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...
- [大牛翻译系列]Hadoop(7)MapReduce:抽样(Sampling)
4.3 抽样(Sampling) 用基于MapReduce的程序来处理TB级的数据集,要花费的时间可能是数以小时计.仅仅是优化代码是很难达到良好的效果. 在开发和调试代码的时候,没有必要处理整个数据集 ...
- 二、MLlib统计指标之关联/抽样/汇总
汇总统计[Summary statistics]: Summary statistics提供了基于列的统计信息,包括6个统计量:均值.方差.非零统计量个数.总数.最小值.最大值. import org ...
- 蓄水池抽样(原理&实现)
前言: 蓄水池抽样:从N个元素中随机的等概率的抽取k个元素,其中N无法确定. 适用场景: 模式识别等概率抽样,抽样查看渐增的log日志(无法先保存整个数据流然后再从中选取,而是期望有一种将数据流遍历一 ...
- top-N 抽样
1, 使用hive标记random:(如果是mr,就自己标记random值) use ps; set mapred.job.priority=VERY_HIGH; set mapred.job ...
随机推荐
- SQL创建删除索引
--创建唯一聚集索引create unique clustered index pk_table1 on table1 (column1) --创建唯一非聚集索引create unique noncl ...
- 管道分隔符Split
string[] areaID = area1Id.Split(new char[] { ',' });
- swift - 快速代码块 - 创建 tableview等一些控件 基本属性
1.创建tableview private lazy var cellId = "cellId" fileprivate lazy var tv : UITableView = { ...
- webpack(二) 根据模板生成简单的html文件
(一)使用webpack 根据模板生成HTML,首先需要安装插件 html-webpack-plugin. 在工程文件夹安装插件 命令如下: npm install html-webpack-plug ...
- Java_7.1 ArrayList应用点名器
1.ArrayList同样可以添加自定义的类 将学生类添加到ArrayList集合中,其中学生类包括学生姓名,年龄 自定义学生类 package demo1; public class Student ...
- log4j 产生的日志位置设置和catalina.home、catalina.base
方法一. 解决的办法自然是用相对路径代替绝对路径,其实log4j的FileAppender本身就有这样的机制,如:log4j.appender.logfile.File=${WORKDIR}/logs ...
- js计算日期增加
<div class="time"> <i class="visa_icon prev"></i><span id=& ...
- 操作系统的发展史 day36
什么是操作系统 可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...
- Vs2015 c# 诊断工具查看程序的占用情况
windbg用着还不熟悉,dottrace 还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...
- Oracle_PL/SQL(5) 包
包1.定义:包用于逻辑组合相关的PL/SQL类型,项和子程序,由包规范和包体组成 建立包规范:包规范是包与应用程序之间的接口,用于定义包的公用组件, 包括常量,变量,游标,过程,函数等 建立包体:用于 ...