Custom metrics in Keras and how simple they are to use in tensorflow2.2
source link: https://mc.ai/custom-metrics-in-keras-and-how-simple-they-are-to-use-in-tensorflow2-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
So lets get down to it. We first make a custom metric class. While there are more steps to this and they are show in the referenced jupyter notebook
, the important thing is to implement the API that integrates with the rest of Keras training and testing workflow. That is as simple as implementing and update_state
that takes in the true labels and predictions, a reset_states
that re-initializes the metric.
class ConfusionMatrixMetric(tf.keras.metrics.Metric): def update_state(self, y_true, y_pred,sample_weight=None): self.total_cm.assign_add(self.confusion_matrix(y_true,y_pred)) return self.total_cm def result(self): return self.process_confusion_matrix() def confusion_matrix(self,y_true, y_pred): """ Make a confusion matrix """ y_pred=tf.argmax(y_pred,1) cm=tf.math.confusion_matrix(y_true,y_pred,dtype=tf.float32,num_classes=self.num_classes) return cm def process_confusion_matrix(self): "returns precision, recall and f1 along with overall accuracy" cm=self.total_cm diag_part=tf.linalg.diag_part(cm) precision=diag_part/(tf.reduce_sum(cm,0)+tf.constant(1e-15)) recall=diag_part/(tf.reduce_sum(cm,1)+tf.constant(1e-15)) f1=2*precision*recall/(precision+recall+tf.constant(1e-15)) return precision,recall,f1
In the normal Keras workflow, the method result
will be called and it will return a number and nothing else needs to be done. However, in our case we have three tensors for precision, recall and f1 being returned and Keras does not know how to handle this out of the box. This is where the new features of tensorflow 2.2 come in.
Related Articles
AI Enabled Anti Money Protocol — Chapter 3
8. March 2019
8 key points you might want to think about before sharing that next COVID-19 stat with your friends
1. April 2020
Nexus backed H2O.ai raises $72.5 M Series D
20. August 2019
Post navigation
← Top Artificial Intelligence Solution Companies
Defining data science, machine learning, and artificial intelligence →
Request for deletion
About
MC.AI – Aggregated news about artificial intelligence
MC.AI collects interesting articles and news about artificial intelligence and related areas. The contributions come from various open sources and are presented here in a collected form.
The copyrights are held by the original authors, the source is indicated with each contribution.
Contributions which should be deleted from this platform can be reported using the appropriate form (within the contribution).
MC.AI is open for direct submissions, we look forward to your contribution!
Search on MC.AI
mc.ai aggregates articles from different sources - copyright remains at original authors
Recommend
-
53
README.md tensorflow2_tutorials_chinese tensorflow2中文教程,持续更新(一般每周末统一更新) 当前tensorflow版本:tensorflow2.0 最...
-
37
readme.md
-
74
TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络) 源代码/数据集已上传到
-
68
最近迷上了复古游戏,想起小时候入迷的玩贪吃蛇,真的是乐趣无穷。现在到了2019年了,一切似乎都变得和以前不同,比如,我们可以用AI下围棋,用AI做自动驾驶,有没有可能让一个AI自己学会玩贪吃蛇呢?答案当然是可以!! 先来看...
-
55
文章转载自公众号:相约机器人 本文内容来自《深度学习之TensorFlow工程化项目实战》一书,获取更多TensorFlow技巧、新旧版本升级方法以及实例...
-
13
Everything You Need To Know
-
32
How to Implement Custom Data Generators for Enabling Dynamic Data Flow in a Keras Model ...
-
6
Keras 模型 Keras提供的模型,其中分为两类: Sequential 顺序模型Model 类模型(使用函数式 API 的 Model 类模型)我们可以通过from tensorflow.keras import Sequential或者 from tensorflo...
-
5
如何理解Keras中的指标Metrics 推荐 原创 来西瓜 2022-07-25 09:57:11
-
4
Custom Metrics for Keras and TensorFlow Hi, thank...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK