When a model is deployed, we want to check if it keeps working well over time. Key metrics include accuracy, precision, recall, and F1 score. These show if the model still predicts correctly. We also watch data drift and prediction distribution to see if new data looks different from training data. This helps catch problems early.
Monitoring deployed models in TensorFlow - Model Metrics & Evaluation
Actual \ Predicted | Positive | Negative
-------------------|----------|---------
Positive | 85 | 15
Negative | 10 | 90
Total samples = 85 + 15 + 10 + 90 = 200
Precision = TP / (TP + FP) = 85 / (85 + 10) = 0.894
Recall = TP / (TP + FN) = 85 / (85 + 15) = 0.85
F1 Score = 2 * (Precision * Recall) / (Precision + Recall) ≈ 0.871
Imagine a deployed email spam filter:
- High precision means most emails marked as spam really are spam. This avoids losing good emails.
- High recall means catching most spam emails, but might mark some good emails as spam.
Depending on user needs, we monitor which metric to keep balanced. If users complain about missing spam, increase recall. If they complain about losing good emails, increase precision.
Good: Accuracy above 85%, precision and recall above 80%, stable over time.
Bad: Sudden drops in accuracy or recall, precision falling below 50%, or big changes in prediction patterns.
Good metrics mean the model still understands data well. Bad metrics warn us to retrain or fix the model.
- Accuracy paradox: High accuracy but poor recall on important classes can hide problems.
- Data leakage: If monitoring data leaks future info, metrics look falsely good.
- Overfitting signs: Metrics suddenly perfect on monitoring data but bad in real use.
- Ignoring data drift: Not checking if new data differs from training data can cause unnoticed failures.
Your deployed fraud detection model has 98% accuracy but only 12% recall on fraud cases. Is it good for production? Why or why not?
Answer: No, it is not good. The model misses 88% of fraud cases (low recall), which is dangerous. High accuracy is misleading because fraud is rare. We need to improve recall to catch more fraud.
Practice
Solution
Step 1: Understand model deployment goals
After deployment, models must be checked regularly to ensure they perform well and remain accurate.Step 2: Identify monitoring purpose
Monitoring focuses on tracking model health and accuracy, not training speed or size reduction.Final Answer:
To check model health and accuracy over time -> Option BQuick Check:
Monitoring = model health and accuracy check [OK]
- Confusing monitoring with training or model design
- Thinking monitoring changes model architecture
- Assuming monitoring reduces model size
Solution
Step 1: Identify tools for model monitoring
TensorFlow Model Analysis (tfma) is designed to evaluate and monitor models by defining metrics and slices.Step 2: Differentiate from other tools
TensorBoard is for visualization, TensorFlow Lite is for mobile deployment, and TensorFlow Hub is for reusable model components.Final Answer:
TensorFlow Model Analysis (tfma) -> Option AQuick Check:
tfma = metrics and slices tool [OK]
- Confusing TensorBoard with tfma
- Using TensorFlow Lite for monitoring
- Mixing TensorFlow Hub with monitoring tools
import tensorflow_model_analysis as tfma
config = tfma.EvalConfig(
model_specs=[tfma.ModelSpec(label_key='label')],
metrics_specs=[tfma.MetricsSpec(metrics=[tfma.MetricConfig(class_name='ExampleCount')])]
)
result = tfma.run_model_analysis(
eval_shared_model=tfma.default_eval_shared_model('model_dir'),
eval_config=config,
eval_input_receiver_fn=None
)
print(result.metrics['']['']['ExampleCount'])Solution
Step 1: Analyze the code snippet
The code calls tfma.run_model_analysis with eval_input_receiver_fn set to None, which is required for input data processing.Step 2: Understand the impact of missing eval_input_receiver_fn
Without a valid eval_input_receiver_fn, the evaluation cannot run properly and will raise an error.Final Answer:
Error due to missing input_receiver_fn -> Option AQuick Check:
Missing input_receiver_fn causes error [OK]
- Assuming metrics print correctly without input_receiver_fn
- Confusing ExampleCount with accuracy or loss
- Ignoring required input functions in tfma
Solution
Step 1: Identify why metrics don't update
If metrics don't update, it usually means the evaluation process is not running after deployment.Step 2: Check scheduling of evaluation jobs
Evaluation jobs must be scheduled regularly to monitor model performance over time.Final Answer:
The evaluation job is not scheduled to run regularly -> Option DQuick Check:
Regular evaluation runs update metrics [OK]
- Blaming training epochs for monitoring issues
- Assuming architecture changes affect monitoring directly
- Thinking data size stops metric updates
Solution
Step 1: Understand slicing in tfma
Slices allow monitoring metrics for specific data segments, such as user groups, to detect performance differences.Step 2: Apply slices in EvalConfig
By defining slices in tfma.EvalConfig, you can track metrics separately for each user group without retraining multiple models.Final Answer:
Define slices in tfma.EvalConfig to segment metrics by user group -> Option CQuick Check:
Slices segment metrics for groups [OK]
- Thinking retraining is needed for monitoring groups
- Using TensorBoard only for overall metrics
- Ignoring monitoring and deploying multiple models blindly
Ready to prove it?
Pass the test and this pattern moves unknown
