我正在尝试创建一个简单的python脚本,该脚本允许您输入手写数字的图片,NN模型将尝试猜测到目前为止它是什么数字我已经成功地制作了模型并对其进行了测试,但是在测试单个图像时,我得到了这样的输出。
https://i.stack.imgur.com/PQhaU.png
def make_pred():
Tk().withdraw()
filename = askopenfilename()
#the array that will hold the values of image
image = np.zeros(784)
#read image
gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE )
#resize image and set background to black
gray = cv2.resize(255-gray, (28,28))
#making the image a one dimensional array of 784
flatten = gray.flatten() / 255.0
cv2.imshow("heh",gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
#saving the image and the correct value
image = flatten
prediction = neural_network_model(x)
n_save = tf.train.Saver()
with tf.Session() as sess2:
n_save.restore(sess2, './nn_test/nnmodel')
print(sess2.run(prediction,feed_dict={x: [image], y:[7]}))
y
值是 7,因为这是我正在尝试的数字。
那么我如何获得 NN 认为角色的值呢?
从你提供的信息中很难分辨出来。但是,我认为您获得的输出很可能只是 logits,在 softmax 输出层之前。
将此输出馈送到 softmax 层,然后得到输出的概率分布。在您的特定情况下,softmax 输出:
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]
现在你只需要取这个张量的argmax。在您的示例中,网络的预测为 5。
您错误地将 logits 作为网络输出的一个可能原因是大多数框架将 softmax 输出与损失函数相结合。因此,虽然损失函数确实将logits作为输入,但只有在logits上应用softmax输出层之后,才会给出网络的实际输出。