为什么keras中的accuracy和binary_accuracy有相同的结果? [英] Why the accuracy and binary_accuracy in keras have same result?

查看:392
本文介绍了为什么keras中的accuracy和binary_accuracy有相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Keras 创建了一个简单的二元分类模型.代码是:

I created a simple model for binary classification with Keras. The code is:

    # create model
    model = Sequential()
    model.add(Dense(250, input_dim=1, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', 'binary_accuracy'])

我的目的是检查 accuracybinary_accuracy 的结果,以了解它们之间的区别.

My purpose was check the result of accuracy and binary_accuracy is understand difference between them.

正如 Keras 所说,binary_accuracy 准确度的阈值默认为 .5,而准确度"则没有.当我用样本数据测试它们时,结果是不同的,但在模型序列中,你在每个时期都有相同的结果.

As Keras says binary_accuracy accuracy have threshold that default is .5, that `accuracy' haven't. When I test them with sample data the result is difference but in the train of model thy have same results in each epoch.

对于这个真实和预测的样本,我测试了accuracybinary_accuracy:

for this true and predicted sample I tested accuracy and binary_accuracy:

   y_true = [[1], [1], [0], [0]]
   y_pred = [[0.51], [1], [0], [0.4]]

对于 binary_accuracy 是:

m = tf.keras.metrics.BinaryAccuracy()
m.update_state(y_true, y_pred)
m.result().numpy()

结果是:1

对于 accuracy 是:

m = tf.keras.metrics.Accuracy()
m.update_state(y_true, y_pred)
m.result().numpy()

结果是:'.5'

但在上面的模型中,每个时代的每个人都是一样的.

But in the above model it is same for each of them in each epoch.

编辑

通过将编译更改为这样,结果发生了变化:

By changing the compile to this the result changed:

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', tf.keras.metrics.BinaryAccuracy(threshold=.7)])

为什么 accuracybinary_accuracy 一样工作,threshold=0.5 在模型中而不在模型外?

Why accuracy work like binary_accuracy with threshold=0.5 in model but not in out of model?

推荐答案

根据 tf.keras.Model.compile() 文档:

当您传递字符串 'accuracy' 或 'acc' 时,我们会根据损失函数将其转换为 tf.keras.metrics.BinaryAccuracy、tf.keras.metrics.CategoricalAccuracy、tf.keras.metrics.SparseCategoricalAccuracy 之一使用和模型输出形状.我们也对字符串 'crossentropy' 和 'ce' 进行了类似的转换.

When you pass the strings 'accuracy' or 'acc', we convert this to one of tf.keras.metrics.BinaryAccuracy, tf.keras.metrics.CategoricalAccuracy, tf.keras.metrics.SparseCategoricalAccuracy based on the loss function used and the model output shape. We do a similar conversion for the strings 'crossentropy' and 'ce' as well.

在您的情况下,它已转换为 BinaryAccuracy,因此结果相同.

In your case it was transformed to BinaryAccuracy and hence result is the same.

然而 tf.keras.metrics.Accuracy 是完全不同的东西.如果您阅读文档:

However tf.keras.metrics.Accuracy is something completely different. If you read the documentation:

计算预测与标签相等的频率.

Calculates how often predictions equal labels.

这意味着它查看 y_predy_true 的唯一值,并将每个唯一值视为一个不同的标签.在您的情况下,0.51 和 0.4 被视为单独的标签,因为它们分别不等于 1 和 0,所以您得到 0.5

which means it looks at unique values of y_pred and y_true and treats every unique value as a distinct label. In your case 0.51 and 0.4 are treated as a separate labels and because they are not equal to 1 and 0, respectively, you get 0.5

抱歉最初将此问题标记为重复,tf.keras 中的行为与 keras 包中的行为不同

Apologies for marking this question as a duplicate at first, the behaviour is different in tf.keras than in keras package

这篇关于为什么keras中的accuracy和binary_accuracy有相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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