[AI] 2-1. 初試神經網路-手寫辨識 mnist
MNIST 是經典的手寫數字圖片資料集,已經內建在 tensorflow 裡面,這個資料集可以相當於是深度學習的 “Hello World”,是由美國國家標準與技術研究院(National Institute of Standard and Technology) 所提供。 1. 認識 MNIST 資料集 透過 tensorflow 引入資料集 from tensorflow.keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() 查看資料的大小 train_images.shape len(train_labels) test_images.shape test_labels min(test_labels), max(test_labels) 結果 (60000, 28, 28) # 代表 train_images 是 60000 張 28*28 的圖片 60000 # 代表 train_labels 同樣也有 60000 份 (10000, 28, 28) # 代表 test_images 有 10000 張 28*28 的圖片 array([7, 2, 1, ..., 4, 5, 6], dtype=uint8) (0, 9) # 代表 test_labels 是 0-9 的數字,資料型別是 uint8 2. 神經網路架構 接下來的操作流程是: 將測資 train_images 和 train_labels 餵給神經網路 神經網路學習分類圖片,與每張圖片的標籤對比,分類錯誤就修正(學習) 最後對 test_images 進行預測,並驗證結果看是否與 test_labels 吻合 from tensorflow import keras from tensorflow.keras import layers model = keras.Sequential([ layers.Dense(512, activation="relu") layers.Dense(10, activation="softmax") ]) 組成神經網路的基本元件為層(layer),一個層就是一個資料處理的模組。可以視之為資料的過濾器。具體而言,每一層都會從資料中萃取出特定的轉換或是表示法(representation),這些特定的表示法會有助於解決某些問題。大多數深度學習模型會將許多層連接在一起,漸次執行資料萃取(data distillation)。 在範例中,神經網路由兩個密集層(Dense layers)緊密連接組成,密集層也稱為全連接(fully connected) 神經層。第二個密集層是有 10 個輸出的 softmax 層,最終會輸出 10 個機率評分的陣列,每個評分就是對應到每一個數字的機率。 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 為了讓神經網路接受訓練,還需要準備三個元件才能進行編譯。 ...