试图使用DiscreteUniform作为numpy索引

时间:2019-10-25 14:44:45

标签: pymc3

我正在尝试使用pymc3.DiscreteUniform作为numpy 1D数组的索引

这可用于pymc(v2),但我正在过渡到pymc3,并且在pymc下工作的代码在pymc3下无法工作。

import pymc3 as pm
d0 = pm.DiscreteUniform('d0', lower=0, upper=nDens - 1, testval = nDens//2)
pred = np.zeros(len(box.match), np.float64)
for iwvl, amatch in enumerate(box.match):
    pred[iwvl] += amatch['intensitySum'][d0]

我收到以下错误消息: IndexError:只有整数,切片(:),省略号(...),numpy.newaxis(None)和整数或布尔数组都是有效索引

1 个答案:

答案 0 :(得分:0)

我发现了一些可行的方法,但是涉及到进入theano和theano.tensor。 `     导入pymc3作为pm     使用pm.Model()作为模型:         em0 = pm.Normal('em0',mu = emLog,sigma = 0.2)         d0 = pm.DiscreteUniform('d0',下= 0,上= nDens-1,testval = Dindex)         boundNormal = pm.Bound(pm.Normal,lower = 0.0)         wght = boundNormal('wght',mu = 0.2,sigma = 0.1)

    pred = np.zeros((nDens, len(box.match)), np.float64)
    for iwvl, amatch in enumerate(box.match):
        pred[0:,iwvl] += amatch['intensitySum']
    xpred = theano.shared(pred, name='p0')
    idx = tensor.as_tensor_variable(d0)
    predicted = xpred[idx]*10.**em0
    nObs = len(box.match)
    intensity = np.zeros(nObs, np.float64)
    for iwvl in range(nObs):
        intensity[iwvl] = box.match[iwvl]['obsIntensity']
    sigma = 0.2
    Y_obs = pm.Normal('Y_obs', mu=predicted, sigma=wght*intensity,    observed=intensity)
    trace = pm.sample(tune=20000, draws=100000, target_accept=0.85)`

然后您可以使用跟踪 甚至有可能使sigma成为pm变量