简体   繁体   English

从另一个python文件导入tf.train.Saver

[英]Importing tf.train.Saver from another python file

I use tf.train.Saver() in one.py file with the following code. 我使用以下代码在one.py文件中使用tf.train.Saver()

saver = tf.train.Saver(tf.all_variables())
saver.save(sess,"checkpoint.data")

How can I restore checkpoint.data in another python file? 如何在另一个python文件中恢复checkpoint.data

I used the following code, but it didn't work. 我使用了以下代码,但是没有用。

from one import saver
import tensorflow as tf

with tf.Session() as sess:
    saver.restore(sess, "checkpoint.data")

The checkpoint file (ie 'checkpoint.data' ) does not provide TensorFlow with enough information to reconstruct your model structure. 检查点文件(即'checkpoint.data' )没有为TensorFlow提供足够的信息来重构模型结构。 In your second program, you need to reconstruct the same TensorFlow graph that was used in the first program. 在第二个程序中,您需要重建在第一个程序中使用的相同的TensorFlow图。 There are a few options for doing this: 有几种方法可以执行此操作:

  • Extract the model building code into a Python function, and call it before creating the tf.train.Saver in each program. 将模型构建代码提取到Python函数中,并在每个程序中创建tf.train.Saver之前调用它。
  • Use saver.export_meta_graph() to write out the graph structure along with a checkpoint in your first program, and tf.train.import_meta_graph() to import the graph structure (and create an appropriately configure tf.train.Saver instance) in your second program. 使用saver.export_meta_graph()在第一个程序中写出图形结构以及检查点,并在第二个程序中使用tf.train.import_meta_graph()导入图形结构(并创建适当配置的tf.train.Saver实例)。程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM