Custom metrics let you measure exactly what matters for your problem. Sometimes built-in metrics like accuracy or loss don't tell the full story. For example, if you want to track how many predictions are above a certain threshold or measure a special score, a custom metric helps. It gives you control to see if your model is really learning what you care about.
Custom metrics in TensorFlow - Model Metrics & Evaluation
Confusion Matrix Example (for classification):
Predicted
Pos Neg
Actual Pos TP FN
Neg FP TN
Custom metrics can also be numeric values like mean squared error or any function you define.
Example: Custom metric tracking how many predictions > 0.5
Predictions: [0.7, 0.2, 0.9, 0.4]
Count > 0.5 = 2
Custom metrics can help balance tradeoffs like precision and recall by defining exactly what you want to measure.
For example, if you want to track how many positive predictions are correct (precision), you can write a custom metric for that.
Or if you want to track how many actual positives you found (recall), you can create a custom metric for recall.
This helps you understand if your model is better at avoiding false alarms or catching all positives.
Good custom metric values depend on what you measure. For example, if your custom metric counts correct predictions above a threshold, a higher number is good.
Bad values mean your metric shows your model is not doing what you want. For example, if your custom metric measures how often predictions are above 0.5 but the value is very low, your model might be underconfident.
Always compare your custom metric values to a baseline or random guess to know if your model is improving.
- Defining metrics that are not differentiable or incompatible with training can cause errors.
- Metrics that are too complex or slow can slow down training.
- Confusing metric output with loss: metrics measure performance but do not guide training directly.
- Not resetting metric state between epochs can give wrong results.
- Using metrics that do not match your problem goals can mislead model evaluation.
No, it is not good for fraud detection. The low recall (12%) means your model misses most fraud cases, which is dangerous. Even though accuracy is high, it is misleading because fraud is rare. You want high recall to catch as many fraud cases as possible.
Practice
Solution
Step 1: Understand what metrics do
Metrics measure how well the model performs by comparing true labels and predictions.Step 2: Identify the role of custom metrics
Custom metrics let you define your own way to measure performance during training.Final Answer:
To track a special score based on true labels and predictions during training -> Option AQuick Check:
Custom metric = special score tracking [OK]
- Confusing metrics with model architecture changes
- Thinking metrics speed up training
- Believing metrics save model weights
Solution
Step 1: Check TensorFlow compatibility
Custom metrics must use TensorFlow operations like tf.reduce_mean for compatibility.Step 2: Identify correct function
def my_metric(y_true, y_pred): return tf.reduce_mean(tf.abs(y_true - y_pred)) uses tf.reduce_mean and tf.abs correctly; def my_metric(y_true, y_pred): return np.mean(np.abs(y_true - y_pred)) uses numpy which is incompatible.Final Answer:
def my_metric(y_true, y_pred): return tf.reduce_mean(tf.abs(y_true - y_pred)) -> Option DQuick Check:
Use TensorFlow ops in custom metrics [OK]
- Using numpy functions inside metric causing errors
- Returning tensors without reduction to scalar
- Printing values instead of returning metric
def custom_metric(y_true, y_pred):
diff = tf.abs(y_true - y_pred)
return tf.reduce_mean(diff)Solution
Step 1: Calculate absolute differences
Differences: |1-1|=0, |2-2|=0, |3-4|=1Step 2: Compute mean of differences
Mean = (0 + 0 + 1) / 3 = 0.3333Final Answer:
0.3333 -> Option CQuick Check:
Mean absolute difference = 0.3333 [OK]
- Summing differences without averaging
- Using squared difference instead of absolute
- Mixing up y_true and y_pred order
def faulty_metric(y_true, y_pred):
return tf.reduce_sum(y_true - y_pred)Solution
Step 1: Understand metric output
Metrics should return a scalar representing average error, not total sum.Step 2: Fix by averaging
Replacing tf.reduce_sum with tf.reduce_mean returns average difference, a better metric.Final Answer:
Use tf.reduce_mean instead of tf.reduce_sum to get average error -> Option BQuick Check:
Average error metric needs reduce_mean [OK]
- Using reduce_sum causing large values
- Mixing TensorFlow and numpy operations
- Not returning a scalar metric
Solution
Step 1: Identify correct comparison and casting
We must compare differences with 0.5 and cast boolean to float for summation.Step 2: Check TensorFlow functions used
def count_large_diff(y_true, y_pred): diff = tf.abs(y_true - y_pred) return tf.reduce_sum(tf.cast(diff > 0.5, tf.float32)) uses tf.abs, comparison, tf.cast to float32, and tf.reduce_sum correctly.Final Answer:
def count_large_diff(y_true, y_pred): diff = tf.abs(y_true - y_pred) return tf.reduce_sum(tf.cast(diff > 0.5, tf.float32)) -> Option AQuick Check:
Cast booleans to float before summing [OK]
- Not casting boolean mask before summing
- Using reduce_mean instead of reduce_sum
- Using wrong data type cast like tf.int32
Ready to prove it?
Pass the test and this pattern moves unknown
