使用Tensorflow概率进行贝叶斯Logistic回归

时间:2019-04-22 02:46:12

标签: python tensorflow tensorflow-probability hierarchical-bayesian

我在尝试对张量流概率运行贝叶斯逻辑回归示例时遇到问题,如An introduction to probabilistic programming, now available in TensorFlow Probability所示。

如果仅在网站上运行代码,则会出现以下错误:

Traceback (most recent call last):
  File "<input>", line 75, in <module>
TypeError: make_simple_step_size_update_policy() missing 1 required positional argument: 'num_adaptation_steps'

然后,当我指定num_adaptation_steps = 5时,出现以下错误:

FailedPreconditionError (see above for traceback): Error while reading resource variable step_size_hmc from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/step_size_hmc)
     [[node mcmc_sample_chain/transformed_kernel_bootstrap_results/Identity_2/ReadVariableOp (defined at /home/abeer/PycharmProjects/TensorFlowProbability/venv/lib/python3.6/site-packages/tensorflow_probability/python/mcmc/hmc.py:127) ]]

我不知道自己在做什么错,任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

当前Colab中第2章中的Challenger代码应该起作用:

https://colab.sandbox.google.com/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/Ch2_MorePyMC_TFP.ipynb#scrollTo=oHU-MbPxs8iL

hmc=tfp.mcmc.TransformedTransitionKernel(
inner_kernel=tfp.mcmc.HamiltonianMonteCarlo(
    target_log_prob_fn=unnormalized_posterior_log_prob,
    num_leapfrog_steps=40,
    step_size=step_size,
    step_size_update_fn=tfp.mcmc.make_simple_step_size_update_policy(
        num_adaptation_steps=int(burnin * 0.8)),
    state_gradients_are_stopped=True),
bijector=unconstraining_bijectors)

我刚刚注意到本章中较早的HMC示例缺少num_adaptation_steps,因此我将尽快进行PR修复。或者也可以这样做。

谢谢 迈克