代码之家  ›  专栏  ›  技术社区  ›  Samuele Colombo

评估期间实验者的张量流混淆矩阵

  •  3
  • Samuele Colombo  · 技术社区  · 8 年前

    在使用Tensorflow和实验API进行模型评估的过程中,我遇到了一些问题。

    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中创建混淆矩阵的答案,我理解了如何创建混淆矩阵,但我认为我的问题与估计器/实验者API更相关。

    2 回复  |  直到 8 年前
        1
  •  6
  •   monchi    8 年前

    tf。混淆矩阵(预测=预测的类,标签=标签,num\u类=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
  •  0
  •   user1454804    8 年前

    请使用任一tf。指标。*或tf。contrib。指标。*用于公制计算。有一些微妙的事情可以写一个currect度量行为。