[英]How does the validation accuracy decide which class is correct in a binary classification with Keras?
I've been using Keras for binary classification with Tensorflow backend in Python. 我一直在使用Keras在Python中使用Tensorflow后端进行二进制分类。 My model is created like this :
我的模型是这样创建的:
model = Sequential()
model.add(Dense(1000, input_dim=168319))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=['accuracy'])
And my result after training looks like this : 训练后的结果如下:
342/1342 [==============================] - 79s 59ms/step - loss: 0.0586 - acc: 0.9911 - val_loss: 0.4632 - val_acc: 0.8169
If I use my network to predict a sample, it gives me a number between [0,1], as it should, since I'm using a sigmoid activation function for my output neuron. 如果我使用我的网络来预测样本,它会给我一个介于[0,1]之间的数字,因为我对输出神经元使用了S型激活函数。 One example of what my output looks like after a prediction for 6 samples which should be of class 1 :
在预测了应该属于1类的6个样本之后,我的输出看起来像一个例子:
[[1. ][1. ][0.99997437][0.18694757][0.18712251][0.9491884 ]]
Since the results are all floats between 0 and 1 , I've been wondering how Keras calculates the validation accuracy. 由于结果都是介于0和1之间的浮点数,因此我一直想知道Keras如何计算验证准确性。 Validation accuracy is measured with testing samples that are not used for training.
验证准确性是通过未用于培训的测试样本来衡量的。 As mentioned here how does Keras compute validation accuracy and training accuracy?
如此处所述,Keras如何计算验证准确性和训练准确性? , the validation accuracy is calculated with
(amount of correct guesses)/(total amount of guesses)
. ,验证准确性的计算方法是
(amount of correct guesses)/(total amount of guesses)
。
My Question here is, how does Keras decide which class the guess belongs to, when is it "correct". 我的问题是,Keras如何确定猜测所属的类别,何时是“正确”的。 Does it round up at 0.5?
舍入为0.5吗? Or is everything between 0 and 1 classified as "wrong guess" (Would expect a way lower number in validation accuracy then)?
还是将0到1之间的所有内容归类为“错误猜测”(届时期望验证准确性的数字会更低)?
Since you are using binary_crossentropy
, in this case each of your six classes is evaluated separately. 由于您使用的是
binary_crossentropy
,因此在这种情况下,六个类别的每个类别都将分别进行评估。 For each a value above 0.5 is set to 1. Below 0.5 is set to 0. If you were using categorical_crossentropy
then only one of the classes can be 1. Whichever has the highest probability will be set to 1. The rest will be set to 0. If you think about it, the binary
case is just a special case of the categorical
one. 对于每个值,将大于0.5的值设置为1。小于0.5的值设置为0。如果您使用
categorical_crossentropy
则只有一个类可以为1。将概率最高的一个设置为1。将其余的设置为0.如果考虑一下, binary
情况只是categorical
情况的一种特殊情况。 The highest value is set to 1. So if and only if the prediction is bigger than 0.5 then the prediction is set to 1. 最大值设置为1。因此,当且仅当预测值大于0.5时,预测值才设置为1。
For more info, see binary_accuracy
under github.com/keras-team/keras/blob/master/keras/metrics.py. 有关更多信息,请参见
binary_accuracy
下的binary_accuracy。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.