通过 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
其中阈值是 一个 python 列表或元组 之间的阈值 [0,1] .然后函数 returns "A float Tensor of shape [len(thresholds)]" 这对于自动将 eval_metric_ops 绘制到张量板是有问题的(因为我相信它们应该是标量)。这些值会很好地打印到控制台,但我也想在张量板上绘制这些值。是否可以进行任何调整以绘制张量板中的值?
我目前的方法是创建一个单独的函数,它只取列表中第一个元素的平均值。但是,我期待有比这更优雅的解决方案:
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
我发现 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
这看起来像:
tf.metrics.precision_at_thresholds()
采用三个参数:labels, predictions, thresholds
其中阈值是 一个 python 列表或元组 之间的阈值 [0,1] .然后函数 returns "A float Tensor of shape [len(thresholds)]" 这对于自动将 eval_metric_ops 绘制到张量板是有问题的(因为我相信它们应该是标量)。这些值会很好地打印到控制台,但我也想在张量板上绘制这些值。是否可以进行任何调整以绘制张量板中的值?
我目前的方法是创建一个单独的函数,它只取列表中第一个元素的平均值。但是,我期待有比这更优雅的解决方案:
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
我发现 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
这看起来像: