history 回呼函式
每次使用 .fit() 方法訓練模型時,預設都會回傳一個 history 回呼函式。你可以在回傳的 h_callback 物件中,透過 history 字典參數與對應的鍵來存取這些評估指標。
上一節你建立的灌溉機器 model 已為你載入,可直接訓練;其特徵與標籤也分別載為 X_train、y_train、X_test、y_test。
這次你會在訓練時儲存模型的 history 回呼,並使用 validation_data 參數。
你將使用兩個簡單的 matplotlib 函式 plot_accuracy() 與 plot_loss(),將 history 中的結果繪圖顯示。
你可以在主控台貼上 show_code(plot_loss) 檢視其程式碼。
一起來看看訓練過程的幕後細節!
本練習屬於課程
Keras 深度學習入門
練習說明
- 使用
X_train與y_train訓練模型,並在每個 epoch 以X_test與y_test做驗證。 - 使用
plot_loss,從h_callback擷取loss與val_loss。 - 使用
plot_accuracy,從h_callback擷取accuracy與val_accuracy。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Train your model and save its history
h_callback = ____.____(____, ____, epochs = 25,
validation_data=(____, ____))
# Plot train vs test loss during training
plot_loss(h_callback.history[____], h_callback.history[____])
# Plot train vs test accuracy during training
plot_accuracy(h_callback.history[____], h_callback.history[____])