如何在Keras中将ModelCheckpoint与自定义指标一起使用? [英] How to use ModelCheckpoint with custom metrics in Keras?
本文介绍了如何在Keras中将ModelCheckpoint与自定义指标一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在ModelCheckpoint
回调?
Is it possible to use custom metrics in the ModelCheckpoint
callback?
推荐答案
是的,有可能.
按照文档中的说明定义自定义指标:
Define the custom metrics as described in the documentation:
import keras.backend as K
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
要检查所有可用的指标,请执行以下操作:
To check all available metrics:
print(model.metrics_names)
> ['loss', 'acc', 'mean_pred']
通过monitor
将度量标准名称传递给ModelCheckpoint
.如果要在验证中计算指标,请使用val_
前缀.
Pass the metric name to ModelCheckpoint
through monitor
. If you want the metric calculated in the validation, use the val_
prefix.
ModelCheckpoint(weights.{epoch:02d}-{val_mean_pred:.2f}.hdf5,
monitor='val_mean_pred',
save_best_only=True,
save_weights_only=True,
mode='max',
period=1)
请勿将mode='auto'
用于自定义指标.理解为什么此处.
Don't use mode='auto'
for custom metrics. Understand why here.
我为什么要回答自己的问题?检查此..
这篇关于如何在Keras中将ModelCheckpoint与自定义指标一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文