【问题标题】:Implementing an accuracy metric in TensorFlow在 TensorFlow 中实现准确度指标
【发布时间】:2016-12-15 22:27:39
【问题描述】:

我正在关注 TensorFlow 文档中的示例,特别是 example 1,其中的指标分配如下:

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

我想知道:如何实现不同的准确度指标。例如 MAP(平均平均精度)。假设我有一个函数:

import numpy as np

def accuracyMAP(y_pred, y_real):

    def __precision_at_k(r, k):   
        r = np.asarray(r)[:k] != 0
        return np.mean(r)

    temp_sorted = y_real[np.argsort(-y_pred)]
    till = np.where(temp_sorted==1)
    r = temp_sorted[:till[0]+1]
    return __precision_at_k(r, len(r))

一种方法是进行预测并将其传递给mean_precision_scorce 函数:

for batch_xs in x:
    y_pred = sess.run(y, feed_dict={x: batch_xs})
    acc = accuracyMAP(y_true, y_pred)

y_true 和 y_predicted 都转换为 numpy 数组。但是有没有办法与 Tensorflow 的示例类似?有什么建议吗?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    如果您的 mean_precision_score 函数可以在 TF 张量上运行,您可以简单地执行以下操作:acc = mean_precision_score(y_true, y) 和更高版本的 sess.run(acc, ...)

    【讨论】:

    • 你的意思是score = accuracy(y_true,y); mean_accuracy = tf.reduce_mean(score) 我收到了一个错误Input 'strides' of 'StridedSlice' Op has type int32 that does not match type int64 of argument 'begin'。也尝试使用tf.cast,但没有找到通过它的方法。
    • 您需要显示accuracy 函数的代码,以便了解错误的来源。
    猜你喜欢
    • 2021-08-02
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2018-06-15
    • 2020-04-04
    相关资源
    最近更新 更多