如何在 PyMC3 中定义自定义可能性?在 PyMC2 中,我可以使用@pymc.potential
。我尝试使用pymc.Potential
然而,在 PyMC3 中,布尔运算似乎无法应用于参数(我收到类似的错误this当我这样做时)。例如,以下代码不起作用:
from pymc import *
with Model() as model:
x = Normal('x', 1, 1)
def z(u):
if u > 0: #comparisons like this are not supported
# if theano.tensor.lt(0,u): this is how comparison should be done
return u ** 2
return -u**3
x2 = Potential('x2', z(x))
start = model.test_point
h = find_hessian(start)
step = Metropolis(model.vars, h)
sample(100, step, start)
我不可能将可能性内的所有比较更改为 Theano 语法(即 theano.tensor.{lt,le,eq,neq,gt,ge})。有没有办法使用定义类似于 PyMC2 的似然函数?
您需要使用DensityDist
函数来包装你的对数可能性。从与源捆绑的示例中:
with Model() as model:
lam = Exponential('lam', 1)
failure = np.array([0, 1])
value = np.array([1, 0])
def logp(failure, value):
return sum(failure * log(lam) - lam * value)
x = DensityDist('x', logp, observed=(failure, value))
您可以使用以下方法做出任意非 Theano 确定性@theano.compile.ops.as_op
装饰器,但对于随机指标来说就不那么容易了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)