Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在张量流中测试模型?_Python_Tensorflow - Fatal编程技术网

Python 如何在张量流中测试模型?

Python 如何在张量流中测试模型?,python,tensorflow,Python,Tensorflow,我将遵循本教程: 我希望能够做的是传入一个测试图像x——作为一个numpy数组,并查看结果softmax分类值——可能作为另一个numpy数组。我能在网上找到的关于测试张量流模型的一切都是通过传递测试值和测试标签以及输出精度来实现的。在我的例子中,我希望仅基于测试值输出模型标签 这就是我所尝试的: 导入tensorflow作为tf 将numpy作为np导入 从skimage导入颜色,io from tensorflow.examples.tutorials.mnist import input

我将遵循本教程:

我希望能够做的是传入一个测试图像x——作为一个numpy数组,并查看结果softmax分类值——可能作为另一个numpy数组。我能在网上找到的关于测试张量流模型的一切都是通过传递测试值和测试标签以及输出精度来实现的。在我的例子中,我希望仅基于测试值输出模型标签

这就是我所尝试的: 导入tensorflow作为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'
TypeError回溯(最近一次调用)
在()
36 sess.run(初始化操作)#执行初始化操作
--->37打印(sess.run(feed_dict={x:x}))#这几乎只是在黑暗中拍摄。这里会有什么?
TypeError:不可损坏的类型:“numpy.ndarray”

因此,当训练时,sess.run被传递一个训练步和一个feed命令。当我试图计算张量x时,这会进入feed命令吗?我甚至会使用sess.run吗?(似乎我不得不这么做),但是火车的踏板是什么呢?是否有“测试步骤”或“评估步骤”

您的tf.Session.run op需要一个fetches run(fetches,feed\u dict=None,options=None,run\u metadata=None)

打印(sess.run(train_step,feed_dict={x:x})),但它也需要一个feed_dict用于y_

你是什么意思


打印我们采样的随机值您得到的是
TypeError
,因为您正在使用(可变的)
numpy.ndarray
作为字典的键,但键应该是
tf.placeholder
和值a
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}
在您的情况下,
精度可定义如下:

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

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