如何在不同的输入上使用训练有素的模型

时间:2017-02-27 05:22:21

标签: python machine-learning tensorflow

我实现了一个相对简单的逻辑回归函数。我保存所有必要的变量,如权重,偏差,x,y等,然后我运行训练算法...

# launch the graph
with tf.Session() as sess:

    sess.run(init)

    # training cycle
    for epoch in range(FLAGS.training_epochs):
        avg_cost = 0
        total_batch = int(mnist.train.num_examples/FLAGS.batch_size)
        # loop over all batches
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(FLAGS.batch_size)

            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})

            # compute average loss
            avg_cost += c / total_batch
        # display logs per epoch step
        if (epoch + 1) % FLAGS.display_step == 0:
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))

    save_path = saver.save(sess, "/tmp/model.ckpt")

保存模型并显示训练模型的predictionaccuracy ......

# list of booleans to determine the correct predictions
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
print(correct_prediction.eval({x:mnist.test.images, y:mnist.test.labels}))

# calculate total accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

这一切都很好,花花公子。但是,现在我希望能够使用训练模型预测任何给定的图像。例如,我想提供说7的图片并​​查看它预测的内容。

我有另一个恢复模型的模块。首先我们加载变量......

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

# tf Graph Input
x = tf.placeholder(tf.float32, [None, 784])  # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, [None, 10])  # 0-9 digits recognition => 10 classes

# set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# construct model
pred = tf.nn.softmax(tf.matmul(x, W) + b)  # Softmax

# minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)

# initializing the variables
init = tf.global_variables_initializer()

saver = tf.train.Saver()

with tf.Session() as sess:
    save.restore(sess, "/tmp/model.ckpt")

这很好。现在我想将一个图像与模型进行比较并得到预测。在此示例中,我从测试数据集mnist.test.images[0]中获取第一个图像,然后尝试将其与模型进行比较。

classification = sess.run(tf.argmax(pred, 1), feed_dict={x: mnist.test.images[0]})
print(classification)

我知道这不行。我收到了错误......

  

ValueError:无法为Tensor'占位符:0'提供形状值(784,),其形状为'(?,784)'

我不知所措。这个问题相当长,如果无法做出直截了当的答案,我可以采取一些指导,以便采取措施。

1 个答案:

答案 0 :(得分:1)

您的输入占位符的大小必须为(?, 784),问号意味着可变大小,可能是批量大小。您正在输入大小为(784,)的输入,该输入不能用作错误消息的状态。

在您的情况下,在预测时间内,批量大小只有1,因此以下情况应该有效:

import numpy as np
...
x_in = np.expand_dims(mnist.test.images[0], axis=0)
classification = sess.run(tf.argmax(pred, 1), feed_dict={x:x_in})

假设输入图像可用作numpy数组。如果它已经是张量,则相应的函数为tf.expand_dims(..)