我在Tensorflow中使用自定义Estimator。 tf.metrics.accuracy似乎工作正常,但不是tf.metrics.auc,它在返回的EstimatorSpec中总是显示0.5用于评估和Tensorboard培训。
以下是我的模型函数的代码片段:
def _model_fn(features, labels, mode, params):
......
accuracy = tf.metrics.accuracy(labels=labels,
predictions=predicted_classes,
name='acc_op')
auc = tf.metrics.auc(labels=labels,
predictions=predicted_classes,
name='auc_op')
metrics = {'accuracy': accuracy, 'auc': auc}
tf.summary.scalar('accuracy', accuracy[1])
tf.summary.scalar('auc', auc[1])
if mode == tf.estimator.ModeKeys.EVAL:
# accuracy shows the right value; auc always shows 0.5
return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)
# the accuracy, loss during training are written to Tensorboard correctly.
# But auc always has value 0.5.
summary_hook = tf.train.SummarySaverHook(
params['skip_step'],
output_dir=params['model_dir'],
summary_op=tf.summary.merge_all())
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op, training_hooks=[summary_hook])
任何人都知道AUC计算出了什么问题?我已经阅读了以下问题。这里的区别是因为我坚持使用高级Estimator API,它没有明确地使用会话。我是否需要初始化局部变量?如果是,我该如何修改我的代码?非常感谢!
how to use tf.metrics.__ with estimator model predict output