基于 tensorflow 的流指标的自定义指标返回 NaN [英] Custom metric based on tensorflow's streaming metrics returns NaN

查看:33
本文介绍了基于 tensorflow 的流指标的自定义指标返回 NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 F1 分数定义为 TensorFlow 中 DNNClassifier 的自定义指标.为此,我编写了一个函数

I'm trying to define the F1-score as a custom metric in TensorFlow for a DNNClassifier. To do that, I wrote a function

def metric_fn(predictions=[], labels=[], weights=[]):
    P, _ = tf.contrib.metrics.streaming_precision(predictions, labels)
    R, _ = tf.contrib.metrics.streaming_recall(predictions, labels)
    if P + R == 0:
        return 0
    return 2*(P*R)/(P+R)

使用来自 TensorFlow 的 streaming_precisionstreaming_recall 来计算 F1 分数.之后,我在validation_metrics 中创建了一个新条目:

that uses streaming_precision and streaming_recall from TensorFlow to calulate the F1 score. After that I made a new entry to the validation_metrics:

validation_metrics = {
    "accuracy":
        tf.contrib.learn.MetricSpec(
            metric_fn=tf.contrib.metrics.streaming_accuracy,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
    "precision":
        tf.contrib.learn.MetricSpec(
            metric_fn=tf.contrib.metrics.streaming_precision,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
    "recall":
        tf.contrib.learn.MetricSpec(
            metric_fn=tf.contrib.metrics.streaming_recall,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
    "f1score":
        tf.contrib.learn.MetricSpec(
            metric_fn=metric_fn,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES)
}

然而,虽然我得到了正确的精度和召回值,f1score 总是 nan:

However, although I get correct precision and recall values, f1score is always nan:

INFO:tensorflow:Saving dict for global step 151: accuracy = 0.982456, accuracy/baseline_label_mean = 0.397661, accuracy/threshold_0.500000_mean = 0.982456, auc = 0.982867, f1score = nan, global_step = 151, labels/actual_label_mean = 0.397661, labels/prediction_mean = 0.406118, loss = 0.310612, precision = 0.971014, precision/positive_threshold_0.500000_mean = 0.971014, recall = 0.985294, recall/positive_threshold_0.500000_mean = 0.985294

我的 metric_fn 出了点问题,但我想不通.metric_fn 得到的值 PR 的形式为Tensor("precision/value:0", shape=(), dtype=float32).我觉得这有点奇怪.我期待一个标量张量.

Something is wrong with my metric_fn, but I can't figure it out. The values P and R obtained by metric_fn are of the form Tensor("precision/value:0", shape=(), dtype=float32). I find this a bit strange. I was expecting a scalar tensor.

感谢任何帮助.

推荐答案

我认为问题可能来自于您在 metric_fn 中使用的流式指标没有得到任何更新.

I think the problem may come from the fact that the streaming metrics you use within your metric_fn do not get any update.

>

尝试以下操作(我还对我的口味做了一些小改动):

Try the following (I also included minor modifications to my taste):

def metric_fn(predictions=None, labels=None, weights=None):
    P, update_op1 = tf.contrib.metrics.streaming_precision(predictions, labels)
    R, update_op2 = tf.contrib.metrics.streaming_recall(predictions, labels)
    eps = 1e-5;
    return (2*(P*R)/(P+R+eps), tf.group(update_op1, update_op2))

这篇关于基于 tensorflow 的流指标的自定义指标返回 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆