精度始终为零

Precision is always zero tensorflow metrics

我正在使用 tf 指标计算精度和召回率,但总是得到 0.0,但我自己计算时精度很高,是 tensorflow 的错误还是我做错了什么。

with tf.name_scope("pointwise_accuracy"):
    correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
    self.classification_accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
    self.precision = tf.metrics.precision(self.input_y, self.logits, name="precison")[0]

输出 精度 - 0.0

tf.metrics.precision is meant to be used in binary classification problems only, and its arguments must be all 0 or 1 since, as the docs say, they will be converted to bool. If you are indeed working in a binary classification problem but want to use the logits as parameter, you can look at tf.metrics.precision_at_thresholds,它允许您指定将预测视为正确的阈值。

但是,由于您的手动计算使用tf.argmax,它看起来更像是一个多类分类问题,在这种情况下您通常不会谈论precision/recall,而只是准确性,所以您可以查看 tf.metrics.accuracy,并将 tf.argmax(self.input_y, 1)self.predictions 作为参数传递。