gpt4 book ai didi

python - 类型错误 : An op outside of the function building code is being passed a Graph tensor

转载 作者:行者123 更新时间:2023-12-04 16:03:33 33 4
gpt4 key购买 nike

我收到以下异常

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: conv2d_flipout/divergence_kernel:0

这也引发了以下异常
tensorflow.python.eager.core._SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'conv2d_flipout/divergence_kernel:0' shape=() dtype=float32>]

运行以下代码时
from __future__ import print_function

import tensorflow as tf
import tensorflow_probability as tfp


def get_bayesian_model(input_shape=None, num_classes=10):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=input_shape))
model.add(tfp.layers.Convolution2DFlipout(6, kernel_size=5, padding="SAME", activation=tf.nn.relu))
model.add(tf.keras.layers.Flatten())
model.add(tfp.layers.DenseFlipout(84, activation=tf.nn.relu))
model.add(tfp.layers.DenseFlipout(num_classes))
return model

def get_mnist_data(normalize=True):
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

if tf.keras.backend.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

if normalize:
x_train /= 255
x_test /= 255

return x_train, y_train, x_test, y_test, input_shape


def train():
# Hyper-parameters.
batch_size = 128
num_classes = 10
epochs = 1

# Get the training data.
x_train, y_train, x_test, y_test, input_shape = get_mnist_data()

# Get the model.
model = get_bayesian_model(input_shape=input_shape, num_classes=num_classes)

# Prepare the model for training.
model.compile(optimizer=tf.keras.optimizers.Adam(), loss="sparse_categorical_crossentropy",
metrics=['accuracy'])

# Train the model.
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1)
model.evaluate(x_test, y_test, verbose=0)


if __name__ == "__main__":
train()

该问题显然与 tfp.layers.Convolution2DFlipout 层有关。 .为什么我会得到这些异常?这是由于我的代码中的逻辑错误还是可能是 TensorFlow 或 TensorFlow Probability 中的错误?这些错误是什么意思?我该如何解决它们?

我正在使用 TensorFlow 2.0.0(默认情况下急切执行)。和 TensorFlow Probability 0.8.0 和 Python 3.7.4。我也打开了相关问题 herehere .

请不要建议我使用 TensorFlow 1 来懒惰地执行我的代码(也就是说,在导入 TensorFlow 后使用 tf.compat.v1.disable_eager_execution(),因为我知道这将使上面的代码运行而不会出现上述异常)或显式创建 session 或占位符。

最佳答案

通过设置参数 experimental_run_tf_function 可以部分解决此问题。的compile方法 False ,正如我在 comment to the Github issue I had opened 中所写的那样.
但是,如果您设置 experimental_run_tf_functionFalse你尝试使用 predict方法,你会得到另一个错误。见 this Github issue .

编辑(2020 年 9 月 28 日)experimental_run_tf_function已在最新版本的 TF 中删除。然而,在最新版本的 TFP 中(我使用的具体版本在下面列出),贝叶斯卷积层(至少是使用 Flipout 估计器的那个)的问题得到了修复。见 https://github.com/tensorflow/probability/issues/620#issuecomment-620821990https://github.com/tensorflow/probability/commit/1574c1d24c5dfa52bdf2387a260cd63a327b1839 .
具体来说,我使用了以下版本

tensorflow==2.3.0
tensorflow-probability==0.11.0
我使用了密集和卷积贝叶斯层,我做了 不是 使用 experimental_run_tf_function=False调用 compile时.

关于python - 类型错误 : An op outside of the function building code is being passed a Graph tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58565913/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com