【问题标题】:Tensorflow plot tf.metrics.precision_at_thresholds in Tensorboard through eval_metric_opsTensorflow 通过 eval_metric_ops 在 Tensorboard 中绘制 tf.metrics.precision_at_thresholds
【发布时间】:2018-02-24 19:04:32
【问题描述】:

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

【问题讨论】:

    标签: python tensorflow tensorboard


    【解决方案1】:

    我发现 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 的概念都会丢失。我想出了一个稍微复杂但更易于使用的解决方案,它使用集合来存储所有指标更新运算符。

    # 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})
    

    下面的 Gist [1] 中的实现还支持很好地格式化有时神秘的指标名称的选项。

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

    这可能是什么样子:

    【讨论】:

      【解决方案2】:

      我目前的方法是创建一个单独的函数,它只取列表中第一个元素的平均值。但是,我期待有一个比这更优雅的解决方案:

      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
      

      【讨论】:

        猜你喜欢
        • 2018-01-20
        • 2020-06-02
        • 2018-12-01
        • 2020-03-01
        • 2018-07-20
        • 2021-04-03
        • 2020-05-03
        • 2019-11-19
        • 2017-06-06
        相关资源
        最近更新 更多