Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在计算逻辑回归的准确度时使用“tf.reduce_mean”?_Python_Tensorflow_Machine Learning_Keras_Logistic Regression - Fatal编程技术网

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%的准确率,这是我们所期望的