Measure ranking quality with ROC AUC
Binary classification evaluation
TensorFlow · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
num_thresholds | int | Number of threshold values used to discretize the prediction range. |
curve | str | Curve to evaluate: 'ROC' or 'PR'; defaults to 'ROC'. |
summation_method | str | Summation method used to estimate the discretized curve area. |
name | str | Optional metric name. |
dtype | str or tensorflow.dtypes.DType | Optional metric dtype. |
thresholds | list or tuple of float | Optional explicit threshold values. |
multi_label | bool | Whether to compute AUC separately for each label. |
num_labels | int | Number of labels when multi_label is True. |
label_weights | list or tuple of float | Optional weights for individual labels. |
from_logits | bool | Whether predictions are logits rather than probabilities. |
A scalar Tensor containing the current AUC value.
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.
Binary classification evaluation
Rare-positive classification
Multi-output or multilabel models
Keras training and validation
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.
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.
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.AUCtf.keras.metrics.Precisiontf.keras.metrics.Recalltf.keras.metrics.BinaryAccuracy