首页 > 解决方案 > 是否可以在 TensorFlow 2 中使用 TensorFlow 1 模型?

问题描述

例如,某些模型已经使用 TensorFlow 1 架构进行了预训练。是否可以在 TensorFlow 2 中加载这个 TensorFlow 1 模型tf.keras.models.load_model(...)

标签: tensorflowtensorflow2.0

解决方案


为了在 tensorflow v2 中使用 tensorflow v1 模型,您需要将兼容性模块导入为tf.compat.v1并通过以下方式禁用 tensorflow v2 行为tf.compat.v1.disable_v2_behavior()

您可以通过以下方式加载版本 1 模型检查点:

with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")

至于以这种方式使用检查点,您需要使用训练代码转换模型,因为SavedModel这是在 tensorflow v2 中使用的正确格式。


推荐阅读