[英]How to predict unlabeled images in Tensorflow
我已经遍历了多个Tensorflow教程,并且在经过训练/测试后没有看到使用模型的任何信息。 我浏览了stackoverflow,发现像这里一样不适合我的一些解决方案
因此,我在这里使用代码,但我更改了代码,因此我可以尝试在以后运行预测,而不是关闭会话。 对于预测,我仅使用测试样本,但尝试在不给出标签的情况下进行计算。 我想看看预计上什么课。
# Launch the graph
#with tf.Session() as sess:
sess = tf.Session()
sess.run(init)
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
keep_prob: dropout})
if step % display_step == 0:
# Calculate batch loss and accuracy
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
# Calculate accuracy for 256 mnist test images
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
y: mnist.test.labels[:256],
keep_prob: 1.}))
从上面列出的堆栈溢出页面中,我应该可以执行以下操作
print(tf.run(pred, feed_dict={x: mnist.test.images[0]}))
虽然看起来像由于tensorflow表示没有运行功能而已将其删除。 建议在同一页面中执行此操作
print(pred.eval(feed_dict={x: mnist.test.images[0]}))
但我收到这个错误
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`
有了这个,我发现我需要运行上面所述的内容,但是我遇到了张量大小不正确的问题
with sess.as_default():
print(pred.eval(feed_dict={x: mnist.test.images[0]}))
ValueError: Cannot feed value of shape (784,) for Tensor 'Placeholder:0', which has shape '(?, 784)'
所以从这里看起来数据未正确对齐? 我尝试使用重塑没有成功。 如果有人能指出我正确的方向,那么我可以弄清楚如何将我的模型实际用于很棒的应用程序。
编辑:这是一个更简单的程序。 我有同样的问题
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.global_variables_initializer())
y = tf.matmul(x,W) + b
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
for i in range(1000):
batch = mnist.train.next_batch(100)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
y.eval(feed_dict={x: mnist.test.images[0]})
我遇到与上述相同的问题
ValueError: Cannot feed value of shape (784,) for Tensor 'Placeholder:0', which has shape '(?, 784)'
笔记本的链接是本地链接,因此没有人可以看到它。 另外,您缺少一些代码。 在哪里定义“ y”?
您需要做的就是评估y,例如:y.eval(feed_dict = {x:NEW_DATA})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.