在tensorflow中恢复保存的模型时出现问题,如何调试?

时间:2018-11-08 19:29:46

标签: python tensorflow deep-learning

在张量流中训练模型后,将其保存如下:

saver = tf.train.Saver()
saver.save(sess,'myModel/Path/Model_1')

生成名为:

的文件
  • Model_1.meta
  • Model_1.index
  • Model_1.data-000000-of-000001
  • 检查点

现在要在创建新的会话并以与最初创建的方式完全相同的方式初始化tensorflow图之后还原模型,我将其还原如下:

sess = tf.Session()    
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
sess.run(init)

imported_meta = tf.train.Saver()
imported_meta.restore(sess,'myModel/Path/Model_1.meta')

哪个抛出以下错误:

InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [6152,32] rhs shape= [6164,80]
 [[Node: save_2/Assign_3 = Assign[T=DT_FLOAT, _class=["loc:@DGNS/bidirectional_rnn/bw/basic_lstm_cell/kernel"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](DGNS/bidirectional_rnn/bw/basic_lstm_cell/kernel, save_2/RestoreV2/_111)]]

Caused by op u'save_2/Assign_3', defined at:
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets    /externalshell/start_ipython_kernel.py", line 205, in <module>
 __ipythonkernel__.start()
 "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/kernelapp.py", line 459, in start
ioloop.IOLoop.instance().start()
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/ioloop.py", line 162, in start
super(ZMQIOLoop, self).start()
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/minitornado/ioloop.py", line 830, in start
self._run_callback(callback)
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/minitornado/ioloop.py", line 603, in _run_callback
ret = callback()

... ...等等

我需要帮助来了解这里发生的事情。该错误提示某些形状不匹配的问题。但是我不知道这是怎么回事,因为我使用了完全相同的代码来生成模型和初始化新图。代码中的唯一区别是模型加载部分。

如何开始调试此错误,以提示如何加载模型的正确性?

1 个答案:

答案 0 :(得分:1)

我很确定您不应该加载.meta文件。理解起来很棘手,因为它会为检查点输出3个不同的文件。试试这个:

with tf.Session() as sess:
new_saver = tf.train.import_meta_graph(
    'myModel/Path/Model_1.meta', clear_devices=True)
new_saver.restore(sess, 'myModel/Path/Model_1')

同样,为了澄清起见,您是否也将完整模型存储在.pb文件中,或者只是生成这些检查点?