學習手寫數字
你要在 digits 資料集 上建立模型。這是 scikit-learn 預先載入的範例資料集。digits 資料集包含 8x8 像素、標記為 0 到 9 的手寫數字:
資料集已經分割為 X_train、y_train、X_test 和 y_test,其中 30% 的資料做為測試資料。標籤已是一個 one-hot 編碼向量,所以你不需要再使用 Keras 的 to_categorical() 函式。
現在就來建立這個新的 model 吧!
本練習屬於課程
Keras 深度學習入門
練習說明
- 新增一個含 16 個神經元、
relu啟用函式的Dense層,並設定input_shape為 8x8 數字影像的「總像素數」。 - 再新增一個具有 10 個輸出、
softmax啟用函式的Dense層。 - 使用
adam、categorical_crossentropy與accuracy指標來編譯模型。 - 以
X_train做預測,確認模型可正常運作。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Instantiate a Sequential model
model = Sequential()
# Input and hidden layer with input_shape, 16 neurons, and relu
model.add(Dense(____, input_shape = (____,), activation = ____))
# Output layer with 10 neurons (one per digit) and softmax
model.____(____)
# Compile your model
model.____(optimizer = ____, loss = ____, metrics = [____])
# Test if your model is well assembled by predicting before training
print(model.predict(____))