tensorflow使用自定义估算器更多指标

问题描述 投票:0回答:1

我创建了自定义估算器,在引擎盖下使用binary_classification_head()。一切都很好,但问题在于可见的指标。我正在使用级别为tf.logging.set_verbosity(tf.logging.INFO)和tensorboard的日志记录,但我只看到损失值。我添加了这段代码,但它没有任何帮助。

def my_accuracy(labels, predictions):
    return {'accuracy': tf.metrics.accuracy(labels, predictions['logistic'])}
classifier = tf.contrib.estimator.add_metrics(classifier, my_accuracy)

你知道其他一些添加指标的方法吗?

tensorflow metrics tensorboard tensorflow-estimator
1个回答
0
投票

您需要在model_fn中放置相关的指标函数。

例如:

tf.summary.image('input_image', input_image, max_outputs)

for v in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
        tf.summary.histogram(v.name, v)

包含update_op的度量标准,如f1得分的准确度,需要输入eval_metric_ops。使用切片是因为它们输出两个值,度量值和更新操作

f1 = tf.contrib.metrics.f1_score(labels, predictions, num_thresholds)
accuracy = tf.metrics.accuracy(labels, predictions)
tf.summary.scalar('accuracy', accuracy[1])

eval_metric_ops = {
    'f1_score': f1,
    'accuracy': accuracy
}

return tf.estimator.EstimatorSpec(mode=mode,
                                  loss=loss,
                                  train_op=train_op,
                                  eval_metric_ops=eval_metric_ops,
                                  )

eval_metric_ops dict可以在火车模式和评估模式下进行。

如果您使用的是预设估算器,则可以使用add_metrics

编辑:根据官方文档,您可以使用带有预测估算器的binary_classification_head或返回estimator_spec的model_fn func。看到

my_head = tf.contrib.estimator.binary_classification_head()
my_estimator = tf.estimator.DNNEstimator(
    head=my_head,
    hidden_units=...,
    feature_columns=...)

在这种情况下,即使没有add_metrics func,您也应该能够添加指标

© www.soinside.com 2019 - 2024. All rights reserved.