tensorflow / addons

Useful extra functionality for TensorFlow 2.x maintained by SIG-addons
Apache License 2.0
1.69k stars 611 forks source link

MatthewsCorrelationCoefficient is always zero in binary classification #2781

Open digital-thinking opened 1 year ago

digital-thinking commented 1 year ago

System information

MCC does not work on binary labels with a single neuron as output, it always returns 0: Last layer:

x = tf.keras.layers.Dense(1, activation='sigmoid')(x)
model.compile('sgd', 'binary_crossentropy', metrics=['accuracy', tfa.metrics.MatthewsCorrelationCoefficient(num_classes=2)])

Output always 0: 38s 19s/step - loss: 0.6899 - accuracy: 0.4843 - MatthewsCorrelationCoefficient: 0.0000e+00 - val_loss: 0.6934 - val_accuracy: 0.4096 - val_MatthewsCorrelationCoefficient: 0.0000e+00

The last comment from the user here also describes the problem, however the solution does not work anymore https://github.com/tensorflow/addons/issues/2339