How to calculate AUC with 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
In TensorFlow, the usual way to calculate AUC is with tf.keras.metrics.AUC. The important part is not just creating the metric, but feeding it the right kind of predictions. AUC expects scores or probabilities, not hard class labels, because it evaluates ranking quality across many thresholds.
Use tf.keras.metrics.AUC directly
A minimal example looks like this:
That computes ROC AUC for one batch of labels and prediction scores.
The key point is that y_pred should be confidence-like outputs. If you convert the model output to hard 0 or 1 predictions first, you throw away the ranking information that AUC depends on.
AUC is a stateful metric
Like many Keras metrics, AUC accumulates state across updates. That means you can feed it one batch at a time during evaluation.
Reset the metric between epochs or evaluation runs so you do not accidentally mix results from unrelated passes.
Use it during model.compile
In Keras, you can register AUC as a metric directly:
Then Keras will update the metric automatically during training or validation.
This is the easiest path for binary classification problems where the model outputs a single probability-like value per example.
ROC AUC versus PR AUC
TensorFlow's AUC metric can compute more than one curve summary:
Use ROC AUC when you want the standard ranking-based classifier metric. Use PR AUC when class imbalance is severe and precision-recall behavior is more informative for the problem.
The metric name is the same class, but the meaning of the score changes with the curve argument.
Be careful with logits and shapes
AUC wants scores. If your model outputs logits instead of probabilities, you usually need to pass them through a sigmoid first for binary classification.
Also make sure the label and prediction shapes align. For ordinary binary classification, both are usually one-dimensional or broadcast-compatible vectors.
Multiclass AUC needs extra care
For multiclass problems, you cannot assume the binary setup automatically applies. Some workflows compute one-vs-rest AUC for each class and then average the results, while others configure the metric differently depending on how predictions are represented. The important point is to decide what AUC means for your task before reporting it, rather than treating the binary example as universal.
Common Pitfalls
The most common mistake is feeding AUC hard predicted classes instead of probabilities or scores. AUC is about threshold behavior across the score distribution, so hard labels defeat the point.
Another common issue is forgetting that the metric is stateful. If you do not call reset_state(), the next evaluation run will continue accumulating the old data.
People also mix ROC AUC and PR AUC as if they were interchangeable. They are both useful, but they answer different questions.
Finally, if the model outputs logits, remember to think carefully about whether the metric is seeing logits or probabilities and whether that matches your training setup.
Summary
- Use
tf.keras.metrics.AUCto calculate AUC in TensorFlow. - Feed it probabilities or scores, not hard class labels.
- The metric is stateful, so reset it between separate runs.
- Choose
curve="ROC"orcurve="PR"based on the evaluation question. - Check prediction shape and whether your model output is logits or probabilities.
Related reading
- How to calculate confidence score of a Neural Network prediction
- How to calculate perplexity of RNN in tensorflow
- How to calculate prediction uncertainty using Keras?
- How to calculate the flops of a tensorflow model loaded from pb file
- How to calculate input_dim for a keras sequential model?
- how to calculate PDF in tensorflow
- How to calculate confidence score of a Neural Network prediction
- How to calculate feature importance in each models of cross validation in sklearn
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.