如何基于AUC指标在Keras中保存最佳模型? [英] How to save best model in Keras based on AUC metric?
问题描述
我想基于auc在Keras中保存最佳模型,并且我有以下代码:
I would like to save the best model in Keras based on auc and I have this code:
def MyMetric(yTrue, yPred):
auc = tf.metrics.auc(yTrue, yPred)
return auc
best_model = [ModelCheckpoint(filepath='best_model.h5', monitor='MyMetric', save_best_only=True)]
train_history = model.fit([train_x],
[train_y], batch_size=batch_size, epochs=epochs, validation_split=0.05,
callbacks=best_model, verbose = 2)
因此我的模型运行不正常,我收到此警告:
SO my model runs nut I get this warning:
RuntimeWarning: Can save best model only with MyMetric available, skipping.
'skipping.' % (self.monitor), RuntimeWarning)
如果能告诉我这是正确的方法,那是很好的,否则我该怎么办?
It would be great if any can tell me this is the right way to do it and if not what should I do?
推荐答案
您必须将要监视的指标传递给model.compile.
You have to pass the Metric you want to monitor to model.compile.
https://keras.io/metrics/#custom-metrics >
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[MyMetric])
此外,tf.metrics.auc返回一个包含张量和update_op的元组. Keras希望自定义指标功能仅返回张量.
Also, tf.metrics.auc returns a tuple containing the tensor and update_op. Keras expects the custom metric function to return only a tensor.
def MyMetric(yTrue, yPred):
import tensorflow as tf
auc = tf.metrics.auc(yTrue, yPred)
return auc[0]
在此步骤之后,您将获得有关未初始化值的错误.请查看以下主题:
After this step, you will get errors about uninitialized values. Please see these threads:
https://github.com/keras-team/keras/issues/3230
这篇关于如何基于AUC指标在Keras中保存最佳模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!