【问题标题】:Restoring Tensorflow Model ways difference恢复 TensorFlow 模型的方式差异
【发布时间】:2019-02-06 05:30:56
【问题描述】:

我已经看到并尝试了两种方法,但无法理解它有什么不同。以下是我使用的两种方法:
方法一:

saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")

sess  = tf.Session()
sess.run(tf.global_variables_initializer())   
sess.run(tf.local_variables_initializer()) 
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
    saver.restore(sess, tf.train.latest_checkpoint(model_path))
    print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")    

方法二:

saver = tf.train.Saver()
sess  =tf.Session()
sess.run(tf.global_variables_initializer())    
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
        saver.restore(sess, tf.train.latest_checkpoint(model_path))
        print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")    

我想知道的是:

以上两种方法有什么区别?
加载模型的最佳方法是什么?

请让我知道您对此有何建议。

【问题讨论】:

    标签: python python-3.x tensorflow model


    【解决方案1】:

    我会尽量简明扼要,所以这是我对此事的 2 美分。我将评论您代码的重要行以指出我的想法。

    # Importing the meta graph is same as building the same graph from scratch
    # creating the same variables, creating the same placeholders and ect.
    # Basically you are only importing the graph definition
    saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")
    
    sess  = tf.Session()
    # Absolutely no need to initialize the variables here. They will be initialized
    # when the you restore the learned variables.
    sess.run(tf.global_variables_initializer())   
    sess.run(tf.local_variables_initializer()) 
    if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
        saver.restore(sess, tf.train.latest_checkpoint(model_path))
        print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")
    

    至于第二种方法:

    # You can't create a saver object like this, you will get an error "No variables to save", which is true.
    # You haven't created any variables. The workaround for doing this is:
    # saver = tf.train.Saver(defer_build=True) and then after building the graph
    # ....Graph building code goes here....
    # saver.build()
    saver = tf.train.Saver()
    sess = tf.Session()
    # Absolutely no need to initialize the variables here. They will be initialized
    # when the you restore the learned variables. 
    sess.run(tf.global_variables_initializer())    
    if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
        saver.restore(sess, tf.train.latest_checkpoint(model_path))
        print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")
    

    所以第一种方法没有错,但第二种方法完全不正确。不要误会我的意思,但我不喜欢他们中的任何一个。不过,这只是个人口味。另一方面,我想做的是:

    # Have a class that creates the model and instantiate an object of that class
    my_trained_model = MyModel()
    # This is basically the same as what you are doing with
    # saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")
    # Then, once I have the graph build, I will create a saver object
    saver = tf.train.Saver()
    # Then I will create a session
    with tf.Session() as sess:
        # Restore the trained variables here
        saver.restore(sess, model_checkpoint_path)
        # Now I can do whatever I want with the my_trained_model object
    

    希望对你有所帮助。

    【讨论】:

    • 任何方法都会给出不同的结果吗?还是一样的结果?
    • 好吧,正确的结果是所有方法都以与保存时相同的条件返回变量。所以是的,所有方法都会返回相同的结果。
    猜你喜欢
    • 2018-09-06
    • 2016-05-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    相关资源
    最近更新 更多