“黑客的贝叶斯方法”jupyter 笔记本不起作用

时间:2021-05-28 08:27:15

标签: tensorflow2.0 bayesian tensorflow-probability

我正在阅读“黑客贝叶斯方法”的在线 TensorFlow 概率 (TFP) 版本。

但是当我执行 Ch2_MorePyMC_TFP.ipynb 的第一个单元格时 出现以下错误:

<块引用>

属性错误:模块“tensorflow”没有属性“contrib”

我想这个版本的“黑客贝叶斯方法”jupyter 笔记本是为 TF1 编写的。

您是否有使用 TF2 的此 jupyter 笔记本的简单修复或更新版本?

2 个答案:

答案 0 :(得分:1)

一些 contrib 函数被移除,其中一些被合并到 TensorFlow 核心中。您需要找到它们的等效版本。

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors

print(tf.__version__) # 2.5.0
print(tfp.__version__) # 0.12.1

例如第一个 contrib 函数在 TensorFlow 中可用,可以重写为:

parameter = tfd.Exponential(rate=1., name="poisson_param").sample()
rv_data_generator = tfd.Poisson(parameter, name="data_generator")
data_generator = rv_data_generator.sample()


data_generator_ = tf.nest.pack_sequence_as(
     data_generator,
     [t.numpy() if tf.is_tensor(t) else t
     for t in tf.nest.flatten(data_generator)])

print("Value of sample from data generator random variable:", data_generator_)

对于其他 TF 操作,您可以像这样替换它们:

with tf.compat.v1.variable_scope(tf.compat.v1.get_variable_scope(), reuse=tf.compat.v1.AUTO_REUSE):
    step_size = tf.compat.v1.get_variable(
        name='step_size',
        initializer=tf.constant(0.5, dtype=tf.float32),
        trainable=False,
        use_resource=True
    )

可以在documentation

中找到更多信息

答案 1 :(得分:0)

Frightera,我在消除以下错误时遇到问题:

<块引用>

模块 'tensorflow' 没有属性 'variable_scope'

在单元格:

# Initialize the step_size. (It will be automatically adapted.)
with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE):
    step_size = tf.get_variable(
        name='step_size',
        initializer=tf.constant(0.5, dtype=tf.float),
        trainable=False,
        use_resource=True
    )

你知道如何更换这个吗?