Monitor ROC-AUC during training
Binary classification with probability outputs.
Keras · Class
keras.metrics.AUC estimates the area under a ROC or precision-recall curve for binary or multi-label classification. It accumulates thresholded confusion statistics and returns the approximated area as a scalar metric.
AUC accumulates classification statistics across predefined prediction thresholds and estimates the area under either a ROC or precision-recall curve. Call update_state() with labels and predictions, then result() to obtain the current scalar; the approximation uses bucketed values and a selected summation method.
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 thresholds used to discretize the ROC curve; must be greater than 1 and defaults to 200. |
curve | str | Curve to compute: "ROC" or "PR"; defaults to "ROC". |
summation_method | str | Method used to sum the discretized curve. |
name | str or None | Optional metric name. |
dtype | str or None | Optional metric dtype. |
thresholds | list or None | Explicit threshold values instead of the default linearly spaced thresholds. |
multi_label | bool | Whether inputs represent multiple labels. |
num_labels | int or None | Number of labels for multi-label data. |
label_weights | list or None | Optional weights for labels. |
from_logits | bool | Whether predictions are logits rather than probabilities. |
A scalar tensor containing the estimated AUC, returned by result().
import keras
x = [0.1, 0.9, 0.8, 0.2]
y = [0, 1, 1, 0]
auc = keras.metrics.AUC(curve="ROC")
auc.update_state(y, x)
print(float(auc.result()))Creates a ROC-AUC metric, accumulates labels and predicted probabilities, then reads the scalar result.
Binary classification with probability outputs.
Imbalanced binary classification where precision and recall matter.
Models producing raw scores before sigmoid or softmax conversion.
Multiple independent labels per example.
AUC estimates the area under a ROC or precision-recall curve. It accumulates predictions into threshold buckets and computes the area from the resulting precision, recall, or false-positive-rate values.
Follow-up: Why can the value differ from an exact ranking-based AUC? Because Keras uses a finite threshold grid and a Riemann-sum approximation, so increasing num_thresholds can improve resolution.
curve selects the curve type: ROC is the default, while PR requests the precision-recall curve. ROC-AUC and PR-AUC evaluate model behavior across operational points rather than one classification threshold.
Follow-up: When might PR-AUC be preferable? It is often more informative when positive examples are uncommon because it focuses on precision and recall.
The metric is stateful: update_state() accumulates the batch statistics, and result() returns the current scalar AUC. A reset_state() call is used to clear accumulated state before a new evaluation period.
Follow-up: What happens if state is not reset? Results combine observations from multiple periods, so the reported metric no longer represents only the intended dataset.
keras.metrics.Precisionkeras.metrics.Recallkeras.metrics.TruePositiveskeras.metrics.TrueNegativeskeras.metrics.FalsePositiveskeras.metrics.FalseNegativeskeras.metrics.PrecisionAtRecall