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

以下是我的项目代码的一部分。如何在TensorFlow中初始化tf.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'

我意识到,我不能创建一个验证初始化这样。当我保存新的检查点模型并应用新一轮验证时,我想重新计算相应的指标。所以,我必须重新初始化指标为零。

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

回答:

我参考了博客(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')

以上是 如何在TensorFlow中初始化tf.metrics成员? 的全部内容, 来源链接: utcz.com/qa/259527.html

回到顶部