開始使用免費開始

history 回呼函式

每次使用 .fit() 方法訓練模型時,預設都會回傳一個 history 回呼函式。你可以在回傳的 h_callback 物件中,透過 history 字典參數與對應的鍵來存取這些評估指標。

上一節你建立的灌溉機器 model 已為你載入,可直接訓練;其特徵與標籤也分別載為 X_trainy_trainX_testy_test。 這次你會在訓練時儲存模型的 history 回呼,並使用 validation_data 參數。

你將使用兩個簡單的 matplotlib 函式 plot_accuracy()plot_loss(),將 history 中的結果繪圖顯示。 你可以在主控台貼上 show_code(plot_loss) 檢視其程式碼。

一起來看看訓練過程的幕後細節!

本練習屬於課程

Keras 深度學習入門

檢視課程

練習說明

  • 使用 X_trainy_train 訓練模型,並在每個 epoch 以 X_testy_test 做驗證。
  • 使用 plot_loss,從 h_callback 擷取 lossval_loss
  • 使用 plot_accuracy,從 h_callback 擷取 accuracyval_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[____])
編輯並執行程式碼