tensorflow
AUC calculation
machine learning
deep learning
model evaluation

How to calculate AUC with tensorflow?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

python
1import tensorflow as tf
2
3metric = tf.keras.metrics.AUC(curve="ROC")
4
5y_true = tf.constant([0, 0, 1, 1], dtype=tf.float32)
6y_pred = tf.constant([0.1, 0.4, 0.35, 0.8], dtype=tf.float32)
7
8metric.update_state(y_true, y_pred)
9print(metric.result().numpy())

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.

python
1import tensorflow as tf
2
3metric = tf.keras.metrics.AUC(curve="ROC")
4
5batches = [
6    (tf.constant([0, 1], dtype=tf.float32), tf.constant([0.2, 0.7], dtype=tf.float32)),
7    (tf.constant([1, 0], dtype=tf.float32), tf.constant([0.9, 0.3], dtype=tf.float32)),
8]
9
10for y_true, y_pred in batches:
11    metric.update_state(y_true, y_pred)
12
13print(metric.result().numpy())
14metric.reset_state()

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:

python
1model.compile(
2    optimizer="adam",
3    loss="binary_crossentropy",
4    metrics=[tf.keras.metrics.AUC(name="auc")]
5)

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:

python
roc_auc = tf.keras.metrics.AUC(curve="ROC")
pr_auc = tf.keras.metrics.AUC(curve="PR")

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.

python
1import tensorflow as tf
2
3logits = tf.constant([-1.2, 0.7, 2.3], dtype=tf.float32)
4probs = tf.math.sigmoid(logits)
5
6metric = tf.keras.metrics.AUC()
7metric.update_state(tf.constant([0, 1, 1], dtype=tf.float32), probs)
8print(metric.result().numpy())

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.AUC to 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" or curve="PR" based on the evaluation question.
  • Check prediction shape and whether your model output is logits or probabilities.

Course illustration
Course illustration

All Rights Reserved.