繁体   English   中英

通过eval_metric_ops在Tensorboard中的Tensorflow图tf.metrics.precision_at_thresholds

[英]Tensorflow plot tf.metrics.precision_at_thresholds in Tensorboard through eval_metric_ops

tf.metrics.precision_at_thresholds()采用三个参数: labels, predictions, thresholds ,其中阈值是[0,1]之间的python列表或阈值元组 然后,该函数返回“形状为[len(thresholds)]的浮点张量”,这对于自动将eval_metric_ops绘制到张量板上是有问题的(因为我认为它们应该是标量)。 这些值会很好地打印到控制台,但我也想在张量板上绘制这些值。 是否可以进行任何调整以能够在张量板上绘制该值?

我发现TensorFlow(从1.8版本开始)没有为tf.metrics.precision_at_thresholds (通常为tf.metrics.*_at_thresholds )之类的指标提供汇总功能真的很奇怪。 以下是一个最小的工作示例:

def summarize_metrics(metrics_update_ops):
    for metric_op in metric_ops:
        shape = metric_op.shape.as_list()
        if shape:  # this is a metric created with any of tf.metrics.*_at_thresholds
            summary_components = tf.split(metric_op, shape[0])
            for i, summary_component in enumerate(summary_components):
                tf.summary.scalar(
                    name='{op_name}_{i}'.format(op_name=summary_components.name, i=i),
                    tensor=tf.squeeze(summary_component, axis=[0])
                )
        else:  # this already is a scalar metric operator
            tf.summary.scalar(name=summary_components.name, tensor=metric_op)

precision, precision_op = tf.metrics.precision_at_thresholds(labels=labels,
                                                             predictions=predictions,
                                                             thresholds=threshold)
summarize_metrics([precision_op])

通常,这种方法的缺点是,在汇总这些thresholds时,一开始会丢失用于创建度量的任何thresholds概念。 我想出了一个稍微复杂但易于使用的解决方案,该解决方案使用集合存储所有度量标准更新运算符。

# Create a metric and let it add the vars and update operators to the specified collections
thresholds = [0.5, 0.7]
tf.metrics.recall_at_thresholds(
    labels=labels, predictions=predictions, thresholds=thresholds,
    metrics_collections='metrics_vars', metrics_update_ops='metrics_update_ops'
)

# Anywhere else call the summary method I provide in the Gist at the bottom [1]
# Because we provide a mapping of a scope pattern to the thresholds, we can
# assign them later
summarize_metrics(list_lookup={'recall_at_thresholds': thresholds})

下面的要点[1]中的实现还支持一些选项,这些选项可以很好地格式化有时是度量标准的神秘名称的格式。

[1]: https//gist.github.com/patzm/961dcdcafbf3c253a056807c56604628

看起来如何: 伊姆古尔

我当前的方法是创建一个单独的函数,该函数仅使用列表中第一个元素的均值。 但是,我希望有一个比这更优雅的解决方案:

def metric_fn(labels, predictions, threshold):
   precision, precision_op = tf.metrics.precision_at_thresholds(labels = labels,
                                                  predictions = predictions,
                                                  thresholds = threshold)
   mean, op = tf.metrics.mean(precision[0])

   return mean, op

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM