在Tensorboard中使用带有Tensorflow Estimator的tf.name_scope

ElP*_*nte 6 tensorflow tensorboard

我有一些代码来计算我的Estimatormodel_fn中的性能指标,该函数在函数中编写,该函数返回指标字典

def __model_eval_metrics(self, classes, labels, mode):
    if mode == tf.estimator.ModeKeys.TRAIN or mode == tf.estimator.ModeKeys.EVAL:
        return {
                'accuracy': tf.metrics.accuracy(labels=tf.argmax(input=labels, axis=1), predictions=classes),
                'precision': tf.metrics.precision(labels=tf.argmax(input=labels, axis=1), predictions=classes),
                'recall': tf.metrics.recall(labels=tf.argmax(input=labels, axis=1), predictions=classes)
                }
    else:
        return None
Run Code Online (Sandbox Code Playgroud)

在估算器培训期间,这些记录为model_fn按名称范围"train_metrics"分组的缩放器

if mode == tf.estimator.ModeKeys.TRAIN:
    with tf.name_scope('train_metrics') as scope:
        tf.summary.scalar('model_accuracy', eval_metrics['accuracy'][1])
        tf.summary.scalar('model_precision', eval_metrics['precision'][1])
        tf.summary.scalar('model_recall', eval_metrics['recall'][1])
        tf.summary.scalar('model_loss', loss)
Run Code Online (Sandbox Code Playgroud)

这在Tensorboard中产生了所需的分组 Tensorboard示例

为了进行Estimator 评估,度量标准作为字典传递给EstimatorSpeceval_metric_ops参数作为结果__model_eval_metrics()

return tf.estimator.EstimatorSpec(
    mode=mode,
    predictions={"predictions": predictions, "classes": classes},
    loss=loss,
    train_op=train_op,
    eval_metric_ops=eval_metrics,
)
Run Code Online (Sandbox Code Playgroud)

问题是在Tensorboard中,这些指标不再按名称范围分组,我无法确定在何处添加名称范围以实现此目的.您可以看到评估指标未分组.

Tensorboard错了

  1. 有没有一种方法可以将name_scope用于评估指标Estimator
  2. 我是否应该完全忽略name_scope并切换Tensorboard屏幕左下角的运行?

小智 1

我通过使用与度量名称(eval)相同的“文件夹”前缀作为摘要(train)的名称范围来解决这个问题:

r2 = metrics_r2(labels, predictions)
metrics = {'metrics/r2': r2}
with tf.name_scope('metrics'):
    tf.summary.scalar('r2', r2[1])

if mode == tf.estimator.ModeKeys.EVAL:
    return tf.estimator.EstimatorSpec(mode, loss=m_loss, eval_metric_ops=metrics)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述