因為 MNIST 提供手寫數字的是 28×28 的灰階圖檔,每一個 pixel 介於 0~255 間, 我們要送到類神經網路裡計算需要先正規化 0~1 的數值

# 6萬張 traning 轉成一個 array
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

tensorflow.keras.utils.to_categorical(train_labels) # one-hot encoding
tensorflow.keras.utils.to_categorical(test_labels) # one-hot encoding

因為這是一個輸出成0~9的類別 (multiclass classification problem),這裡要先用 one-hot encoding 作資料前處理,最後一層因為要輸出 10 個類別,所以用了 10 個節點的 softmax

完整程式碼

from tensorflow.keras.datasets import mnist
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()


# pixel values 0~255 資料為 0~1
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

print(train_images.shape[0], 'train size')
print(test_images.shape[0], 'test size')

# one-hot encoding
print('before one hot encoding: ', train_labels[:4])
train_labels = to_categorical(train_labels) 
test_labels = to_categorical(test_labels)
print('after one hot encoding: ', train_labels[:4])

num_pixels = 28*28 
num_classes = 10

network = models.Sequential()
network.add(layers.Dense(num_pixels, activation='relu', input_shape=(num_pixels, ), name="hiddenlayer")) 
network.add(layers.Dense(num_classes, activation='softmax', name="outputlayer")) 

network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])


# Fit the neural network (iterate on the training data)
# each iteration over all the training data is called an epoch
network.fit(train_images, train_labels, epochs=5, batch_size=128)

#
# Evaluate the network performance
#
scores = network.evaluate(test_images, test_labels) 
print("\nscore: %.2f%%\n" % (scores[1]*100))

輸出

60000 train size
10000 test size
before one hot encoding:  [5 0 4 1]
after one hot encoding: 
[[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]
Metal device set to: Apple M1

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

Epoch 1/5
469/469 [==============================] - 4s 7ms/step - loss: 0.2395 - accuracy: 0.9294
Epoch 2/5
469/469 [==============================] - 3s 7ms/step - loss: 0.0950 - accuracy: 0.9712
Epoch 3/5
469/469 [==============================] - 3s 7ms/step - loss: 0.0613 - accuracy: 0.9813
Epoch 4/5
469/469 [==============================] - 3s 7ms/step - loss: 0.0433 - accuracy: 0.9871
Epoch 5/5
469/469 [==============================] - 3s 7ms/step - loss: 0.0317 - accuracy: 0.9905
313/313 [==============================] - 2s 5ms/step - loss: 0.0624 - accuracy: 0.9817

score: 98.17%