Keras 是如何计算准确率的? [英] How does Keras calculate the accuracy?

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

问题描述

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

以下是交叉熵损失函数.

但是p不一定是0或1,那么Keras是如何计算准确率的呢?Keras 会自动将我们的预测四舍五入为 0 或 1 吗?

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

<预><代码>>>>分数 = model.evaluate(x=test_Features,y=test_Label)>>>分数[1]0.74909090952439739

解决方案

你在这里有点困惑;你谈到了准确性,同时展示了损失的公式.

您显示的方程确实是 交叉熵损失公式二元分类(或简单的逻辑损失).

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

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

现在对于每个 i,总和中只有一个项会存活 - 当 y[i] = 0 时第一项消失,类似地,当第二项消失时y[i] = 1.

让我们看一些例子:

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

假设现在下一个样本的真实标签又是 1,即 y[1] = 1,但这里我们对 p[1] = 做了一个相当糟糕的预测0.1;再次,第二项消失了,这个预测对整体损失的贡献现在是 -log(0.1) = 2.3,这确实比我们第一个很好的预测更大,正如我们应该直观地预期的那样.

作为最后一个例子,让我们假设 y[2] = 0,并且我们在这里对 p[2] = 0 做出了完美的预测;因此,第一项消失,第二项变成

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

即我们没有损失贡献,这再次符合我们的直觉预期,因为我们在这里对 i=2 做出了非常好的预测.

逻辑损失公式简单地计算各个预测的所有这些误差,将它们相加,然后除以它们的数量 n.

尽管如此,这是损失(即scores[0] 在您的代码段中),而不是 准确度.

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

<块引用>

Keras 会自动将我们的预测四舍五入为 0 或 1 吗?

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

总结一下:

  • 损失的计算没有四舍五入
  • 有一个用于计算准确度
  • 的隐式阈值操作

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.

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?

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] are the labels, which are indeed either 0 or 1.

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

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.

Let's see some examples:

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).

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.

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.e. we have no loss contributed, again as we intuitively expected, since we have made a perfectly good prediction here for i=2.

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

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

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)...

Will Keras automatically round our predictions to 0 or 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].

So, to summarize:

  • There is no rounding for the computation of loss
  • There is an implicit thresholding operation for the computation of accuracy

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

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