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

TensorFlow · Class

metrics.Recall

Computes recall from accumulated true positives and false negatives. It supports threshold-based, top-k, and class-specific evaluation.

How it works

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.

Syntax

tf.keras.metrics.Recall(thresholds=None, top_k=None, class_id=None, name=None, dtype=None)

Parameters

NameTypeDescription
thresholdsfloat | list/tuple[float] | NoneA float or list/tuple of floats in [0, 1]; each value determines a prediction threshold.
top_kint | NoneThe number of highest-scoring predictions considered for recall.
class_idint | NoneThe class ID for which binary recall is calculated.
namestr | NoneThe metric instance name.
dtypestr | tf.dtypes.DType | NoneThe metric data type.

Returns

result() returns the current recall value as a tensor; multiple thresholds produce corresponding metric values.

Key points

  • The metric stores true-positive and false-negative counters as its local state.
  • Supplying multiple thresholds produces one recall value per threshold.
  • The default configuration evaluates binary predictions at a 0.5 threshold.
  • With top_k, recall measures how often labeled classes appear among the highest-k predictions.
  • class_id restricts the calculation to examples whose labels contain that class.

Common mistakes

  • When predictions are logits rather than sigmoid probabilities, set the threshold to 0.
  • Do not configure class_id and top_k together.
  • A class_id must be valid for the prediction tensor's class dimension.

Example: Accumulate binary recall

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.

When to use it

Threshold-based evaluation

Binary classification with probability outputs.

Top-k recall

Multiclass classification where ranking matters more than a fixed cutoff.

Per-class recall

Monitoring one target class in a multiclass model.

Interview questions

QHow does Recall calculate its result?

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.

QWhat threshold does Recall use by default?

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.

QWhat do top_k and class_id control?

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.Precision
  • tf.keras.metrics.AUC
  • tf.keras.metrics.PrecisionAtRecall
  • tf.keras.Metric

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