1

I tried to use numpy inside cnn_model.evaluate(), but it gave AttributeError: 'Tensor' object has no attribute 'numpy'. I used numpy to calculate accuracy and mean squared error using tf.keras.metrics.Accuracy() and tf.keras.metrics.MeanSquaredError() inside cnn_model.evaluate()

I googled it, and in tensorflow documentation, it said

"Calling methods of Estimator will work while eager execution is enabled. However, the model_fn and input_fn is not executed eagerly, Estimator will switch to graph mode before calling all user-provided functions (incl. hooks), so their code has to be compatible with graph mode execution."

So, I was wondering how I can update the current tf 1.x code to tf 2.1.0 code, while also using above information.

My current code is:

eval_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn(
    x={"x": np.array(train_inputs, dtype=np.float32)},
    y=np.array(train_labels, dtype=np.float32),
    #y=np.array(train_labels),
    batch_size=1,
    num_epochs=1,
    shuffle=False)

eval_results = CNN.evaluate(input_fn=eval_input_fn)

What I have tried so far is add tf.compat.v1.enable_eager_execution() to the 1) beginning of the code after all the imports, 2) next line right after importing tf, 3) line right before declaring eval_input_fn, 4) line right before calling eval_results, 5) inside CNN model definition. It all failed to turn on the eager mode.

One other option that I found was remove @tf.function decorator, but I have no idea what that means and how to pass input_fn if @tf.function is removed.

0 Answers0