【问题标题】:How to calculate the accuracy for multilabel classification with tf.metrics?如何使用 tf.metrics 计算多标签分类的准确率?
【发布时间】:2018-10-16 09:47:44
【问题描述】:

我想用 tensorflow (tf.estimator.Estimator) 训练多标签分类模型。我需要在评估时输出准确性。但它似乎不适用于以下代码:

accuracy = tf.metrics.accuracy(labels=labels, predictions=preds)
metrics = {'accuracy': accuracy}

if mode == tf.estimator.ModeKeys.EVAL:
    return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

tf.metrics.accuracy 不适用于 multihot 结果。那么什么是多标签指标?

【问题讨论】:

    标签: tensorflow evaluation tensorflow-estimator


    【解决方案1】:

    实际上tf.metrics.accuracy 也计算了多标签分类的准确率。请看下面的例子:

    import tensorflow as tf
    
    labels = tf.constant([[1, 0, 0, 1],
                          [0, 1, 1, 1],
                          [1, 1, 0, 0],
                          [0, 0, 0, 1],
                          [1, 1, 0, 0]])
    
    preds = tf.constant([[1, 0, 1, 1],
                         [0, 1, 1, 1],
                         [1, 1, 0, 0],
                         [0, 0, 0, 1],
                         [1, 1, 0, 0]])
    
    acc, acc_op = tf.metrics.accuracy(labels, preds)
    
    with tf.Session() as sess:
        sess.run(tf.local_variables_initializer())
        sess.run(tf.global_variables_initializer())
        print(sess.run([acc, acc_op]))
        print(sess.run([acc]))
    

    如您所见,我们总共有 20 个标签,第一行中只有一个条目被错误标记,因此我们的准确率为 0.95%。

    【讨论】:

    • module 'tensorflow.keras.metrics' has no attribute 'accuracy'
    猜你喜欢
    • 2018-11-14
    • 2019-03-23
    • 1970-01-01
    • 2018-07-20
    • 2018-02-09
    • 2020-06-08
    • 1970-01-01
    • 2015-10-03
    • 2021-09-03
    相关资源
    最近更新 更多