A custom metric is a user-defined way to measure how well a model performs, beyond the built-in metrics. It helps track specific goals during training.
Click to reveal answer
beginner
How do you define a custom metric function in TensorFlow?
You write a Python function that takes true labels and predicted labels as inputs and returns a single number showing performance, like accuracy or error rate.
Click to reveal answer
intermediate
Why might you want to create a custom metric instead of using built-in ones?
Built-in metrics might not capture what matters most for your problem. Custom metrics let you measure exactly what you care about, like a special kind of accuracy or error.
Click to reveal answer
advanced
What is the role of the 'update_state' method in a custom metric class in TensorFlow?
It updates the metric's internal variables with new data from each batch during training, helping to keep track of the metric over time.
Click to reveal answer
beginner
How do you add a custom metric to a TensorFlow model during compilation?
You pass the custom metric function or metric class instance to the 'metrics' argument in model.compile(), so it runs during training and evaluation.
Click to reveal answer
What inputs does a custom metric function in TensorFlow usually take?
ATrue labels and predicted labels
BOnly predicted labels
COnly true labels
DModel weights
✗ Incorrect
A custom metric function compares true labels and predicted labels to calculate performance.
Which method in a TensorFlow custom metric class updates the metric with new batch data?
Aupdate_state()
Bresult()
Creset_states()
Dcompile()
✗ Incorrect
update_state() is called each batch to update metric calculations.
Why use a custom metric instead of built-in metrics?
ABecause built-in metrics are slower
BTo measure a specific performance aspect not covered by built-in metrics
CTo avoid using labels
DTo change the model architecture
✗ Incorrect
Custom metrics let you track exactly what matters for your problem.
How do you include a custom metric in model training in TensorFlow?
AAdd it to the optimizer
BCall it after training
CReplace the loss function
DAdd it to the 'metrics' list in model.compile()
✗ Incorrect
Custom metrics are passed to the 'metrics' argument during model compilation.
What should a custom metric function return?
AThe training loss
BA list of predictions
CA single number representing the metric value
DThe model weights
✗ Incorrect
The function returns a single value that measures model performance.
Explain how to create and use a custom metric in TensorFlow from scratch.
Think about inputs, outputs, and integration with the model.
You got /4 concepts.
Describe why custom metrics are important and give an example scenario where built-in metrics are not enough.
Consider a real-life problem with special success criteria.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a custom metric in TensorFlow model training?
easy
A. To track a special score based on true labels and predictions during training
B. To change the model's architecture automatically
C. To speed up the training process by using GPU
D. To save the model weights after each epoch
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 A
Quick Check:
Custom metric = special score tracking [OK]
Hint: Custom metrics measure model performance uniquely during training [OK]
Common Mistakes:
Confusing metrics with model architecture changes
Thinking metrics speed up training
Believing metrics save model weights
2. Which of the following is the correct way to define a simple custom metric function in TensorFlow?
easy
A. def my_metric(y_true, y_pred): print(y_true, y_pred)
B. def my_metric(y_true, y_pred): return np.mean(np.abs(y_true - y_pred))
C. def my_metric(y_true, y_pred): return y_true + y_pred
D. def my_metric(y_true, y_pred): return tf.reduce_mean(tf.abs(y_true - y_pred))
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 D
Quick Check:
Use TensorFlow ops in custom metrics [OK]
Hint: Use TensorFlow ops, not numpy, inside custom metric functions [OK]
Common Mistakes:
Using numpy functions inside metric causing errors
Returning tensors without reduction to scalar
Printing values instead of returning metric
3. Given the following custom metric code, what will be the output metric value for y_true = [1, 2, 3] and y_pred = [1, 2, 4]?
A. Convert y_true and y_pred to numpy arrays first
B. Use tf.reduce_mean instead of tf.reduce_sum to get average error
C. Replace subtraction with addition
D. Add print statements to debug values
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 B
Quick Check:
Average error metric needs reduce_mean [OK]
Hint: Use reduce_mean for average, not reduce_sum [OK]
Common Mistakes:
Using reduce_sum causing large values
Mixing TensorFlow and numpy operations
Not returning a scalar metric
5. You want to create a custom metric that counts how many predictions differ from true labels by more than 0.5. Which of the following implementations is correct?