callbacks可以用来做这些事情:
- 模型断点续训:保存当前模型的所有权重
- 提早结束:当模型的损失不再下降的时候就终止训练,当然,会保存最优的模型。
- 动态调整训练时的参数,比如优化的学习速度。
earlystopping
# Callbacks are passed to the model fit the `callbacks` argument in `fit`,
# which takes a list of callbacks. You can pass any number of callbacks.
callbacks_list = [
# This callback will interrupt training when we have stopped improving
keras.callbacks.EarlyStopping(
# This callback will monitor the validation accuracy of the model
monitor='acc',
# Training will be interrupted when the accuracy
# has stopped improving for *more* than 1 epochs (i.e. 2 epochs)
patience=1,
)
modelcheckpoint
# This callback will save the current weights after every epoch
keras.callbacks.ModelCheckpoint(
filepath='my_model.h5', # Pa