Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tf.估计器设置中使用tf.metrics.precision/call计算F1分数_Python_Tensorflow_Tensorflow Estimator - Fatal编程技术网

Python 在tf.估计器设置中使用tf.metrics.precision/call计算F1分数

Python 在tf.估计器设置中使用tf.metrics.precision/call计算F1分数,python,tensorflow,tensorflow-estimator,Python,Tensorflow,Tensorflow Estimator,我试图在tf.Estimator设置中计算F1分数 我见过这个,但不能从中提取出有效的解决方案 tf.Estimator的问题是它希望我提供一个值和一个更新op,所以现在,我在我的模型末尾有一段代码: if mode == tf.estimator.ModeKeys.EVAL: with tf.variable_scope('eval'): precision, precision_update_op = tf.metrics.precision(labels=label

我试图在
tf.Estimator
设置中计算F1分数

我见过这个,但不能从中提取出有效的解决方案

tf.Estimator
的问题是它希望我提供一个值和一个更新op,所以现在,我在我的模型末尾有一段代码:

if mode == tf.estimator.ModeKeys.EVAL:
    with tf.variable_scope('eval'):
        precision, precision_update_op = tf.metrics.precision(labels=labels,
                                            predictions=predictions['class'],
                                            name='precision')

        recall, recall_update_op = tf.metrics.recall(labels=labels,
                                      predictions=predictions['class'],
                                      name='recall')

        f1_score, f1_update_op = tf.metrics.mean((2 * precision * recall) / (precision + recall), name='f1_score')

        eval_metric_ops = {
            "precision": (precision, precision_update_op),
            "recall": (recall, recall_update_op),
            "f1_score": (f1_score, f1_update_op)}
现在,精确性和召回率似乎很好,但在F1成绩上,我一直得到
nan

我该怎么做才能让它发挥作用呢

编辑:

使用
tf.contrib.metrics.f1_score
可以实现一个有效的解决方案,但是由于
contrib
将在TF2.0中被弃用,我希望使用
contrib
更少的解决方案1)为什么要使用
tf.metrics.mean
?召回率和精度是标量值

2) 您是否尝试过打印
f1\u分数
f1\u更新操作

3) 从他们提到的

为了估计数据流上的度量值,该函数创建一个update_op来更新这些变量并返回回调。通过权重中的相应值更新每个预测的_op权重


由于您直接从处理更新的两个操作中获得F1分数,请尝试执行tf.identity(这实际上不会导致任何更改)

F1值张量可以从精度和召回值计算得出。度量必须是(value,update_op)元组。我们可以通过f1的tf.identity。这对我很有用:

import tensorflow as tf

def metric_fn(labels, logits):
    predictions = tf.argmax(logits, axis=-1)
    pr, pr_op = tf.metrics.precision(labels, predictions)
    re, re_op = tf.metrics.recall(labels, predictions)
    f1 = (2 * pr * re) / (pr + re)
    return {
        'precision': (pr, pr_op),
        'recall': (re, re_op),
        'f1': (f1, tf.identity(f1))
    }
我是这样做的:

def f1_score_class0(labels, predictions):
    """
    To calculate f1-score for the 1st class.
    """
    prec, update_op1 = tf.compat.v1.metrics.precision_at_k(labels, predictions, 1, class_id=0)
    rec,  update_op2 = tf.compat.v1.metrics.recall_at_k(labels, predictions, 1, class_id=0)

    return {
            "f1_Score_for_class0":
                ( 2*(prec * rec) / (prec + rec) , tf.group(update_op1, update_op2) )
    }
已经有了正式的解决方案


你不能这样做,因为
评估度量操作
期望得到
(值,更新操作)
的元组是的,所以第一个值是你计算的f1分数,第二个操作只是身份操作?身份(f1)的含义是什么
op应该在哪里?它将张量转换为伪操作,因为tf.metrics必须返回元组(值张量,更新操作)。您确定这有效吗?根据我的理解,op用于批量计算度量,因为op在这里没有任何意义,我想知道它是否会破坏分数