如何在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.
我为什么要回答自己的问题?检查此..