问题描述
ModelCheckPoint
提供了分别为val_Acc
和val_loss
保存的选项.我想以某种方式修改它,以便val_acc
在改进->保存模型.如果val_acc
等于先前的最佳val_acc
,则检查val_loss
,如果val_loss
小于先前的最佳val_loss
,则保存模型.
ModelCheckPoint
gives options to save both for val_Acc
and val_loss
separately. I want to modify this in a way so that if val_acc
is improving -> save model. if val_acc
is equal to previous best val_acc
then check for val_loss
, if val_loss
is less than previous best val_loss
then save the model.
if val_acc(epoch i)> best_val_acc:
save model
else if val_acc(epoch i) == best_val_acc:
if val_loss(epoch i) < best_val_loss:
save model
else
do not save model
推荐答案
您可以添加两个回调:
callbacks = [ModelCheckpoint(filepathAcc, monitor='val_acc', ...),
ModelCheckpoint(filepathLoss, monitor='val_loss', ...)]
model.fit(......., callbacks=callbacks)
使用自定义回调
您可以在LambdaCallback(on_epoch_end=saveModel)
中做任何您想做的事情.
Using custom callbacks
You can do anything you want in a LambdaCallback(on_epoch_end=saveModel)
.
best_val_acc = 0
best_val_loss = sys.float_info.max
def saveModel(epoch,logs):
val_acc = logs['val_acc']
val_loss = logs['val_loss']
if val_acc > best_val_acc:
best_val_acc = val_acc
model.save(...)
elif val_acc == best_val_acc:
if val_loss < best_val_loss:
best_val_loss=val_loss
model.save(...)
callbacks = [LambdaCallback(on_epoch_end=saveModel)]
但这与具有val_acc
的单个ModelCheckpoint
没什么不同.除非您使用的样本很少,或者您的自定义准确性相差不大,否则您将不会真正获得相同的准确性.
But this is nothing different from a single ModelCheckpoint
with val_acc
. You won't really be getting identical accuracies, unless you're using very few samples, or you have a custom accuracy that doesn't vary much.
这篇关于如何在keras中修改ModelCheckPoint以同时监视val_acc和val_loss并相应地保存最佳模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!