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

如何在TensorFlow中初始化tf.metrics成员?,tensorflow,metrics,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=l

下面是我的项目代码的一部分

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'
回溯(最近一次呼叫最后一次):
文件“/run_dnn.py”,第285行,在
列车(wnd_形态)
文件“/run_dnn.py”,第89行,列车中
name='validation\u metrics\u init')
文件“/export/local/anaconda2/lib/python2.7/site-
packages/tensorflow/python/ops/variables.py”,第1176行
变量初始值设定项
返回控制流操作组(*[v.变量列表中v的初始值设定项],名称=名称)
AttributeError:“Tensor”对象没有属性“initializer”
我意识到我不能创建这样的验证初始值设定项。我想在保存新的检查点模型并应用新一轮验证时重新计算相应的度量。因此,我必须重新初始化度量为零


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

参考博客()后,我用以下方法解决了这个问题()

# 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')