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 ...
随机推荐
- 使用Python3.x抓取58同城(南京站)的演出票的信息
#!/usr/bin/env python #-*-coding: utf-8 -*- import re import urllib.request as request from bs4 impo ...
- 半吊子的STM32 — IIC通信
半双工通信模式:以字节模式发送(8位): 两线式串行总线,SDA(数据信号)和SCL(时钟信号)两条信号线都为高电平时,总线为空闲状态:起始时,SCL稳定为高电平,SDA电平由高向低跳变:停止时,SC ...
- 13.Mysql触发器
13.触发器13.1 创建触发器定义:触发器是与表有关的数据库对象,在满足定义条件时触发,并执行触发器中定义的语句集合.语法:create trigger 触发器名称 触发时机 触发事件 on 表名 ...
- istio分布式调用链Jaeger
1.安装 kubectl apply -n istio-system -f https://raw.githubusercontent.com/jaegertracing/jaeger-kuberne ...
- PAT 1036 跟奥巴马一起编程(15)(代码)
1036 跟奥巴马一起编程(15)(15 分) 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝"计算机科学教育周& ...
- Java VisualVM 插件地址
打开Java VisualVM检查更新插件时,默认的连接连不上,通过浏览器访问之后发现默认的服务器已经404,新地址已经迁移到github,下面这个地址里面有不同版本jdk对应的插件中心地址. htt ...
- LibreOJ #6007. 「网络流 24 题」方格取数 最小割 最大点权独立集 最大流
#6007. 「网络流 24 题」方格取数 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- LibreOJ 2003. 「SDOI2017」新生舞会 基础01分数规划 最大权匹配
#2003. 「SDOI2017」新生舞会 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- dbc file
DBC文件是用来描述CAN网络通信信号的一种格式文件.它可以用来监测与分析CAN网络上的报文数据,也可以用来模拟某个CAN节点.(DBC file is a format file used to d ...
- How to return AJAX errors from Laravel Controller?
Questions: I am building a REST API with Laravel 5. In Laravel 5, you can subclassApp\Http\Requests\ ...