使用 Keras 的評估指標與驗證
上一個練習裡我們訓練了一個模型來預測手語字母,但成效其實不明確。本練習要提升結果的可解釋性。由於當時沒有使用驗證切分,我們只看到訓練集內的效能提升;然而,其中有多少來自過度擬合並不清楚。此外,因為沒有指定評估指標,我們只看到損失函式下降,但無法清楚解讀其意義。
注意:keras 已從 tensorflow 匯入供你使用。
本練習屬於課程
Python 的 TensorFlow 入門
練習說明
- 將第一個全連接層設為 32 個節點,使用
sigmoid啟用函式,輸入形狀為 (784,)。 - 使用根均方傳播最佳化器、類別交叉熵損失,以及 accuracy 評估指標。
- 訓練 10 個 epochs,並使用資料集的 10% 作為驗證。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define sequential model
model = keras.Sequential()
# Define the first layer
model.add(keras.layers.Dense(____, ____, ____))
# Add activation function to classifier
model.add(keras.layers.Dense(4, activation='softmax'))
# Set the optimizer, loss function, and metrics
model.compile(optimizer='____', loss='____', metrics=['____'])
# Add the number of epochs and the validation split
model.fit(sign_language_features, sign_language_labels, epochs=____, validation_split=____)