PyMC3:每次给出不同的结果

PyMC3: Giving a Different Result Every time

我定义了一个对数似然函数,并且我有一个变量在均匀分布上被采样。我确保对数似然函数 returns 相同输入的结果相同。但是当我采样时,每次分布都有些不同(在相同范围内)。

这是怎么回事?

import pymc3 as mc
import theano.tensor as tt

SAMPLES = 1000
TUNING_SAMPLES = 100
N_CORES = 10
N_CHAINS = 2

#(logl_ThetaFromChoices is defined above with the input)

# use PyMC3 to sampler from log-likelihood
with mc.Model() as modelFindTheta:
    theta = mc.Uniform('theta', lower=-200.0, upper=200.0)

    # convert m and c to a tensor vector
    theta = tt.as_tensor_variable(theta)

    def callOp(v):
        return logl_ThetaFromChoices(v)
    mc.DensityDist('logl_ThetaFromChoices', callOp, observed={'v': theta})

    step1 = mc.Metropolis()
    trace_theta = mc.sample(SAMPLES,
                            tune=TUNING_SAMPLES,
                            discard_tuned_samples=True,
                            chains=N_CHAINS,
                            cores=N_CORES,
                            step=step1)

'alpha' == 这里是 Theta

由于涉及到随机数生成,需要设置种子才能得到可重现的结果。对于 PyMC3,这是通过 the pymc3.sampling.sample() method.

中的 random_seed 参数完成的