Keras如何计算精度? [英] How does Keras calculate the accuracy?

查看:339
本文介绍了Keras如何计算精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果存在二进制分类问题,则标签为0和1. 我知道预测是浮点数,因为p是属于该类的概率.

If there's a binary classification problem, the labels are 0 and 1. I know the prediction is a floating-point number because p is the probability of belonging to that class.

以下是交叉熵损失函数.

The following is the cross-entropy loss function.

但是,p不一定是0或1,那么Keras如何计算精度? Keras会自动将我们的预测取整为0或1吗?

However, p is not necessarily 0 or 1, so how does Keras calculate the accuracy? Will Keras automatically round our predictions to 0 or 1?

例如,在下面的代码中,精度为0.749,但目标为0和1,预测是不一定为0.0或1.0的浮点数.

For example, in the following code, the accuracy is 0.749, but the targets are 0 and 1 and the predictions are floating-point numbers that are not necessarily 0.0 or 1.0.

>>> scores = model.evaluate(x=test_Features, 
                    y=test_Label)
>>> scores[1]
0.74909090952439739

推荐答案

您在这里有点困惑;在显示损失公式的同时,您谈论的是准确性.

You are a little confused here; you speak about accuracy, while showing the formula for the loss.

您显示的方程式确实是交叉熵损失公式二进制分类(或简称为物流损失).

The equation you show is indeed the cross-entropy loss formula for binary classification (or simply logistic loss).

y[i]是标签,实际上是0或1.

y[i] are the labels, which are indeed either 0 or 1.

p[i]是预测,通常被解释为概率,它们是[0,1]中的实数(无任何舍入).

p[i] are the predictions, usually interpreted as probabilities, which are real numbers in [0,1] (without any rounding).

现在对于每个i,总和中只有一个词可以保留-第一个词在y[i] = 0时消失,第二个词在y[i] = 1时消失.

Now for each i, only one term in the sum will survive - the first term vanishes when y[i] = 0, and similarly the second term vanishes when y[i] = 1.

让我们看一些示例:

假设y[0] = 1,而我们已经预测了p[0] = 0.99(即相当不错的预测).和的第二项消失(自1 - y[0] = 0起),而第一项变为log(0.99) = -0.01.因此,此样本预测(i=0)对总损失的贡献为0.01(由于总和前面的-符号).

Suppose that y[0] = 1, while we have predicted p[0] = 0.99 (i.e. a rather good prediction). The second term of the sum vanishes (since 1 - y[0] = 0), while the first one becomes log(0.99) = -0.01; so, the contribution of this sample prediction (i=0) to the overall loss is 0.01 (due to the - sign in front of the sum).

现在假设下一个样本的真实标签再次为1,即y[1] = 1,但是在这里我们对p[1] = 0.1的预测相当差;再次,第二项消失了,现在该预测对总损失的贡献为-log(0.1) = 2.3,确实比我们第一个良好的预测要大,正如我们应该凭直觉所期望的那样.

Suppose now that the true label of the next sample is again 1, i.e. y[1] = 1, but here we have made a rather poor prediction of p[1] = 0.1; again, the second term vanishes, and the contribution of this prediction to the overall loss is now -log(0.1) = 2.3, which is indeed greater than our first, good prediction, as we should expect intuitively.

作为最后一个例子,让我们假设y[2] = 0,并且我们对p[2] = 0做出了非常好的预测.因此,第一项消失了,第二项变成了

As a final example, let's suppose that y[2] = 0, and we have made a perfectly good prediction here of p[2] = 0; hence, the first term vanishes, and the second term becomes

(1 - y[2]) * log(1 - p[2]) = 1 * log(1) = log(1) = 0

即再次,正如我们直观地预期的那样,我们没有损失,这是因为我们在这里对i=2做出了非常好的预测.

i.e. we have no loss contributed, again as we intuitively expected, since we have made a perfectly good prediction here for i=2.

逻辑损失公式仅计算各个预测的所有这些误差,求和,然后除以其数字n.

The logistic loss formula simply computes all these errors of the individual predictions, sums them, and divides by their number n.

不过,这是 损失 (即您的scores[0]代码段),而不是准确性.

Nevertheless, this is the loss (i.e. scores[0] in your snippet), and not the accuracy.

损失和准确性是不同的东西;粗略地说,从业务的角度来看,准确性是我们真正感兴趣的,而损失则是学习算法(优化器)试图从数学最小化的目标函数. em>观点.更粗略地说,您可以将损失视为业务目标(准确性)对数学域的转换",这是分类问题中必不可少的转换(在回归问题中,损失和业务目标通常是损失).相同,或者至少在原理上可以相同,例如RMSE)...

Loss and accuracy are different things; roughly speaking, the accuracy is what we are actually interested in from a business perspective, while the loss is the objective function that the learning algorithms (optimizers) are trying to minimize from a mathematical perspective. Even more roughly speaking, you can think of the loss as the "translation" of the business objective (accuracy) to the mathematical domain, a translation which is necessary in classification problems (in regression ones, usually the loss and the business objective are the same, or at least can be the same in principle, e.g. the RMSE)...

Keras会自动将我们的预测取整为0或1吗?

Will Keras automatically round our predictions to 0 or 1?

实际上是:为了计算准确性,我们在预测的概率中隐式设置了一个阈值(通常在二进制分类中为0.5,但是在高度不平衡的数据中可能有所不同);因此,在model.evaluate中,如果p[i] > 0.5,Keras实际上会将我们的预测转换为1,否则将其转换为0.然后,只需对y_true==y_pred(正确预测)的情况进行计数,然后除以样本总数即可得出[0,1]中的数字.

Actually yes: to compute the accuracy, we implicitly set a threshold in the predicted probabilities (usually 0.5 in binary classification, but this may differ in the case of highly imbalanced data); so, in model.evaluate, Keras actually converts our predictions to 1 if p[i] > 0.5 and to 0 otherwise. Then, the accuracy is computed by simply counting the cases where y_true==y_pred (correct predictions) and dividing by the total number of samples, to give a number in [0,1].

因此,总结一下:

  • 损失
  • 的计算没有四舍五入
  • 有一个用于计算准确性
  • 的隐式阈值运算
  • There is no rounding for the computation of loss
  • There is an implicit thresholding operation for the computation of accuracy

这篇关于Keras如何计算精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆