Python 为什么在计算逻辑回归的准确度时使用“tf.reduce_mean”?
以下函数用于计算逻辑回归的准确性,但在此函数中使用Python 为什么在计算逻辑回归的准确度时使用“tf.reduce_mean”?,python,tensorflow,machine-learning,keras,logistic-regression,Python,Tensorflow,Machine Learning,Keras,Logistic Regression,以下函数用于计算逻辑回归的准确性,但在此函数中使用reduce\u mean函数有什么意义 代码是: import tensorflow as tf def accuracy(y_pred, y_true): # Predicted class is the index of the highest score in prediction vector (i.e. argmax). correct_prediction = tf.equal(tf
reduce\u mean
函数有什么意义
代码是:
import tensorflow as tf
def accuracy(y_pred, y_true):
# Predicted class is the index of the highest score in prediction vector (i.e. argmax).
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64))
return tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
首先,请注意,度量或损失函数通常期望一批预测/真实标签作为输入。现在,如果相应的预测是正确的,
correct_prediction
的每个元素都是True
;否则,它将为False
。然后,tf.cast(correct\u prediction,tf.float32)
将True
值转换为1,将False
值转换为0。因此,计算其平均值(即平均值)将等同于预测的准确性(尽管,作为范围[0,1]中的值,而不是百分比)
为了进一步阐明这一点,请考虑如下:
>>正确的预测
[真、假、假、真、真]#五分之三的预测是正确的
>>>tf.cast(正确的预测,tf.float32)
[1, 0, 0, 1, 1]
>>>减少平均值
0.6#这意味着60%的准确率,这是我们所期望的