Tensorflow 2.5.0 - TypeError: An op outside of the function building code is being passed a "Graph" tensor

Tensorflow 2.5.0 - TypeError: An op outside of the function building code is being passed a "Graph" tensor

我正在训练一个使用 tfp.layers.Convolution3DFlipout 层的卷积贝叶斯神经网络。我的损失函数如下:

from tensorflow.keras.losses import binary_crossentropy

def variational_free_energy_loss(model, scale_factor = tf.constant(1.)):
    kl = sum(model.losses) / scale_factor

    def loss(y_true, y_pred):
        bce = binary_crossentropy(y_true, y_pred)
        return bce + kl 
    return loss

我收到这个错误:

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: conv3d_flipout_189/divergence_kernel:0

有谁知道导致此错误的原因吗?

张量流版本:2.5.0

tensorflow_probability版本:0.13.0

您需要禁用即时执行:tf.compat.v1.disable_eager_execution()

  • 检查此 gist TFlow 2.4 和 TFlow-Prob 0.12.1
  • 这也是 tensorflow/probability 回购中的一个问题 - link