使用交叉验证进行训练
现在请用已找到的最佳参数来训练模型:学习率 0.001、训练 50 轮 (epochs)、batch_size 为 128,以及 relu 激活函数。
前一个练习中的 create_model() 函数已为您准备好可直接使用。X 和 y 分别已载入为特征和标签。
在创建 KerasClassifier 对象时,请使用为您的模型找到的最佳取值,以便在执行 cross_validation 时生效。
以在 乳腺癌数据集 上训练一个经过精调的优秀模型来结束本章!
本练习是课程的一部分
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())