【问题标题】:How to test a model in tensor flow?如何在张量流中测试模型?
【发布时间】:2016-11-01 17:53:22
【问题描述】:

我正在关注本教程:

https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html#mnist-for-ml-beginners

我想要做的是传入一个测试图像 x - 作为一个 numpy 数组,并查看生成的 softmax 分类值 - 也许作为另一个 numpy 数组。我可以在网上找到的关于测试张量流模型的所有内容都是通过传入测试值和测试标签以及输出准确性来工作的。就我而言,我想仅根据测试值输出模型标签。

这是我正在尝试的: 将张量流导入为 tf 将 numpy 导入为 np 从 skimage 导入颜色,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”?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您的 tf.Session.run 操作需要获取

    tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=None)
    

    https://www.tensorflow.org/versions/r0.9/api_docs/python/client.html#session-management

    print (sess.run(train_step,feed_dict={x:x}))   #but it also needs a feed_dict for y_ 
    

    你是什么意思:

    打印我们采样的随机值

    【讨论】:

    • 我将删除该打印语句,因为它可能令人困惑。你能把你的答案写成代码形式吗?我不完全确定你的意思
    • 打印 (sess.run(train_step,feed_dict={x:x y_:some-value}))
    • 在这种情况下 some-value 是什么?这就是我试图让它预测的内容。
    • 它需要是一个值:y_ = tf.placeholder(tf.float32, [None, 10])
    【解决方案2】:

    您得到TypeError 是因为您使用(可变)numpy.ndarray 作为字典的键,但键应该是tf.placeholder,值是numpy 数组。

    以下调整解决了这个问题:

    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 数组。

    如果您想评估您的模型(例如计算准确度),您还需要输入相应的真实标签y,如下所示:

    accuracy = sess.run([accuracy_op], feed_dict={x_placeholder:x, y_placeholder:y}
    

    在您的情况下,accuracy_op 可以定义如下:

    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 是模型的输出张量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2021-03-20
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多