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

ree*_*106 5 python tensorflow tensorboard

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

mad*_*n25 5

我发现它真的很奇怪,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])
Run Code Online (Sandbox Code Playgroud)

通常,此方法的缺点是,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})
Run Code Online (Sandbox Code Playgroud)

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

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

看起来如何: 伊姆古尔