随机采样和随机模拟:吉布斯采样Gibbs Sampling实现文档分类
http://blog.csdn.net/pipisorry/article/details/51525308
吉布斯采样的实现问题
本文主要说明如何通过吉布斯采样进行文档分类(聚类),当然更复杂的实现可以看看吉布斯采样是如何采样LDA主题分布的[主题模型TopicModel:隐含狄利克雷分布LDA]。
关于吉布斯采样的介绍文章都停止在吉布斯采样的详细描述上,如随机采样和随机模拟:吉布斯采样Gibbs Sampling(why)但并没有说明吉布斯采样到底如何实现的(how)?
也就是具体怎么实现从下面这个公式采样?
怎么在模型中处理连续参数问题?
怎么生成最终我们感兴趣的公式的期望值,而不是仅仅做T次随机游走?
下面介绍如何为朴素贝叶斯Na ̈ıve Bayes[概率图模型:贝叶斯网络与朴素贝叶斯网络]构建一个吉布斯采样器,其中包含两大问题:如何利用共轭先验?如何通过等式14的条件中进行实际的概率采样?
朴素贝叶斯模型的吉布斯采样器
问题
基于朴素贝叶斯框架,通过吉布斯采样对文档进行(无监督和有监督)分类。假设features是文档下的词,我们要预测的是doc-level的文档分类标签(sentiment label),值为0或1。
首先在无监督数据上进行朴素贝叶斯的采样,对于监督数据的简化会在后面说明。
Following Pedersen [T. Pedersen. Knowledge lean word sense disambiguation. In AAAI/IAAI, page 814, 1997., T. Pedersen. Learning Probabilistic Models of Word Sense Disambiguation. PhD thesis, Southern Methodist University, 1998. http://arxiv.org/abs/0707.3972.],
we’re going to describe the Gibbs sampler in a completely unsupervised setting where no labels at all are provided as training data.
模型表示
朴素贝叶斯模型对应的plate-diagram:

Figure4-1
变量代表的意思如下表:
1.每一个文档有一个Label(j),是文档的class,同时θ0和θ1是和Lj对应的,如果Lj=1则对应的就是θ1
3.在这个model中,Gibbs Sampling所谓的P(Z),就是产生图中这整个数据集的联合概率,也就是产生这N个文档整体联合概率,还要算上包括超参γ产生具体π和 θ的概率。所以最后得到了上图中表达式与对应色彩。
给定文档,我们要选择文档的label L使下面的概率越大:
文档生成过程
文档j的label生成
文档j的词袋生成描述
先验Priors
π来自哪里?
hyperparameters : parameters of a prior, which is itself used to pick parameters of the model.
Our generative story is going to assume that before this whole process began, we also picked π randomly. Specifically we’ll assume that π is sampled from a Beta distribution with parameters γ π1 and γ π0 .
In Figure 4 we represent these two hyperparameters as a single two-dimensional vector γ π = γ π1 , γ π0 . When γ π1 = γ π0 = 1, Beta(γ π1 , γ π0 ) is just a uniform distribution, which means that any value for π is equally likely. For this reason we call
Beta(1, 1) an “uninformed prior”.
θ 0 和 θ 1来自哪里?
Let γ θ be a V -dimensional vector where the value of every dimension equals 1. If θ0 is sampled from Dirichlet(γθ ), every probability distribution over words will be equally likely. Similarly, we’ll assume θ 1 is sampled from Dirichlet(γ θ ).
Note: θ0为label为0的文档中词的概率分布;θ1为label为1的文档中词的概率分布。θ0and θ1are sampled separately. There’s no assumption that they are related to each other at all.
2.3 模型的状态空间和初始化
状态空间
朴素贝叶斯模型中状态空间的变量定义
• one scalar-valued variable π (文档j的label为1的概率)
• two vector-valued variables, θ 0 and θ 1
• binary label variables L, one for each of the N documents
We also have one vector variable W j for each of the N documents, but these are observed variables, i.e.their values are already known (and which is why W jk is shaded in Figure 4).
初始化
Pick a value π by sampling from the Beta(γ π1 , γ π0 ) distribution. sample出文档j的label为1的概率,也就知道了文档j的label的bernoulli概率分布(π, 1-π)。
Then, for each j, flip a coin with success probability π, and assign label L j(0)— that is, the label of document j at the 0 th iteration – based on the outcome of the coin flip. 通过上步得到的bernoulli分布sample出文档的label。
Similarly,you also need to initialize θ 0 and θ 1 by sampling from Dirichlet(γ θ ).
派生联合分布
for each iteration t = 1 . . . T of sampling, we update every variable defining the state space by sampling from its conditional distribution given the other variables, as described in equation (14).
处理过程:
• We will define the joint distribution of all the variables, corresponding to the numerator in (14).
• We simplify our expression for the joint distribution.
• We use our final expression of the joint distribution to define how to sample from the conditional distribution in (14).
• We give the final form of the sampler as pseudocode.
联合分布的表示和简化
模型对于整个文档集的联合分布为
Note: 分号右边是联合分布的参数,也就是说分号左边的变量是被右边的超参数条件住的。
联合分布可分解为(通过图模型):
因子1:
也即Figure4-1红色部分:这个是从beta分布sample出一个伯努利分布,伯努利分布只有一个参数就是π,不要normalization项(要求的是整个联合概率,所以在这里纠结normalization是没有用的),得到:
因子2:
也即Figure4-1绿色部分:这里L是一整个向量,其中值为0的有C0个,值为1的有C1个,多次伯努利分布就是二项分布啦,因此:
因子3:
词的分布概率,也即Figure4-1蓝色部分:对于0类和1类的两个θ都采样自参数为γθ的狄利克雷分布,注意所有这些都是向量,有V维,每一维度对应一个Word。根据狄利克雷的PDF得到以下表达式,其实这个表达式有两个,分别为θ0和θ1用相同的式子采样:
因子4:
P (C 0 |θ 0 , L) and P (C 1 |θ 1 , L): the probabilities of generating the contents of the bags of words in each of the two document classes.
也即Figure4-1紫色部分:首先要求对于单独一个文档n,产生所有word也就是Wn的概率。假设对于某个文档,θ=(0.2,0.5,0.3),意思就是word1产生概率0.2,word2产生概率0.5,假如这个文档里word1有2个,word2有3个,word3有2个,则这个文档的产生概率就是(0.2*0.2)*(0.5*0.5*0.5)*(0.3*0.3)。所以按照这个道理,一个文档整个联合概率如下:
let θ = θ L n:
Wni: W n中词i的频数。
文档间相互独立,同一个class中的文档合并,上面这个概率是针对单个文档而言的,把所有文档的这些概率乘起来,就得到了Figure4-1紫色部分:
Note: 其中x的取值可以是0或1,所以Cx可以是C0或C1,当x=0时,n∈Cx的意思就是对于所有class 0中的文档,然后对于这些文档中每一个word i,word i在该文档中出现Wni次,求θ0,i的Wni次方,所有这些乘起来就是紫色部分。后式27是规约后得到的结果,NCx (i) :word i在 documents with class label x中的计数,如NC0(i)的意思就是word i出现在calss为0的所有文档中的总数,同理对于NC1(i)。
联合分布的表示和先验选择的原因
使用式19和21:
使用式24和25:
如果使用所有文档的词(也就是使用式24和27)
可知后验分布式30是一个unnormalized Beta distribution, with parameters C 1 + γ π1 and C 0 + γ π0 ,且式32是一个unnormalized Dirichlet distribution, with parameter vector N C x (i) + γ θi for 1 ≤ i ≤ V .
也就是说先验和后验分布是一种形式,这样Beta distribution是binomial (and Bernoulli)分布的共轭先验,Dirichlet分布是多项式multinomial分布的共轭先验。
而超参数就如观察到的证据,是一个伪计数pseudocounts。
让,整个文档集的联合分布表示为:
将隐含变量π积出
why: 为了方便,可以对隐含变量π进行积分,最后达到消去这个变量的目的。我们可以通过积分掉π来减少模型有效的参数个数。This has the effect of taking all possible values of π into account in our sampler, without representing it as a variable explicitly and having to sample it at every iteration. Intuitively, “integrating
out” a variable is an application of precisely the same principle as computing the marginal probability for a discrete distribution.As a result, c is “there” conceptually, in terms of our understanding of the model, but we don’t need to deal with manipulating
it explicitly as a parameter.
Note: 积分掉的意思就是
于是联合分布的边缘分布为:
只考虑积分项:
而38式后面的积分项是一个参数为C 1 + γ π1 and C 0 + γ π0的beta分布,且Beta(C 1 + γ π1 , C 0 + γ π0 )的积分为
让N = C 0 + C 1
则38式表示为:
整个文档集的联合分布表示(三因子式)为:
其中,N = C 0 + C 1
构建吉布斯采样器Gibbs Sampler
吉布斯采样就是通过条件概率给Zi一个新值
如要计算,需要计算条件分布
Note: There’s no superscript on the bags of words C because they’re fully observed and don’t change from iteration to iteration.
要计算θ 0,需要计算条件分布
直觉上,在每个迭代t开始前,我们有如下当前信息:
每篇文档的词计数,标签为0的文档计数,标签为1的文档计数,每篇文档的当前label,θ0 和 θ1的当前值等等。
采样准则
采样label:When we want to sample the new label for document j, we temporarily remove all information (i.e. word counts and label information) about this document from that collection of information. Then we look at the conditional probability that L j = 0
given all the remaining information, and the conditional probability that L j = 1 given the same information, and we sample the new label L j (t+1) by choosing randomly according to the relative weight of those two conditional probabilities.
采样θ:Sampling to get the new values operates according
to the same principal.
2.5.1 文档标签的采样
定义条件概率
L (−j) are all the document labels except L j , and C (−j) is the set of all documents except W j .
分子是全联合概率分布,分母是除去Wj信息的相同的表达式,所以我们需要考虑的只是式40的3个因子。
其实我们要做的只是考虑除去Wj后,改变了什么。
因子1
由于因子1仅依赖于超参数,分子分母一样,不予考虑,故只考虑式40中的因子2和因子3。
因子2
式42因子2分母的计算与上一次迭代Lj是多少有关。
不过语料大小总是从N变成了N-1,且其中一个文档类别的计数减少1。如Lj=0,则,Cx只有一个有变化,这样
let x be the class for which C x(−j)= C x − 1,式42的因子2重写为:
又Γ(a + 1) = aΓ(a) for all a
这样式42的因子2简化为:
因子3
同因子2,总有某个class对应的项没变,也就是式42的因子3中θ 0 or θ 1有一项在分子和分母中是一样的。
合并
for x ∈ {0, 1},最终合并得到采样文档label的的条件分布为
从式49看文档的label是如何选择出来的:
式49因子1:L j = x considering only the distribution of the other labels
式49因子2:is like a word distribution “fitting room.”, an indication of how well the words in W j “fit” with each of the two distributions.
前半部分其实只有Cx是变量,所以如果C0大,则P(L(t+1)j=0)的概率就会大一点,所以下一次Lj的值就会倾向于C0,反之就会倾向于C1。而后半部分,是在判断当前θ参数的情况下,整个文档的likelihood更倾向于C0还是C1。
式42的条件分布采样过程如下
Note: 步骤3是对两个label的概率分布进行归一化。
对于监督数据
Using labeled documents just don’t sample L j for those documents! Always keep L j equal to the observed label.
The documents will effectively serve as “ground truth” evidence for the distributions that created them. Since we never sample for their labels, they will always contribute to the counts in (49) and (51) and will never be subtracted out.
2.5.2 θ的采样
由于θ 0 and θ 1的分布估计是独立的,这里我们先消去θ下标。
显然
since we used conjugate priors, this posterior, like the prior, works out to be a Dirichlet distribution. We actually derived the full expression , but we don’t need the full expression here. All we need to do to sample a new distribution is to make another
draw from a Dirichlet distribution, but this time with parameters N C x (i) + γ θi for each i in V .
define the V dimensional vector t such that each (这里i下标代表V维向量t的第i个元素):
new θ的采样公式
从Dirichlet分布采样的实现
sample a random vector a = <a 1 , . . . , a V> from the V -dimensional Dirichlet distribution with parameters <α 1 , . . . , α V>
最快的实现是draw V independent samples y 1 , . . . , y V from gamma distributions, each with density
然后(也就是正则化gamma分布的采样)
[http://en.wikipedia.org/wiki/Dirichlet distribution]
整个朴素贝叶斯模型的吉布斯采样框架
模型初始化
=<1, 1> uninformed prior: uniform distribution
=<1, ..., 1> Let γθ be a V-dimensional vector where the value of every dimension
equals 1. uninformed prior
Pick a value π by sampling from the Beta(γ π1 , γ π0 ) distribution. sample出文档j的label为1的概率,也就知道了文档j的label的bernoulli概率分布(π, 1-π)。
Then, for each j, flip a coin with success probability π, and assign label L j(0)— that is, the label of document j at the 0 th iteration – based on the outcome of the coin flip. 通过上步得到的bernoulli分布sample出文档的label。
Similarly,you also need to initialize θ 0 and θ 1 by sampling from Dirichlet(γθ ).
模型迭代
2.5.1 文档j标签label的采样公式
2.5.2 θ采样中的(这里i下标代表V维向量t的第i个元素)
Note: lz: 如果要并行计算,特别注意的变量主要只有三个:,
,
算 法中第3步好像写错了,应该去掉not?
Note: as soon as a new label for L j is assigned, this changes the counts that will affect the labeling of the subsequent documents. This is, in fact, the whole principle behind a Gibbs sampler!
从吉布斯采样中产生值
吉布斯采样算法的初始化和采样迭代都会产生每个变量的值(for iterations t = 1, 2, . . . , T),In theory, the approximated value for any variable Z i can simply be obtained by calculating:
正如我们所知,吉布斯采样迭代进入收敛阶段才是稳定分布,所以一般式59加和不是从1开始,而是B + 1 through T,要丢弃t < B的采样结果。
In this context, Jordan Boyd-Graber (personal communication) also recommends looking at Neal’s [15] discussion of likelihood as a metric of convergence.
PS
1 2.6 Optional: A Note on Integrating out Continuous Parameters
In Section 3 we discuss how to actually obtain values from a Gibbs sampler, as opposed to merely watching it walk around the state space. (Which might be entertaining, but wasn’t really the point.) Our discussion includes convergence and burn-in, auto-correlation
and lag, and other practical issues.
In Section 4 we conclude with pointers to other things you might find it useful to read, as well as an invitation to tell us how we could make this document more accurate or more useful.
同步图计算并行化处理
lz的不一定正确。。。
将文档数据分成M份,分布在M个worker节点中。
超步1:所有节点运行朴素贝叶斯吉布斯采样算法的模型初始化部分,得到,
,
的初始值
超步2+:
超步开始前,主节点通过其它节点发来的改变的增量信息计算
的新值,并分发给worker所有节点。(全局模型)
每个节点保存有自己的信息,收到其它节点发来的
改变的增量信息,主节点发来的
信息
重新运行重新朴素贝叶斯吉布斯采样算法的模型的迭代部分,计算新值(局部模型)
整个节点的数据迭代完成后计算改变的增量信息,并在超步结束后发给主节点。
burn-in阶段丢弃所有节点采样结果;收敛阶段将所有节点得到的采样结果:所有文档的标签值记录下来。
整个过程进行多次迭代,最后文档的标签值就是所有迭代得到的向量[标签值]的均值偏向的标签值。
还有一种同步图并行计算可能是这样?
类似Spark MLlib LDA 基于GraphX实现原理,以文档到词作为边,以词频作为边数据,把语料库构造成图,把对语料库中每篇文档的每个词操作转化为在图中每条边上的操作,而对边RDD处理是GraphX中最常见的的处理方法。
[Spark MLlib LDA 基于GraphX实现原理及源码分析]
基于GraphX实现的Gibbs Sampling LDA,定义文档与词的二部图,顶点属性为文档或词所对应的topic向量计数,边属性为Gibbs Sampler采样生成的新一轮topic。每一轮迭代采样生成topic,用mapReduceTriplets函数为文档或词累加对应topic计数。这好像是Pregel的处理方式?Pregel实现过LDA。
[基于GraphX实现的Gibbs Sampling LDA]
[Collapsed Gibbs Sampling for LDA]
from: http://blog.csdn.net/pipisorry/article/details/51525308
ref: Philip Resnik : GIBBS SAMPLING FOR THE UNINITIATED*
Reading Note : Gibbs Sampling for the Uninitiated
随机采样和随机模拟:吉布斯采样Gibbs Sampling实现文档分类的更多相关文章
- 随机模拟MCMC和Gibbs Sampling
随机模拟 统计模拟中有一个重要的问题就是给定一个概率分布 p(x),我们如何在计算机中生成它的样本.一般而言均匀分布 Uniform(0,1)的样本是相对容易生成的. 通过线性同余发生器可以生成伪随机 ...
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling
http://blog.csdn.net/pipisorry/article/details/51373090 吉布斯采样算法详解 为什么要用吉布斯采样 通俗解释一下什么是sampling. samp ...
- [学习笔记] Gibbs Sampling
Gibbs Sampling Intro Gibbs Sampling 方法是我最近在看概率图模型相关的论文的时候遇见的,采样方法大致为:迭代抽样,最开始从随机样本中抽样,然后将此样本作为条件项,按条 ...
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling实现高斯分布参数推断
http://blog.csdn.net/pipisorry/article/details/51539739 吉布斯采样的实现问题 本文主要说明如何通过吉布斯采样来采样截断多维高斯分布的参数(已知一 ...
- 随机采样方法整理与讲解(MCMC、Gibbs Sampling等)
本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...
- 机器学习方法(八):随机采样方法整理(MCMC、Gibbs Sampling等)
转载请注明出处:Bin的专栏,http://blog.csdn.net/xbinworld 本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比 ...
- 随机采样方法整理与讲解(Acceptance-Rejection、MCMC、Gibbs Sampling等)
本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...
- SAS文档:简单的随机点名器
本次实验,我们设计了一个简单的随机点名系统,下面我来介绍一下它的SRS文档. 1.功能需求: 1.1 模块1 在此模块中,我们设置了RandomName类,创建一个随机点名器,里面加入了所在课程的名单 ...
- Atitit.并发测试解决方案(2) -----获取随机数据库记录 随机抽取数据 随机排序 原理and实现
Atitit.并发测试解决方案(2) -----获取随机数据库记录 随机抽取数据 随机排序 1. 应用场景 1 2. 随机抽取数据原理 1 3. 常用的实现方法:::数据库随机函数 1 4. Mssq ...
随机推荐
- ●BZOJ 1797 [Ahoi2009]Mincut 最小割
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1797 题解: 详细的讲解去看http://hzwer.com/3217.html首先跑一个最 ...
- Codeforces Round#433 简要题解
来自FallDream的博客,未经允许,请勿转载,谢谢. 我的号自从几个月前姿势水平过低疯狂掉分之后就没动过了 突然想上点分 就打了一场Div1 没想到打到了rank5 一发上橙了,可还行. ...
- Windows 2008 R2_NLB网络负载均衡(图文详解)(转)
目录 前言 软件环境 DNS域名服务器 DNS服务器原理 DNS域名空间 DNS区域 DNS服务器的类别 DNS查询模式 缓存文件 配置DNS服务器 DNS服务的应用 新建子域 在DNS正向解析中新建 ...
- Optaplanner逐步学习(0) : 基本概念 - Optaplanner,规划问题, 约束,方案
之前的文章中,分别从APS,排产到规划引擎叙述了一些理论基础:并介绍了一些Optaplanner大概的情况:并一步步将Optaplanner的示例运行起来,将示例源码导进Eclipse分析了一下它的H ...
- Jenkins的关闭、重启
以前一直用从cmd进入jenkins的安装目录,执行jenkins stop/start,但是新的jenkins有更加方便功能 关闭jenkins服务 只需要在访问jenkins服务器的网址url地址 ...
- 电子凭证 : Java 生成 Pdf
来源:蛙牛, my.oschina.net/lujianing/blog/894365 如有好文章投稿,请点击 → 这里了解详情 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中 ...
- JVM之Java虚拟机详解
这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 规范的典型的 JVM 核心内部组件. 上图显示的组件分两个章节解释.第一章讨论针对每个线程创建的组件,第二章节讨论 ...
- tomcat7+jdk的keytool生成证书 配置https
目前只会使用jdk的keytool来生成证书.本文仅介绍这种方法. 1Windows下: 1.1 生成keystore文件及导出证书 打开控制台: 运行: %JAVA_HOME%\bin\keytoo ...
- javascript templating
JavaScript Micro-Templating I’ve had a little utility that I’ve been kicking around for some time no ...
- Button 使用Command 按钮置灰未更新
当Button绑定了Command,按钮在窗口弹出.或者其它事件时,会自动置灰. 有时,异步执行时间较长时,界面一些绑定了命令的Button,State不会变化,会一直置灰. 直到再次转移Focus. ...