我一直在浏览几篇Tensorflow教程,并且在使用模型进行了训练/测试之后,还没有看到任何有关使用模型的内容。我通过计算器看了看,发现不喜欢here如何在Tensorflow中预测未标记的图像
工作了我,所以我正在使用的代码here与我改变了代码,所以我可以尝试运行的预测,而不是事后的会议闭幕除外几个解决方案。对于预测,我只是使用一个测试样本,但试图在没有给出标签的情况下完成。我想看看什么课程是预测的。
# 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)'
道歉。我更新了链接。我将使用所有代码编辑我的帖子。 – Exuro
我做了一个编辑以显示一个较小的例子。如您所述,我正在接受y输出并使用eval。现在我得到一个输出,但我不确定这些值是什么。 – Exuro
好吧,我想通了。得到了评估工作。我的问题是没有认识到产出代表着这个阶级,而最高价值是最可能的。我将找出如何将其转化为0 - 1概率。谢谢。 – Exuro