在 Keras 中使用 EarlyStopping 回调时如何获得最佳模型? [英] How to get the best model when using EarlyStopping callback in Keras?
问题描述
我正在使用基于 val_acc
和 patience=0
的 EarlyStopping
使用 Keras 训练神经网络.EarlyStopping
在 val_acc
减小后立即停止训练.
I am training a neural network with Keras using EarlyStopping
based on val_acc
and patience=0
. EarlyStopping
stops the training as soon as val_acc
decreases.
然而,我得到的最终模型并不是最好的模型,即 val_acc
最高的模型.但我宁愿有一个对应于之后 epoch 的模型,即对应于 val_acc
的模型比最好的略低一点,这导致了提前停止!
However the final model that I obtain is not the best model, namely the one with the highest val_acc
. But I rather have the model corresponding to the epoch after, namely the one corresponding to a val_acc
just a bit lower than the best one and that caused the early stopping!
我如何获得最好的?
我尝试使用使用回调保存最佳模型:
I tried to use the save the best model using the call back:
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
但我得到了相同的结果.
But I get the same results.
推荐答案
如果您想保存最高准确度,那么您应该设置检查点 monitor='val_acc'
它会自动保存在最高准确度.最低损失不一定对应最高准确度.您还可以设置 verbose=1
以查看正在保存的模型以及原因.
If you would like to save the highest accuracy then you should set the checkpoint monitor='val_acc'
it will automatically save on highest. Lowest loss might not necessarily correspond to highest accuracy. You can also set verbose=1
to see which model is being saved and why.
这篇关于在 Keras 中使用 EarlyStopping 回调时如何获得最佳模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!