【问题标题】:tensorflow confusion matrix in Experimenter during evaluation评估期间 Experimenter 中的张量流混淆矩阵
【发布时间】:2018-03-01 18:28:45
【问题描述】:

我在使用 Tensorflow 和 Experimenter API 进行模型评估时遇到了一些麻烦。

我以前使用 2-classes NN 工作,但这次我设法训练了 4-classes 神经网络,我需要弄清楚在这种情况下如何构建混淆矩阵。我尝试使用tf.confusion_matrix 函数,但它根本不起作用。

这是我使用的代码片段:

if mode == ModeKeys.EVAL:

    eval_metric_ops = {
        'accuracy' : metrics.streaming_accuracy(predictions=predicted_classes, labels=labels),

        # Other metrics...

        'confusion_matrix': tf.confusion_matrix(prediction=predicted_classes, label=labels, num_classes=4)
    }

    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=predicted_classes,
        loss=loss,
        eval_metric_ops=eval_metric_ops
    )

这是我得到的错误:

TypeError: Values of eval_metric_ops must be (metric_value, update_op) tuples, given: (<tf.Operation 'test/group_deps' type=NoOp>, <tf.Tensor 'test/accuracy/value:0' shape=() dtype=float32>, <tf.Variable 'test/confusion:0' shape=(4, 4) dtype=int32_ref>) for key: confusion_matrix

我阅读了有关在 Tensorflow 中创建混淆矩阵的其他答案,并且我了解如何执行此操作,但我认为我的问题与 Estimator/Experimenter API 更相关。

【问题讨论】:

  • 我也有同样的问题!

标签: python tensorflow neural-network confusion-matrix


【解决方案1】:

您的代码不起作用,因为框架期望 eval_metric_ops 是一个字典,其中包含具有操作名称和元组类型值的键(结果张量,此张量的更新操作)

tf.confusion_matrix(prediction=predicted_classes, label=labels, num_classes=4) 只返回预期的张量。

您必须像这样实现自己的指标操作:

def eval_confusion_matrix(labels, predictions):
    with tf.variable_scope("eval_confusion_matrix"):
        con_matrix = tf.confusion_matrix(labels=labels, predictions=predictions, num_classes=4)

        con_matrix_sum = tf.Variable(tf.zeros(shape=(4,4), dtype=tf.int32),
                                            trainable=False,
                                            name="confusion_matrix_result",
                                            collections=[tf.GraphKeys.LOCAL_VARIABLES])


        update_op = tf.assign_add(con_matrix_sum, con_matrix)

        return tf.convert_to_tensor(con_matrix_sum), update_op



# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {
    "accuracy": tf.metrics.accuracy(labels, predicted_classes),
    "conv_matrix": eval_confusion_matrix(
        labels, predicted_classes)
    }

【讨论】:

    【解决方案2】:

    请使用 tf.metrics.* 或 tf.contrib.metrics.* 进行度量计算。编写正确的度量行为有一些微妙的事情。 tf.contrib.metrics 中有一些实用程序,例如您可能会发现有用的 streaming_true_positives。

    【讨论】:

    • 我知道,但是如果你有 4 个班级,streaming_true_positives 并不是很有用,因为它只显示了整体的真实情况。
    猜你喜欢
    • 2018-07-05
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2021-03-17
    • 2017-08-20
    • 2020-12-30
    • 2018-03-18
    相关资源
    最近更新 更多