【发布时间】: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