Custom metrics for Keras/TensorFlow
Recently, I published an article about binary classification metrics that you can check here. The article gives a brief explanation of the most traditional metrics and presents less famous ones like NPV, Specificity, and MCC. If you don't know some of these metrics, take a look at the article. It's only 7 minutes to read. I'm sure it will be useful for you.
In this article, I decided to share the implementation of these metrics for Deep Learning frameworks. It includes recall, precision, specificity, negative predictive value (NPV), f1-score, and Matthews' Correlation Coefficient (MCC). You can use it in both Keras or TensorFlow v1/v2.
The Code
Here's the complete code for all metrics:
Almost all the metrics in the code are described in the article previously mentioned. Therefore, you can find a detailed explanation there.
How to use in Keras or TensorFlow
If you use Keras or TensorFlow (especially v2), it’s quite easy to use such metrics. Here’s an example:
model = ... # define you model as usualmodel.compile(
optimizer="adam", # you can use any other optimizer
loss='binary_crossentropy',
metrics=[
"accuracy",
precision,
recall,
f1,
fbeta,
specificity,
negative_predictive_value,
matthews_correlation_coefficient,
equal_error_rate
]
)model.fit(...) # train your model
As you can see, you can compute all the custom metrics at once. Please, remember that:
- as they are binary classification metrics, you can only use them in binary classification problems. Maybe you’ll have some results for multiclass or regression problems, but they will be incorrect.
- they are supposed to be used as metrics only. It means you can’t use them as losses. In fact, your loss must always be “binary_crossentropy”, since it's a binary classification problem.
Final Words
I hope you liked this article. If it was helpful for you too, please give some applause 👏👏. Follow me on Medium for more posts like this. You can also check my work in: