我正在使用add_metric尝试创建一个自定义指标,用于计算分类器的前3个准确率。这就是我所得到的:
def custom_metrics(labels, predictions):
# labels => Tensor("fifo_queue_DequeueUpTo:422", shape=(?,), dtype=int64)
# predictions => {
# 'logits': <tf.Tensor 'dnn/logits/BiasAdd:0' shape=(?, 26) dtype=float32>,
# 'probabilities': <tf.Tensor 'dnn/head/predictions/probabilities:0' shape=(?, 26) dtype=float32>,
# 'class_ids': <tf.Tensor 'dnn/head/predictions/ExpandDims:0' shape=(?, 1) dtype=int64>,
# 'classes': <tf.Tensor 'dnn/head/predictions/str_classes:0' shape=(?, 1) dtype=string>
# }看看现有tf.metrics的实现,一切都是使用tf操作实现的。我如何才能实现前3名的准确率?
发布于 2019-04-19 06:07:59
如果你想自己实现它,tf.nn.in_top_k是非常有用的-它返回一个布尔数组,表明目标是否在前k个预测之内。你只需要取结果的平均值:
def custom_metrics(labels, predictions):
return tf.metrics.mean(tf.nn.in_top_k(predictions=predictions, targets=labels, k=3))您还可以导入它:
from tf.keras.metrics import top_k_categorical_accuracyhttps://stackoverflow.com/questions/55753916
复制相似问题