簡體   English   中英

使用TensorFlow對手寫數字進行分類,無需驗證標簽

[英]Using TensorFlow to classify Handwritten Digits without validation labels

我一直在閱讀TensorFlow教程並總體閱讀機器學習。

我的理解是,使用神經網絡的一個主要好處是它們能夠在訓練后快速對所呈現的輸入進行分類。

首先,我開始逐步完成示例代碼並查看訓練數據的結構,並且我能夠成功使用基本示例(91%准確度)來識別我使用以下代碼創建的圖像(僅限數字)片段:

# 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})

從上面的代碼生成的圖像示例: 從使用的測試數據中提取的位圖。

注意: 此圖像是根據點列表構建的,然后按比例縮小以適合28 * 28陣列。 由於圖像直接從numpy數組轉換為位圖,因此顏色會反轉。 根據MNIST文件格式,列表中的每個點都設置為0xFF,其中0為白色,255為黑色

上面的代碼片段輸出1.0 (有時為0.75具體取決於培訓的准確性),並正確地將輸入分類為標簽。

所以我的問題是使用TensorFlow構建的神經網絡簡單地對輸入進行分類所涉及的必要步驟是什么,例如,如果輸入是'7',輸出將是例如:

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

我查看了TensorFlow文檔,但我無法提出解決方案。 我懷疑這可能是因為缺少教程中的內容。

謝謝

假設您已經遵循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_

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM