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'])
My purpose was check the result of accuracy and binary_accuracy is understand difference between them.
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.
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]]
For binary_accuracy is:
m = tf.keras.metrics.BinaryAccuracy()
m.update_state(y_true, y_pred)
m.result().numpy()
that result is:
1
For accuracy is:
m = tf.keras.metrics.Accuracy()
m.update_state(y_true, y_pred)
m.result().numpy()
and the result is: '.5'
But in the above model it is same for each of them in each epoch.
Edit
By changing the compile to this the result changed:
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', tf.keras.metrics.BinaryAccuracy(threshold=.7)])
Why accuracy work like binary_accuracy with threshold=0.5 in model but not in out of model?