使用交叉驗證進行訓練
該用找到的最佳參數來訓練你的模型了:learning rate 為 0.001、50 個 epochs、batch_size 為 128,以及 relu activation。
上一個練習中的 create_model() 函式已可直接使用。X 與 y 已載入為特徵與標籤。
在建立 KerasClassifier 物件時請帶入這些最佳參數,讓它們能在進行 cross_validation 時生效。
用這些調過參數的強大模型,在 breast cancer 資料集 上完成本章吧!
本練習屬於課程
Keras 深度學習入門
練習說明
- 從
tensorflow.keras的 scikit_learn 包裝器匯入KerasClassifier。 - 以找到的最佳參數建立一個
KerasClassifier物件。 - 將你的
model、特徵與標籤傳入cross_val_score,以 3 折進行交叉驗證。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import KerasClassifier from tensorflow.keras wrappers
from tensorflow.keras.wrappers.____ import ____
# Create a KerasClassifier
model = ____(build_fn = create_model(learning_rate = ____, activation = ____), epochs = ____,
batch_size = ____, verbose = 0)
# Calculate the accuracy score for each fold
kfolds = cross_val_score(____, ____, ____, cv = ____)
# Print the mean accuracy
print('The mean accuracy was:', kfolds.mean())
# Print the accuracy standard deviation
print('With a standard deviation of:', kfolds.std())