Tensorflow:恢复模型时出错

时间:2018-01-29 06:49:32

标签: tensorflow

我不确定如何使用此模型来获取预测。该模型的代码如下:

layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2, name = "layer_2")

loss_function = tf.reduce_mean(tf.nn.sampled_softmax_loss(
                 weights=weights['out'],
                 biases=biases['out'],
                 labels=y,
                 inputs=layer_2,
                 num_sampled=int(num_words * .10),
                 num_true=1,
                 num_classes=num_words))
optimizer = tf.train.AdamOptimizer().minimize(loss_function)
save_path = saver.save(sess, "C:\\Users\\gowth\\Documents\\model.ckpt")
print("Model saved in file: %s" % save_path)

要恢复模型并访问变量layer_2,我正在使用此代码:

saver = tf.train.import_meta_graph("C:\\Users\\gowth\\Documents\\model.ckpt.meta")

with tf.Session() as sess:
    saver.restore(sess, tf.train.latest_checkpoint('"C:\\Users\\gowth\\Documents\\'))
    print("Model restored.")
    graph = tf.get_default_graph()
    ima, lab = next_batch(1)
    x = graph.get_tensor_by_name("x:0")
    y = graph.get_tensor_by_name("y:0")
    feed_dict={x: ima, y: lab}
    prediction=graph.get_tensor_by_name('layer_2:0')
    print (sess.run(prediction,feed_dict))

我得到的错误是:

TypeError                                 Traceback (most recent call last)
TypeError: expected bytes, NoneType found

During handling of the above exception, another exception occurred:

SystemError                               Traceback (most recent call last)
<ipython-input-150-6c2213900ab9> in <module>()
      4 
      5 with tf.Session() as sess:
----> 6     saver.restore(sess, tf.train.latest_checkpoint('"C:\\Users\\gowth\\Documents\\'))
      7     print("Model restored.")
      8     graph = tf.get_default_graph()

Documents文件夹中,存在以下文件: model.ckpt.meta, checkpoint, model.ckpt.data-00000-of-00001, model.ckpt.index

一般来说,您是否可以评论这种评估方法是否正确。

1 个答案:

答案 0 :(得分:1)

也许问题只是路径前面的双引号。否则,您应该检查检查点文件中的路径是否正确指向模型文件。