Class wise precision and recall for multi class classification in Tensorflow?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
For multiclass classification, class-wise precision and recall are usually computed after prediction time, not as a single built-in Keras metric during training. The common workflow is to run the model, convert probabilities to predicted class IDs, build a confusion matrix, and then derive precision and recall for each class from that matrix.
Why accuracy is not enough
Accuracy collapses everything into one number. That can hide important class-specific failures, especially when:
- classes are imbalanced
- one class is much harder than the others
- some errors matter more than others
Class-wise precision and recall answer more specific questions:
- precision for class
i: when the model predicts classi, how often is it correct - recall for class
i: when the true label is classi, how often does the model find it
Start from predicted class IDs
Assume you have a softmax model:
For multiclass softmax, argmax is the usual way to convert output probabilities into predicted labels.
Use a confusion matrix to get per-class counts
TensorFlow can build the confusion matrix directly:
For a class i:
- true positives are
cm[i, i] - false positives are the rest of column
i - false negatives are the rest of row
i
From that, you can compute class-wise precision and recall:
This is the clearest manual way to understand what the metrics mean.
sklearn is often the easiest reporting tool
In practice, many people train with TensorFlow and evaluate with scikit-learn reporting helpers:
This prints class-wise precision, recall, F1-score, and support in one step. It is often the fastest answer when the question is about reporting metrics rather than implementing them from scratch.
Why built-in Keras precision and recall can be confusing here
Keras has Precision and Recall metrics, but they are more natural for binary or one-vs-rest thresholded setups. For multiclass softmax, people often expect them to automatically emit a separate metric per class, which is not the default experience.
That is why confusion-matrix or classification_report workflows are so common for multiclass problems: they make the class dimension explicit.
Macro, micro, and weighted averages are different summaries
Once you have class-wise metrics, you may also see:
- macro average: unweighted average across classes
- weighted average: average weighted by class support
- micro average: computed from globally aggregated counts
Those are useful summaries, but they should not replace the actual per-class numbers when class-specific behavior matters.
Common Pitfalls
- Looking only at accuracy and missing class-specific failures.
- Forgetting to convert softmax outputs to class IDs with
argmax. - Expecting built-in Keras precision and recall metrics to automatically give per-class results in the desired format.
- Mixing one-hot labels and integer labels without converting consistently.
- Reporting only macro averages when stakeholders need per-class detail.
Summary
- For multiclass TensorFlow models, class-wise precision and recall are usually computed after prediction.
- Convert softmax outputs with
argmax, then build a confusion matrix. - Precision and recall for each class come directly from the confusion matrix counts.
- '
sklearn.metrics.classification_reportis often the easiest reporting tool.' - Keep per-class metrics visible instead of relying only on global accuracy or averages.
Related reading
- Class wise precision and recall for multi class classification in Tensorflow?
- Clearing Tensorflow GPU memory after model execution
- Clearing tf.data.Dataset from GPU memory
- closing session in tensorflow doesn't reset graph
- Classification metrics can't handle a mix of binary and continuous targets
- Classification report with Nested Cross Validation in SKlearn Average/Individual values
- CNN Image Recognition with Regression Output on Tensorflow
- CNN model conditional layer in Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.