【问题标题】:TensorFlow metric: top-N accuracyTensorFlow 指标:top-N 准确率
【发布时间】:2019-04-18 21:24:33
【问题描述】:

我正在使用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 ops实现的。我怎样才能实现前 3 名的准确性?

【问题讨论】:

    标签: python tensorflow metrics top-n


    【解决方案1】:

    如果你想自己实现它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_accuracy
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2021-12-19
      • 2016-10-06
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      相关资源
      最近更新 更多