NotImplementedError: Cannot convert a symbolic Tensor (truediv_2:0) to a numpy array

南笙酒味 提交于 2020-05-14 13:40:06

问题


If you execute the following TensorFlow 2.1 code

import tensorflow as tf
import tensorflow_probability as tfp

tf.config.experimental_run_functions_eagerly(True)


def get_mnist_data(normalize=True, categorize=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

    if categorize:
        y_train = tf.keras.utils.to_categorical(y_train)
        y_test = tf.keras.utils.to_categorical(y_test)

    return x_train, y_train, x_test, y_test, input_shape


def get_model(input_shape, num_classes=10):
    model = tf.keras.Sequential()
    model.add(tfp.layers.Convolution2DFlipout(6, input_shape=input_shape, 
                                              kernel_size=3, padding="SAME",
                                              activation=tf.nn.relu))
    model.add(tf.keras.layers.Flatten())
    model.add(tfp.layers.DenseFlipout(num_classes))
    return model


def train():
    x_train, y_train, x_test, y_test, input_shape = get_mnist_data()

    batch_size = 64

    model = get_model(input_shape)

    model.summary()

    model.compile(loss="categorical_crossentropy")

    model.fit(x_train, y_train, batch_size=batch_size, epochs=1)


if __name__ == '__main__':
    train()

You will get the following error

NotImplementedError: Cannot convert a symbolic Tensor (truediv_2:0) to a numpy array

According to the traceback, this error occurs when the progress bar attempts to print something. This error is due to statement tf.config.experimental_run_functions_eagerly(True). However, if you remove it, you get another error TypeError: An op outside of the function building code is being passed a Graph tensor.

I was trying to run all functions eagerly for debugging purposes because apparently not all functions run eagerly in TensorFlow 2.0. See this Github issue.

Here's the Github issue related to this question.

来源:https://stackoverflow.com/questions/61388919/notimplementederror-cannot-convert-a-symbolic-tensor-truediv-20-to-a-numpy-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!