对模型进行早停
早停回调非常实用,它可以在经过指定数量的轮次后,若指标不再提升,则停止模型训练。要使用该功能,您需要在 .fit() 方法中,将回调以列表形式传给模型的回调参数。
您之前构建用于识别假美元的 model 已为您加载,这次将配合早停进行训练。X_train、y_train、X_test 和 y_test 也已准备好供您使用。
本练习是课程的一部分
Keras 深度学习入门
练习说明
- 从
tensorflow.keras.callbacks导入EarlyStopping回调。 - 定义一个回调,监控
'val_accuracy',并将patience设为 5 个轮次。 - 使用早停回调训练您的模型。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the early stopping callback
from tensorflow.____.____ import ____
# Define a callback to monitor val_accuracy
monitor_val_acc = ____(monitor=____,
patience=____)
# Train your model using the early stopping callback
model.____(____, ____,
epochs=1000, validation_data=____,
callbacks= ____)