Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

metrics.AUC

A stateful Keras metric that approximates ROC AUC or precision-recall AUC from predictions accumulated across batches. It supports configurable thresholds, summation, multilabel evaluation, label weighting, and logits.

How it works

AUC accumulates confusion-matrix counts at predefined prediction thresholds, then uses those counts to approximate the area under either a ROC or precision-recall curve. Calling update_state adds observations, result computes the current scalar, and reset_state clears accumulated statistics.

Syntax

tf.keras.metrics.AUC(num_thresholds=200, curve='ROC', summation_method='interpolation', name=None, dtype=None, thresholds=None, multi_label=False, num_labels=None, label_weights=None, from_logits=False)

Parameters

NameTypeDescription
num_thresholdsintNumber of threshold values used to discretize the prediction range.
curvestrCurve to evaluate: 'ROC' or 'PR'; defaults to 'ROC'.
summation_methodstrSummation method used to estimate the discretized curve area.
namestrOptional metric name.
dtypestr or tensorflow.dtypes.DTypeOptional metric dtype.
thresholdslist or tuple of floatOptional explicit threshold values.
multi_labelboolWhether to compute AUC separately for each label.
num_labelsintNumber of labels when multi_label is True.
label_weightslist or tuple of floatOptional weights for individual labels.
from_logitsboolWhether predictions are logits rather than probabilities.

Returns

A scalar Tensor containing the current AUC value.

Key points

  • The class reports an approximate area under either the ROC curve or the precision-recall curve.
  • Predictions are grouped into threshold buckets, and the bucket statistics are interpolated to estimate curve points.
  • Its internal state tracks four confusion-matrix totals for each threshold.
  • num_thresholds controls how finely prediction space is divided; larger values can improve the approximation.

Common mistakes

  • With a finite threshold grid, prediction values concentrated near 0 or 1 can reduce approximation quality; use suitable threshold coverage.

Example: Compute ROC-AUC

import tensorflow as tf

metric = tf.keras.metrics.AUC(num_thresholds=100)
metric.update_state([0, 1, 1, 0], [0.1, 0.8, 0.6, 0.3])
print(float(metric.result()))

Creates a ROC-AUC metric, accumulates one batch of labels and probabilities, then reads the accumulated scalar result.

When to use it

Measure ranking quality with ROC AUC

Binary classification evaluation

Evaluate precision-recall performance with PR AUC

Rare-positive classification

Aggregate per-label AUC scores

Multi-output or multilabel models

Track AUC as a compiled model metric

Keras training and validation

Interview questions

QHow does tf.keras.metrics.AUC compute its value?

AUC approximates the area under a ROC or precision-recall curve. It accumulates true-positive, true-negative, false-positive, and false-negative counts over predefined thresholds, then integrates the resulting curve.

Follow-up: How does threshold resolution affect the result? Increasing num_thresholds provides a finer discretization and generally a closer approximation, at the cost of more metric state.

QHow do ROC AUC and PR AUC differ in this class?

Use curve='ROC' for receiver-operating-characteristic AUC, which is the default, or curve='PR' for precision-recall AUC. The choice determines which rate pairs are accumulated and integrated.

Follow-up: When is PR AUC often more informative? It is particularly useful when positive examples are relatively rare, because it focuses on precision and recall.

QHow should AUC be used across batches?

The metric is stateful: update_state adds a batch to its accumulated counts, result returns the current AUC, and reset_state clears the counts. This supports calculating one metric over multiple batches or epochs.

Follow-up: What must happen between independent evaluations? Call reset_state so the next result does not include observations from the earlier evaluation.

  • tf.keras.metrics.AUC
  • tf.keras.metrics.Precision
  • tf.keras.metrics.Recall
  • tf.keras.metrics.BinaryAccuracy

Source: tf.keras.metrics.AUC. Checked 2026-10-15.