Closed henghamao closed 2 years ago
Thanks to the project, I finally found out the solution by using the customized metric function. See my answer on the stackoverflow: https://stackoverflow.com/questions/73564461/recall-and-precision-metrics-for-multi-class-classification-in-tensorflow-keras/73633124#73633124
See the answer on the above.
This is the great work! Though tf and keras have official recall() and precision() metrics, the metrics only worked for binary classfication. We had a problem to classify three categories. And we would like to figure out the recall, precision metrics for the each class. In our model, the last layer is Dense layer with active function 'softmax'. The loss function is 'sparse_categorical_crossentropy', as we used class label for y.
The output of the prediction result is a vector of the probability of the each class, e.g. [0.3, 0.5, 0.2].To get the class label, we need to apply np.argmax() for the prediction results. And thus, the official recall and precision metrics do not work! I found there are sparse_categorical metrics in this project. I tried the following code to get the metric for the class label 1, and passed the metrics to model compile.
Here are error messages we got: