【问题标题】:Run into the following issue: build_tensor_flow is not supported in Eager Mode遇到以下问题:Eager Mode 不支持 build_tensor_flow
【发布时间】:2020-07-10 10:55:21
【问题描述】:

我正在使用 TensorFlow,并且正在尝试将 Keras 模型导出为 TensorFlow 模型。我遇到了上述错误。我正在关注 Lynda (https://www.linkedin.com/learning/building-deep-learning-applications-with-keras-2-0/exporting-google-cloud-compatible-models?u=42751868) 的“使用 Keras 2.0 构建深度学习应用程序”

在尝试构建张量流模型时,我遇到了这个错误,在第 66 行抛出了添加元图和变量函数的定义。

第 66 行,在 build_tensor_info 中 raise RuntimeError("Eager 模式不支持 build_tensor_info。") RuntimeError:Eager 模式不支持 build_tensor_info。

...model_builder.add_meta_graph_and_variables(
        K.get_session(),
        tags=[tf.compat.v1.saved_model.tag_constants.SERVING],
        signature_def_map={
            tf.compat.v1.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
        }
    )
...

大家有什么想法吗?

【问题讨论】:

    标签: python-3.x tensorflow keras tensorflow2.0 eager-execution


    【解决方案1】:

    是因为您使用的是 tensorflow v2。您必须使用 tensorflow v2 兼容性并禁用 Eager 模式。

    请注意您使用的 tensorflow 导入,例如,如果您使用 tensorflow_core,请确保您使用的是“tensorflow”中的所有依赖项。 您必须在代码之前添加:

    import tensorflow as tf
    if tf.executing_eagerly():
       tf.compat.v1.disable_eager_execution()
    

    【讨论】:

    • 谢谢,除非对缩进进行一些调整,否则此解决方案对我有用!
    【解决方案2】:

    在编译来自 LinkedIn Learning 的代码以导出使用 TensorFlow 1.x API 编写的 Keras 模型时遇到了完全相同的问题。将所有内容替换为等效的 tf.compat.v1 函数后,例如更改

    model_builder = tf.saved_model.builder.SavedModelBuilder("exported_model")
    

    model_builder = tf.compat.v1.saved_model.Builder("exported_model")
    

    并按照上面 Cristian Zumelzu 的建议禁用急切执行,代码能够正常运行,并出现有关已弃用函数的预期警告。

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多