Threshold-based evaluation
Binary classification with probability outputs.
TensorFlow · Class
Computes recall from accumulated true positives and false negatives. It supports threshold-based, top-k, and class-specific evaluation.
Recall is a stateful Keras metric that accumulates true positives and false negatives across updates. Its result divides true positives by their sum with false negatives; thresholds, top-k selection, or a class ID control how predictions are interpreted.
tf.keras.metrics.Recall(thresholds=None, top_k=None, class_id=None, name=None, dtype=None)| Name | Type | Description |
|---|---|---|
thresholds | float | list/tuple[float] | None | A float or list/tuple of floats in [0, 1]; each value determines a prediction threshold. |
top_k | int | None | The number of highest-scoring predictions considered for recall. |
class_id | int | None | The class ID for which binary recall is calculated. |
name | str | None | The metric instance name. |
dtype | str | tf.dtypes.DType | None | The metric data type. |
result() returns the current recall value as a tensor; multiple thresholds produce corresponding metric values.
import tensorflow as tf
recall = tf.keras.metrics.Recall()
recall.update_state([1, 0, 1], [0.9, 0.8, 0.2])
print(float(recall.result()))Creates a recall metric, accumulates one batch of labels and prediction scores, then prints the resulting recall.
Binary classification with probability outputs.
Multiclass classification where ranking matters more than a fixed cutoff.
Monitoring one target class in a multiclass model.
Recall is computed from accumulated true positives and false negatives: true positives divided by their sum with false negatives. The metric keeps these values as state so multiple updates can contribute to one result.
Follow-up: How would you make the metric batch-independent? Use the metric object's accumulated state across batches and read result only after all updates.
Without top_k or explicit thresholds, Recall uses a threshold of 0.5. Each prediction is classified as positive when it is above the configured threshold.
Follow-up: What changes for logits? Use thresholds=0 because logits have not passed through sigmoid.
top_k restricts evaluation to the highest-ranked predictions, while class_id limits binary evaluation to one class. These options provide alternatives to ordinary threshold-based binary evaluation.
Follow-up: Can both options be supplied? No; the documentation requires configuring at most one of them.
tf.keras.metrics.Precisiontf.keras.metrics.AUCtf.keras.metrics.PrecisionAtRecalltf.keras.Metric