【问题标题】:TensorFlow: Is there a metric to calculate and update top k accuracy?TensorFlow:是否有计算和更新 top k 准确率的指标?
【发布时间】:2017-06-28 10:07:39
【问题描述】:

目前tf.contrib.metrics.streaming_accuracy只能计算top 1的准确率,不能计算top k。作为一种解决方法,这是我一直在使用的:

tf.reduce_mean(tf.cast(tf.nn.in_top_k(predictions=predictions, targets=labels, k=5), tf.float32))

但是,这并没有给我一种计算每个批次的平均流准确度的方法,这对于获得稳定的评估准确度很有用。我目前正在通过使用其 numpy 输出手动计算此流式前 5 名精度,但这意味着我将无法在 tensorboard 上可视化此指标。

有没有办法通过创建一个 accuracy_update 函数来实现更简单的实现,或者是否有一个现有的函数已经这样做了?

谢谢。

【问题讨论】:

    标签: machine-learning tensorflow deep-learning metrics


    【解决方案1】:

    您可以将您对tf.contrib.metrics.streaming_accuracy 的使用替换为较低级别的tf.metrics.mean,这是streaming_accuracy 最终使用的方式——您会在它们各自的文档中找到相似之处。

    例如(未测试)

    tf.metrics.mean(tf.nn.in_top_k(predictions=predictions, targets=labels, k=5))
    

    【讨论】:

    • 感谢您的建议!该功能完全按预期工作。
    • 您的预测和标签是什么数据类型和形状?我也在尝试使用这个函数,但我得到一个 InvalidArgumentError 说 targets[0] is out of range..
    • 没关系,找到问题了(我使用 argmax(logits) 作为“预测”而不是 logits 本身)
    【解决方案2】:

    对于每批次的 top-k 准确度,这也有效。

    k_val=3
    accs = []
    for each_bach in range(batch_size):
        acc = tf.keras.metrics.top_k_categorical_accuracy(y_true=tf_class1[each_bach], y_pred=tf_class2[each_bach], k=k_val)
        accs.append(acc)
    
    acc_data_per_batch = tf.reduce_mean(accs)
    

    tf.keras.metrics.top_k_categorical_accuracy 返回 K.mean( nn.in_top_k(y_pred, math_ops.argmax(y_true, axis=-1), k), axis=-1) 每批

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2021-08-02
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多