Keras和TF2.0的热切vs图形模式

问题描述

我正在以图形模式编译以前训练过的模型:

inference_model = model_from_json(json.dumps(inference_model_dict))
inference_model.set_weights(original_model.get_weights())
inference_model.compile('adam','binary_crossentropy',run_eagerly=False)

用于预测时:

predictions = inference_model.predict(gen)

出现以下错误

ValueError:在启用了[eager]模式的情况下构造Model.predict实例时,不支持在图形模式下调用Model。请在图形模式下构建Model实例,或在启用快速模式的情况下调用Model.predict

我试图通过禁用TF2.0认值

tf.compat.v1.disable_eager_execution()
  • 这也没有帮助。

在阅读Keras文档时,找不到如何遵循此建议:“在启用紧急模式的情况下致电Model.predict

最初,该模型是在急切模式下训练的。这有关系吗?不能以某种方式将其覆盖吗?

解决方法

通过重新安排与会话相关的代码来解决该问题。

,

我相信我对会话的定义方式有一些疑问(从TF1.15代码继承)。一旦简化了这一点,问题就消失了。

,

我使用了解决我问题的 tf.compat.v1.disable_eager_execution()