我实现了一个相对简单的逻辑回归函数。我保存所有必要的变量,如权重、偏差、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")
保存模型并显示训练模型的prediction
和{
这一切都很好。但是,现在我希望能够使用经过训练的模型来预测任何给定的图像。例如,我想向它提供say 7
的图片,看看它预测它是什么。在
我有另一个模块来恢复模型。首先我们加载变量。。。在
^{3}$这很好。现在我想把一张图像与模型进行比较,得到一个预测。在这个例子中,我从测试数据集mnist.test.images[0]
获取第一张图像,并尝试将其与模型进行比较。在
classification = sess.run(tf.argmax(pred, 1), feed_dict={x: mnist.test.images[0]})
print(classification)
我知道这行不通。我知道错误。。。在
ValueError: Cannot feed value of shape (784,) for Tensor 'Placeholder:0', which has shape '(?, 784)'
我不知道该怎么想。这个问题相当长,如果不可能有一个直接的答案,请提供一些关于我可以采取的步骤的指导。在
输入占位符的大小必须是
(?, 784)
,问号表示可变大小,可能是批大小。您正在输入一个大小为(784,)
的输入,它不作为错误消息状态工作。在在您的情况下,在预测时间内,批大小仅为1,因此以下操作应该有效:
假设输入图像可用作numpy数组。如果它已经是张量,对应的函数是
tf.expand_dims(..)
。在相关问题 更多 >
编程相关推荐