Tensorflow概率/ Edward2-在MCMC期间获取内部模型值

时间:2019-01-15 22:12:15

标签: python tensorflow tensorflow-probability edward

我已经在Tensorflow Probability / Edward2中建立了一个生成模型,现在我对适应MCMC技术很感兴趣。

flips1 = sum(np.round(np.random.rand(10)))
flips2 = sum(np.round(np.random.rand(10)))


def m():
    prior1 = ed.Beta(concentration0=10, concentration1=10, name='prior1')
    outcome1 = ed.Binomial(total_count=10, probs=prior1, name='outs1')

    prior2 = ed.Beta(concentration0=1, concentration1=1, name='prior2')
    outcome2 = ed.Binomial(total_count=10, probs=prior2, name='outs2')

    c = outcome1 + outcome2
    return c


fnc = ed.make_log_joint_fn(m)

def target(p1, p2):
    return fnc(prior1=p1, outs1=flips1, prior2=p2, outs2=flips2)


step_size = tf.get_variable(
    name='step_size',
    initializer=.001,
    use_resource=True,  # For TFE compatibility.
    trainable=False)

hmc = tfp.mcmc.HamiltonianMonteCarlo(
    target_log_prob_fn=target,
    num_leapfrog_steps=3,
    step_size=step_size,
    step_size_update_fn=tfp.mcmc.make_simple_step_size_update_policy())

samples, kernel_results = tfp.mcmc.sample_chain(
    num_results=int(10e3),
    num_burnin_steps=int(1e3),
    current_state=[.01, .01],
    kernel=hmc)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    samples_, res = sess.run([samples, kernel_results])

从这里开始,samples_给了我prior1prior2的马尔可夫链样本。但是,我还有兴趣查看模型中其他变量的值。例如,我想查看outcome1outcome2的马尔可夫链。

最重要的是,我希望看到c的值而无需参数化。在pyMC3中,可以通过将其设置为pm.Deterministic来实现。我无法在TF概率中找到等效的功能-是否可用? ed.tape()与此有关吗?

0 个答案:

没有答案