【问题标题】:restoring two Tensorflow models恢复两个 TensorFlow 模型
【发布时间】:2017-11-01 15:19:30
【问题描述】:

我已经训练了两个独立的 Tensorflow 模型,并希望在一个 Jupyter 笔记本中同时使用它们。我正在关注以下SO post。但是,我想避免使用 with 语句,因为它模糊了我对正在发生的事情的理解。这是我的代码和错误消息:

meta_path_1 = r'.\NN state save\case_guessing-3.meta'
checkpoint_path_1 = r'.\NN state save'

meta_path_2 = r'.\NN state save\class_guessing-3.meta'
checkpoint_path_2 = r'.\NN state save'

new_all_saver_1 = tf.train.import_meta_graph(meta_path_1)
new_all_saver_2 = tf.train.import_meta_graph(meta_path_2)

graph_1 = tf.Graph()
graph_2 = tf.Graph()

sess_1 = tf.Session(graph = graph_1)
sess_2 = tf.Session(graph = graph_2)

new_all_saver_1.restore(sess_1, tf.train.latest_checkpoint(checkpoint_path_1))
new_all_saver_2.restore(sess_2, tf.train.latest_checkpoint(checkpoint_path_2))

predict_tensor_1= graph_1.get_tensor_by_name('predictions:0')
predict_tensor_2= graph_2.get_tensor_by_name('predictions:0')

x_1=graph_1.get_tensor_by_name('input_placeholder:0')
x_2=graph_2.get_tensor_by_name('input_placeholder:0')

print(sess_1.run(tf.shape(x_1)))
print(sess_2.run(tf.shape(x_2)))

这里是错误信息:

INFO:tensorflow:Restoring parameters from .\TNC-Kaggle\Output\NN_1\NN state save\case_guessing-3
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-18-9f8dfdc2cc26> in <module>()
     14 sess_2 = tf.Session(graph = graph_2)
     15 
---> 16 new_all_saver_1.restore(sess_1, tf.train.latest_checkpoint(checkpoint_path_1))
     17 new_all_saver_2.restore(sess_2, tf.train.latest_checkpoint(checkpoint_path_2))
     18 

~\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\training\saver.py in restore(self, sess, save_path)
   1558     logging.info("Restoring parameters from %s", save_path)
   1559     sess.run(self.saver_def.restore_op_name,
-> 1560              {self.saver_def.filename_tensor_name: save_path})
   1561 
   1562   @staticmethod

~\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    893     try:
    894       result = self._run(None, fetches, feed_dict, options_ptr,
--> 895                          run_metadata_ptr)
    896       if run_metadata:
    897         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1051       raise RuntimeError('Attempted to use a closed Session.')
   1052     if self.graph.version == 0:
-> 1053       raise RuntimeError('The Session graph is empty.  Add operations to the '
   1054                          'graph before calling run().')
   1055 

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

我该如何解决?我已经多次重新阅读关于图形和会话之间交互的谷歌文档,但我仍然不清楚缺少什么。插入as_default(),因为某些地方产生了不同的错误(此处无法重现)

【问题讨论】:

    标签: tensorflow save restore


    【解决方案1】:

    出现问题是因为您在导入模型后创建了图表。此时您的模型将被导入到当前的默认图表中。可能还有更多问题,但至少您需要将代码更改为:

    meta_path_1 = r'.\NN state save\case_guessing-3.meta'
    checkpoint_path_1 = r'.\NN state save'
    
    meta_path_2 = r'.\NN state save\class_guessing-3.meta'
    checkpoint_path_2 = r'.\NN state save'
    
    # Create the graphs first
    graph_1 = tf.Graph()
    graph_2 = tf.Graph()
    
    # Import models into corresponding graphs
    new_all_saver_1 = tf.train.import_meta_graph(meta_path_1, graph=graph_1)
    new_all_saver_2 = tf.train.import_meta_graph(meta_path_2, graph=graph_2)
    
    # The rest of your code
    ...
    

    【讨论】:

      【解决方案2】:

      我想知道和你一样的情况。 我从一个链接中找到了一个很好的答案,虽然这个人说这是“不完美的解决方案”,但它对我来说非常有效。

      Loading two models from Saver in the same Tensorflow session

      根据链接,我认为您的代码应如下所示:

      graph_1 = tf.Graph()
      graph_2 = tf.Graph()
      
      new_all_saver_1 = tf.train.import_meta_graph(meta_path_1)
      new_all_saver_2 = tf.train.import_meta_graph(meta_path_2)
      
      
      
      sess_1 = tf.Session(graph = graph_1)
      sess_2 = tf.Session(graph = graph_2)
      
      with sess_1.as_default():
          with graph_1.as_default():
              new_all_saver_1.restore(sess_1, tf.train.latest_checkpoint(checkpoint_path_1))
      with sess_2.as_default():
          with graph_2.as_default():
              new_all_saver_2.restore(sess_2, tf.train.latest_checkpoint(checkpoint_path_2))
      
      predict_tensor_1= graph_1.get_tensor_by_name('predictions:0')
      predict_tensor_2= graph_2.get_tensor_by_name('predictions:0')
      

      如果应用后出现其他错误,则可能必须应用

      with sess_n.as_default():
          with graph_n.as_default():
      

      到您的 graph_1.get_tensor_by_name 部分....

      【讨论】:

        猜你喜欢
        • 2018-09-06
        • 2016-05-01
        • 2017-08-21
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 2017-08-04
        相关资源
        最近更新 更多