Keras
decision threshold
precision
recall
machine learning

Keras custom decision threshold for precision and recall

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In binary classification, the model usually outputs a score or probability, and the decision threshold determines where you turn that score into a positive or negative class label. Changing the threshold does not retrain the model, but it can dramatically change precision and recall, which is why threshold selection belongs in evaluation and deployment logic.

Why the Threshold Matters

The default threshold is often 0.5, but that is only a convention. If false positives are expensive, you may want a higher threshold. If missing positives is worse, you may want a lower threshold.

For example:

  • higher threshold usually increases precision and lowers recall
  • lower threshold usually increases recall and lowers precision

That tradeoff is exactly why threshold tuning exists.

In imbalanced classification problems, this matters even more because a model can look good at threshold 0.5 on overall accuracy while still performing poorly on the class you actually care about.

Set Custom Thresholds in Keras Metrics

Keras metrics such as Precision and Recall accept thresholds directly, which makes it easy to monitor the model under the operating point you actually care about.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(10,)),
5    tf.keras.layers.Dense(1, activation="sigmoid"),
6])
7
8model.compile(
9    optimizer="adam",
10    loss="binary_crossentropy",
11    metrics=[
12        tf.keras.metrics.Precision(thresholds=0.7, name="precision_at_07"),
13        tf.keras.metrics.Recall(thresholds=0.7, name="recall_at_07"),
14    ],
15)

This does not change the model's output layer. It changes how the predicted probabilities are interpreted when computing the metric.

That makes custom thresholds especially useful during model selection, because you can monitor the exact operating point that matches the application's tolerance for false positives and false negatives.

You can even monitor several thresholds at once:

python
1metrics = [
2    tf.keras.metrics.Precision(thresholds=[0.3, 0.5, 0.7]),
3    tf.keras.metrics.Recall(thresholds=[0.3, 0.5, 0.7]),
4]

That is very useful when choosing a deployment threshold from validation results.

Apply the Threshold Explicitly at Inference Time

During prediction, you usually make the thresholding step yourself.

python
1import numpy as np
2
3probs = np.array([0.12, 0.44, 0.68, 0.91])
4threshold = 0.7
5preds = (probs >= threshold).astype(int)
6
7print(preds)  # [0 0 0 1]

This is important because the threshold used during evaluation should match the threshold used in production decisions. Otherwise, the precision and recall you measured are not the ones your deployed system will actually achieve.

Do Not Confuse Metrics with the Loss

Changing a metric threshold does not mean the model is optimizing precision or recall directly during training. In a standard setup, the model still optimizes the loss, such as binary cross-entropy.

That distinction matters:

  • loss drives training
  • thresholded precision and recall describe performance at a chosen operating point

If you need a model tuned for a specific operating region, the usual approach is to train normally, then choose the threshold on a validation set based on the business objective.

Many teams use a precision-recall curve or a validation sweep across candidate thresholds to find that operating point instead of guessing a threshold up front.

Common Pitfalls

  • Assuming the default 0.5 threshold is automatically the best one.
  • Changing the metric threshold and thinking the model is now training against that threshold directly.
  • Reporting precision and recall at one threshold but deploying a different threshold in production.
  • Choosing the threshold on the test set instead of a validation set and leaking evaluation information.
  • Tuning the threshold once and forgetting that it may need reevaluation if class balance or business costs change over time.

Summary

  • Precision and recall depend on the decision threshold, not just on model weights.
  • Keras metrics can evaluate precision and recall at custom thresholds.
  • Threshold selection should usually happen after training, using validation data.
  • Use the same threshold in deployment that you used when reporting the chosen operating point.
  • Metrics describe thresholded behavior; they do not automatically change the training objective.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.