Skip to main content
 首页 » 编程设计

python之恢复 Tensorflow 模型的方式差异

2024年09月03日25jiqing9006

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

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")     

我想知道的是:

What is the difference between the above two methods?
Which is the best method to load the model?

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

请您参考如下方法:

我会尽量简洁,所以这是我对此事的 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 

希望对您有所帮助。