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 ...
随机推荐
- swift 8.0之后打开 手机设置
if #available(iOS 8.0, *){ if let url = URL(string: UIApplication.openSettingsURLString), UIApplicat ...
- linux命令学习之:chown
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- 微信小程序 循环列表添加点击事件和样式
如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 <input/> 中的输入内容,<switch/> 的选中状态),需要使 ...
- three.map.control
网址:https://github.com/anvaka/three.map.control 在threejs群里发现的一个很有意思的问题之前没有接触过: 存在的问题: 我在微信小游戏中,用orbi ...
- STL set,mulityset用法
#include<iostream> #include <set> using namespace std; template <class T> class Ru ...
- vs2015未能计算子级
数据源 属性里边值 设置出现问题
- (转)在WCF服务的ServiceReferences.ClientConfig中使用相对路径
问题: Silverlight项目中添加服务引用后会在Silverlight项目中生成一个ServiceReferences.ClientConfig文件,这个文件中包含了引用服务的绑定(bindin ...
- iOS.KVC.setValue:forKey:
Foundation Framework 定义了 NSObject(NSKeyValueCoding), - (void)setValue:(id)value forKey:(NSString *)k ...
- Yii2 数据操作Query Builder查询数据
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- UI设计教程分享:字体变形—阴阳收缩法
阴阳师中国古代对自然规律发展变化基础因素的描述,是古代美学逻辑思维.推理分析的核心要素,也是描述万物基本要素和成因的概念之一.阴阳代表事物的对立关系,它是自然界的客观规律,是万物运动变化的本源,是人类 ...