【问题标题】:Using TensorFlow to classify Handwritten Digits without validation labels使用 TensorFlow 对没有验证标签的手写数字进行分类
【发布时间】:2016-03-03 19:03:08
【问题描述】:

我一直在阅读 TensorFlow 教程,并在一般情况下阅读机器学习。

据我了解,使用神经网络的主要好处之一是它们能够在训练后对呈现的输入进行快速分类。

首先,我开始逐步浏览示例代码并查看训练数据的结构,我能够成功地使用基本示例(91% 的准确率)来识别我使用创建的图像(仅数字)以下代码sn -p:

# Training is already done using the code from the tutorial 
# Do the same for five
...
test_five_image = np.zeros((28, 28), dtype=np.uint8, order='C')
for five_coords in npg.five_coordinates:
    i = int(five_coords[0] / 28)
    j = int((five_coords[1] / 28) + 3) # By Eye Centering
    test_five_image[i][j] = 0xFF
test_five_image = np.rot90(test_five_image, 1)
Image.fromarray(np.uint8(test_five_image)).save(str(5) + '.bmp') 
...
# Images are Four, Five, 0 and 6
test_labels = input_data.dense_to_one_hot(np.array([4, 5, 0, 6], np.int32))
dataset = input_data.DataSet(test_images, test_labels)
print sess.run(accuracy, feed_dict={x: dataset.images, y_: dataset.labels})

由上述代码生成的图像示例: Bitmap extracted from test data used.

注意: 此图像由点列表构建,然后按比例缩小以适合 28 * 28 数组。颜色是反转的,因为图像只是从 numpy 数组直接转换为位图。根据 MNIST 文件格式,列表中的每个点都设置为 0xFF,其中 0 为白色,255 为黑色

上面的这个 sn-p 输出 1.0(有时是 0.75,取决于训练的准确度),它正确地对标签的输入进行分类。

所以我的问题是,使用 TensorFlow 构建的神经网络对输入进行简单分类,例如,如果输入是“7”,那么输出将是:

>>> [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]

我查看了 TensorFlow 文档,但无法提出解决方案。我怀疑这可能是因为在教程中遗漏了一些东西。

谢谢

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    假设你已经关注MNIST for ML Beginners Tutorial,为了得到一个简单的预测,像这样添加一个 argmax 节点:

    prediction = tf.argmax(y, 1)
    

    然后运行它,输入您想要分类的数据:

    prediction_val = sess.run(prediction, feed_dict={x: dataset.images})
    

    prediction_val 的形状为 (batch_size,),并包含批次中每个图像的最可能标签。

    注意feed_dict 只包括x 而不是y_,因为prediction 不依赖于y_

    【讨论】:

      猜你喜欢
      • 2013-01-14
      • 2017-06-03
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2018-07-04
      • 2014-04-30
      • 2021-08-25
      相关资源
      最近更新 更多