我试图在numpy
里面使用cnn_model.evaluate()
,但它给了AttributeError: 'Tensor' object has no attribute 'numpy'
。我曾经使用and insidenumpy
计算准确性和均方误差tf.keras.metrics.Accuracy()
tf.keras.metrics.MeanSquaredError()
cnn_model.evaluate()
我用谷歌搜索了它,在 tensorflow 文档中,它说
“Estimator 的调用方法将在启用 Eager Execution 的情况下工作。但是,model_fn 和 input_fn 不会立即执行,Estimator 将在调用所有用户提供的函数(包括钩子)之前切换到图形模式,因此它们的代码必须兼容图形模式执行。”
所以,我想知道如何将当前的 tf 1.x 代码更新为 tf 2.1.0 代码,同时还使用上述信息。
我目前的代码是:
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)
到目前为止,我尝试的是添加tf.compat.v1.enable_eager_execution()
到 1)所有导入之后的代码开头,2)在导入 tf 之后的下一行,3)在声明 eval_input_fn 之前的行,4)在调用 eval_results 之前的行,5)在 CNN 内部模型定义。这一切都未能打开渴望模式。
我发现的另一个选项是删除 @tf.function 装饰器,但我不知道这意味着什么以及如果删除了 @tf.function 如何传递 input_fn。