TensorFlow恢复抛出“无变量保存”错误

时间:2017-08-30 06:41:17

标签: python tensorflow

我正在研究一些代码,以了解如何在tensorflow中保存和恢复检查点。为此,我实现了一个简单的神经网络,它使用MNIST数字并保存.ckpt文件,如下所示:

<html>

<head>
  <title>children</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFb0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="MM_preloadImages('1-b.jpg','2-b.jpg','3-b.jpg','4-b.jpg','5-b.jpg','6-b.jpg','7-b','8-b','9-b')">
  <center>
    <div class="wrapper">
      <h1> Some title </h1>
      <div class="pics-wrapper">
        <div class="left-pics">
          <p>
            <img src="https://media-cdn.tripadvisor.com/media/photo-s/0e/9a/e3/1d/freedom-tower.jpg" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://media-cdn.tripadvisor.com/media/photo-s/0e/9a/e3/1d/freedom-tower.jpg',1)" onMouseOut="MM_swapImgRestore()"
            />
            <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Empire_State_Building_%28HDR%29.jpg/150px-Empire_State_Building_%28HDR%29.jpg" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Empire_State_Building_%28HDR%29.jpg/150px-Empire_State_Building_%28HDR%29.jpg',1)"
              onMouseOut="MM_swapImgRestore()" />
            <img src="https://www.nycgo.com/images/uploads/homepage/Empire-State-Building-Observatory-Tom-Perry-2618.jpg" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://www.nycgo.com/images/uploads/homepage/Empire-State-Building-Observatory-Tom-Perry-2618.jpg',1)"
              onMouseOut="MM_swapImgRestore()" />
          </p>
        </div>
        <table id="Table_01" border="0" width="851" cellspacing="0" cellpadding="0">
          <tr>
            <td colspan="2">&nbsp;</td>
            <td colspan="7" rowspan="9"><img id="biggest_one" style="display: block; margin-left: auto; margin-right: auto;" src="https://www.nycgo.com/images/uploads/homepage/Empire-State-Building-Observatory-Tom-Perry-2618.jpg" alt="" width="480" height="640" name="biggest_one"
              /></td>
            <td colspan="2" rowspan="10">&nbsp;</td>
            <td><img src="photo_over/spacer.gif" alt="" width="1" height="3" /></td>
          </tr>
        </table>
        <div class="right-pics">
          <p>
            <img src="https://media.timeout.com/images/103678916/image.jpg" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://media.timeout.com/images/103678916/image.jpg',1)" onMouseOut="MM_swapImgRestore()" />
            <img src="https://lonelyplanetimages.imgix.net/mastheads/GettyImages-538096543_medium.jpg?sharp=10&vib=20&w=1200" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://lonelyplanetimages.imgix.net/mastheads/GettyImages-538096543_medium.jpg?sharp=10&vib=20&w=1200',1)"
              onMouseOut="MM_swapImgRestore()" />
            <img src="https://media.timeout.com/images/103444978/image.jpg" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://media.timeout.com/images/103444978/image.jpg',1)" onMouseOut="MM_swapImgRestore()" />
          </p>
        </div>
      </div>
      <div class="bottom-pic">
        <img src="https://res.cloudinary.com/simpleview/image/upload/c_fill,f_auto,h_510,q_75,w_1280/v1/clients/newyorkstate/dumbo_brooklyn_bridge_park_julienne_schaer_0732_fdff1de5-9486-480c-a1c7-4135d784c75f.jpg" alt="" width="100" height="100" onMouseOver="MM_swapImage('biggest_one','','https://res.cloudinary.com/simpleview/image/upload/c_fill,f_auto,h_510,q_75,w_1280/v1/clients/newyorkstate/dumbo_brooklyn_bridge_park_julienne_schaer_0732_fdff1de5-9486-480c-a1c7-4135d784c75f.jpg',1)" onMouseOut="MM_swapImgRestore()" />
      </div>
    </div>
  </center>
</body>

</html>

这部分效果很好,我将.ckpt文件保存在正确的目录中。当我尝试恢复模型以尝试再次处理它时,问题就出现了。我使用以下代码恢复模型:

    from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

learning_rate = 0.001
n_input = 784 # MNIST data input (img shape = 28*28)
n_classes = 10 # MNIST total classes 0-9

#import MNIST data
mnist = input_data.read_data_sets('.', one_hot = True)

#Features and Labels 
features = tf.placeholder(tf.float32, [None, n_input])
labels = tf.placeholder(tf.float32, [None, n_classes])

#Weights and biases
weights = tf.Variable(tf.random_normal([n_input, n_classes]))
bias = tf.Variable(tf.random_normal([n_classes]))

#logits = xW + b
logits = tf.add(tf.matmul(features, weights), bias)

#Define loss and optimizer
cost = tf.reduce_mean(\
  tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\
.minimize(cost)


# Calculate accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


import math

save_file = './train_model.ckpt'
batch_size = 128
n_epochs = 100

saver = tf.train.Saver()

# Launch the graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for epoch in range(n_epochs):
        total_batch = math.ceil(mnist.train.num_examples / batch_size)

        # Loop over all batches
        for i in range(total_batch):
            batch_features, batch_labels = mnist.train.next_batch(batch_size)
            sess.run(
                optimizer,
                feed_dict={features: batch_features, labels: batch_labels})

        # Print status for every 10 epochs
        if epoch % 10 == 0:
            valid_accuracy = sess.run(
                accuracy,
                feed_dict={
                    features: mnist.validation.images,
                    labels: mnist.validation.labels})
            print('Epoch {:<3} - Validation Accuracy: {}'.format(
                epoch,
                valid_accuracy))

    # Save the model
    saver.save(sess, save_file)
    print('Trained Model Saved.')

并最终得到错误:saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, 'train_model.ckpt.meta') print('model restored')

不太确定,这里的错误是什么。任何帮助表示赞赏。提前致谢

2 个答案:

答案 0 :(得分:1)

您应该告诉Saver哪个Variables要恢复,默认Saver会从默认图表中获取所有Variables

与您的情况一样,您应该在saver = tf.train.Saver()

之前添加构建图代码

答案 1 :(得分:0)

GraphSession不同。图是连接张量的一组操作,每个张量是一组值的符号表示。 SessionVariable张量分配特定值,并允许您在该图表中进行run次操作。

chkpt文件保存变量值 - 即保存在权重和偏差中的值 - 但不保存图形本身。

解决方案很简单:重新运行图形构造(Session之前的所有内容,然后启动会话并从chkpt文件加载值。

或者,您可以查看this guide for exporting and importing MetaGraphs