如何在TensorFlow中初始化tf.metrics成员?

时间:2017-12-03 04:47:35

标签: tensorflow metrics

以下是我的项目代码的一部分。

with tf.name_scope("test_accuracy"):
    test_mean_abs_err, test_mean_abs_err_op = tf.metrics.mean_absolute_error(labels=label_pl, predictions=test_eval_predict)
    test_accuracy, test_accuracy_op         = tf.metrics.accuracy(labels=label_pl, predictions=test_eval_predict)
    test_precision, test_precision_op       = tf.metrics.precision(labels=label_pl, predictions=test_eval_predict)
    test_recall, test_recall_op             = tf.metrics.recall(labels=label_pl, predictions=test_eval_predict)
    test_f1_measure = 2 * test_precision * test_recall / (test_precision + test_recall)
tf.summary.scalar('test_mean_abs_err', test_mean_abs_err)
tf.summary.scalar('test_accuracy', test_accuracy)
tf.summary.scalar('test_precision', test_precision)
tf.summary.scalar('test_recall', test_recall)
tf.summary.scalar('test_f1_measure', test_f1_measure)
# validation metric init op
validation_metrics_init_op = tf.variables_initializer(\
        var_list=[test_mean_abs_err_op, test_accuracy_op, test_precision_op, test_recall_op], \
        name='validation_metrics_init')

然而,当我运行它时,会发生如下错误:

Traceback (most recent call last):
  File "./run_dnn.py", line 285, in <module>
    train(wnd_conf)
  File "./run_dnn.py", line 89, in train
    name='validation_metrics_init')
  File "/export/local/anaconda2/lib/python2.7/site-
packages/tensorflow/python/ops/variables.py", line 1176, in 
variables_initializer
return control_flow_ops.group(*[v.initializer for v in var_list], name=name)
AttributeError: 'Tensor' object has no attribute 'initializer'

我意识到我无法创建这样的验证初始化器。我想在保存新检查点模型并应用新一轮验证时重新计算相应的指标。因此,我必须将指标重新初始化为零。

但是如何将所有这些指标重置为零?非常感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

在引用博客(Avoiding headaches with tf.metrics)之后,我以下列方式解决了这个问题。

# validation metrics
validation_metrics_var_scope = "validation_metrics"
test_mean_abs_err, test_mean_abs_err_op = tf.metrics.mean_absolute_error(labels=label_pl, predictions=test_eval_predict, name=validation_metrics_var_scope)
test_accuracy, test_accuracy_op         = tf.metrics.accuracy(labels=label_pl, predictions=test_eval_predict, name=validation_metrics_var_scope)
test_precision, test_precision_op       = tf.metrics.precision(labels=label_pl, predictions=test_eval_predict, name=validation_metrics_var_scope)
test_recall, test_recall_op             = tf.metrics.recall(labels=label_pl, predictions=test_eval_predict, name=validation_metrics_var_scope)
test_f1_measure = 2 * test_precision * test_recall / (test_precision + test_recall)
tf.summary.scalar('test_mean_abs_err', test_mean_abs_err)
tf.summary.scalar('test_accuracy', test_accuracy)
tf.summary.scalar('test_precision', test_precision)
tf.summary.scalar('test_recall', test_recall)
tf.summary.scalar('test_f1_measure', test_f1_measure)
# validation metric init op
validation_metrics_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope=validation_metrics_var_scope)
validation_metrics_init_op = tf.variables_initializer(var_list=validation_metrics_vars, name='validation_metrics_init')