開始使用免費開始

學習手寫數字

你要在 digits 資料集 上建立模型。這是 scikit-learn 預先載入的範例資料集。digits 資料集包含 8x8 像素、標記為 0 到 9 的手寫數字

給定一張影像,你想要分辨 10 個可能的數字,因此這是一個多類別分類問題。

資料集已經分割為 X_trainy_trainX_testy_test,其中 30% 的資料做為測試資料。標籤已是一個 one-hot 編碼向量,所以你不需要再使用 Keras 的 to_categorical() 函式。

現在就來建立這個新的 model 吧!

本練習屬於課程

Keras 深度學習入門

檢視課程

練習說明

  • 新增一個含 16 個神經元、relu 啟用函式的 Dense 層,並設定 input_shape 為 8x8 數字影像的「總像素數」。
  • 再新增一個具有 10 個輸出、softmax 啟用函式的 Dense 層。
  • 使用 adamcategorical_crossentropyaccuracy 指標來編譯模型。
  • 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(____))
編輯並執行程式碼