PyMC 3中的Dirichlet过程 [英] Dirichlet process in PyMC 3

查看:238
本文介绍了PyMC 3中的Dirichlet过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以实现在中引用的Dirichlet过程示例 为贝叶斯半参数模型实施Dirichlet流程 (来源:此处)在PyMC 3中.

I would like to implement to implement the Dirichlet process example referenced in Implementing Dirichlet processes for Bayesian semi-parametric models (source: here) in PyMC 3.

在此示例中,使用pymc.deterministic计算不合格概率 装饰者:

In the example the stick-breaking probabilities are computed using the pymc.deterministic decorator:

v = pymc.Beta('v', alpha=1, beta=alpha, size=N_dp)
@pymc.deterministic
def p(v=v):
    """ Calculate Dirichlet probabilities """

    # Probabilities from betas
    value = [u*np.prod(1-v[:i]) for i,u in enumerate(v)]
    # Enforce sum to unity constraint
    value[-1] = 1-sum(value[:-1])

    return value

 z = pymc.Categorical('z', p, size=len(set(counties)))

您如何在使用Theano进行梯度计算的PyMC 3中实现此目标?

How would you implement this in PyMC 3 which is using Theano for the gradient computation?

我使用theano.scan方法尝试了以下解决方案:

edit: I tried the following solution using the theano.scan method:

with pm.Model() as mod:
    conc = Uniform('concentration', lower=0.5, upper=10)
    v = Beta('v', alpha=1, beta=conc, shape=n_dp)
    p, updates = theano.scan(fn=lambda stick, idx: stick * t.prod(1 - v[:idx]),
                             outputs_info=None,
                             sequences=[v, t.arange(n_dp)])
    t.set_subtensor(p[-1], 1 - t.sum(p[:-1]))
    category = Categorical('category', p, shape=n_algs)
    sd = Uniform('precs', lower=0, upper=20, shape=n_dp)
    means = Normal('means', mu=0, sd=100, shape=n_dp)
    points = Normal('obs',
                    means[category],
                    sd=sd[category],
                    observed=data)

    step1 = pm.Slice([conc, v, sd, means])
    step3 = pm.ElemwiseCategoricalStep(var=category, values=range(n_dp))
    trace = pm.sample(2000, step=[step1, step3], progressbar=True)

可悲的是,它确实很慢,并且无法获得合成数据的原始参数.

Which sadly is really slow and does not obtain the original parameters of the synthetic data.

有没有更好的解决方案,这是否正确?

Is there a better solution and is this even correct?

推荐答案

不确定我是否有一个好的答案,但是也许可以通过使用theano黑盒op来加快速度,该操作允许您在其中编写分布(或确定性) python代码.例如: https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_arbitrary_deterministic.py

Not sure I have a good answer but perhaps this could be sped up by instead using a theano blackbox op which allows you to write a distribution (or deterministic) in python code. E.g.: https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_arbitrary_deterministic.py

这篇关于PyMC 3中的Dirichlet过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆