Tensorflow 2.5.0 - TypeError:函数构建代码之外的操作正在传递“图形”张量
Posted
技术标签:
【中文标题】Tensorflow 2.5.0 - TypeError:函数构建代码之外的操作正在传递“图形”张量【英文标题】:Tensorflow 2.5.0 - TypeError: An op outside of the function building code is being passed a "Graph" tensor 【发布时间】:2021-09-27 07:29:38 【问题描述】:我正在训练一个使用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
【问题讨论】:
你能用虚拟数据添加一个可重现的例子吗? 【参考方案1】:您需要禁用急切执行:tf.compat.v1.disable_eager_execution()
。
【讨论】:
以上是关于Tensorflow 2.5.0 - TypeError:函数构建代码之外的操作正在传递“图形”张量的主要内容,如果未能解决你的问题,请参考以下文章