如何在张量流中测试模型? [英] How to test a model in tensor flow?
问题描述
我正在学习本教程:
https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html#mnist-for-ml-beginners
我想要做的是传入一个测试图像 x - 作为一个 numpy 数组,然后查看得到的 softmax 分类值 - 也许作为另一个 numpy 数组.我可以在网上找到的关于测试张量流模型的所有内容都是通过传入测试值和测试标签以及输出准确性来工作的.就我而言,我想仅根据测试值输出模型标签.
What I want to be able to do is pass in a test image x - as a numpy array, and see the resulting softmax classification values - perhaps as another numpy array. Everything I can find online about testing tensor flow models works by passing in test values and test labels and the outputting the accuracy. In my case, I want to output the model labels just based on the test values.
这就是我正在尝试的:将张量流导入为 tf将 numpy 导入为 np从 skimage 导入颜色,io
This is what Im trying: import tensorflow as tf import numpy as np from skimage import color,io
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#so now its trained successfully, and W and b should be the stored "model"
#now to load in a test image
greyscale_test = color.rgb2gray(io.imread('4.jpeg'))
greyscale_expanded = np.expand_dims(greyscale_test,axis=0) #now shape (1,28,28)
x = np.reshape(greyscale_expanded,(1,784)) #now same dimensions as mnist.train.images
#initialize the variable
init_op = tf.initialize_all_variables()
#run the graph
with tf.Session() as sess:
sess.run(init_op) #execute init_op
print (sess.run(feed_dict={x:x})) #this is pretty much just a shot in the dark. What would go here?
现在结果如下:
TypeError Traceback (most recent call last)
<ipython-input-116-f232a17507fb> in <module>()
36 sess.run(init_op) #execute init_op
---> 37 print (sess.run(feed_dict={x:x})) #this is pretty much just a shot in the dark. What would go here?
TypeError: unhashable type: 'numpy.ndarray'
所以在训练时,sess.run 会传递一个 train_step 和一个 feed_dict.当我尝试评估张量 x 时,这会出现在 feed dict 中吗?我什至会使用 sess.run 吗?(似乎我必须),但是 train_step 是什么?是否有test_step"或evaluate_step"?
So when training, the sess.run is passed a train_step and a feed_dict. When I am trying to evaluate a tensor x, would this go in the feed dict? Would I even use sess.run?(seems I have to), but what would the train_step be? Is there a "test_step" or "evaluate_step"?
推荐答案
您收到 TypeError
因为您使用(可变)numpy.ndarray
作为字典的键,但键应该是 tf.placeholder
,值应该是 numpy
数组.
You're getting the TypeError
because you are using a (mutable) numpy.ndarray
as a key for your dictionary but the key should be a tf.placeholder
and the value a numpy
array.
以下调整解决了这个问题:
The following adjustment fixes this problem:
x_placeholder = tf.placeholder(tf.float32, [None, 784])
# ...
x = np.reshape(greyscale_expanded,(1,784))
# ...
print(sess.run([inference_step], feed_dict={x_placeholder:x}))
如果您只想对模型进行推理,这将打印一个带有预测的 numpy
数组.
If you just want to perform inference on your model, this will print a numpy
array with the predictions.
如果您想评估您的模型(例如计算准确度),您还需要输入相应的真实标签 y
,如下所示:
If you want to evaluate your model (for example compute the accuracy) you also need to feed in the corresponding ground truth labels y
as in:
accuracy = sess.run([accuracy_op], feed_dict={x_placeholder:x, y_placeholder:y}
在您的情况下,accuracy_op
可以定义如下:
In your case, the accuracy_op
could be defined as follows:
correct_predictions = tf.equal(tf.argmax(predictions, 1), tf.cast(labels, tf.int64))
accuracy_op = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
这里,predictions
是模型的输出张量.
Here, predictions
is the output tensor of your model.
这篇关于如何在张量流中测试模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!