ML Notes
Monitoring ML models in production: performance metrics, data drift detection, alerting, and logging.
Monitoring ensures models perform as expected in production and alerts to issues before they impact users.
What to Monitor
1. Model Performance Metrics
Predictions:
- Latency: time to generate prediction (target: <100ms)
- Throughput: predictions per second (scale requirements)
- Error rate: crashes, exceptions (target: <0.1%)
Quality:
- Accuracy on recent data (baseline)
- Precision, recall for classification
- RMSE, MAE for regression
2. Data Quality Metrics
Input validation:
- Missing values percentage
- Out-of-range values
- Null/NaN counts
Data distribution:
- Feature mean, std deviation
- Min, max, percentiles
- Expected range violations
3. Data Drift Detection
Input distribution changed from training:
Training data
Age: mean=45, std=15, range=[20,70]
Income: mean=$50k, range=[$20k, $200k]
Production (Week 1)
Age: mean=47, std=16 ← small change, OK
Income: mean=$48k
Production (Week 8)
Age: mean=52, std=20 ← significant drift!
Income: mean=$35k ← significant drift!
→ Model may perform worse
Detection methods:
- Statistical tests: Kolmogorov-Smirnov test (p < 0.05 = drift)
- Distribution metrics: Compare CDFs
- Statistical distances: Wasserstein distance, Maximum Mean Discrepancy
4. Model Drift Detection
Model performance degrading over time:
| Week 1 | Accuracy 95% |
| Week 2 | Accuracy 93% (OK, natural variation) |
| Week 3 | Accuracy 91% |
| Week 4 | Accuracy 88% (significant drop!) |
| Week 5 | Accuracy 82% (critical, retrain needed) |
5. Operational Metrics
System health:
- CPU, memory usage
- Request queue length
- Model loading time
- Database latency
Monitoring Implementation
Logging Predictions
Performance Metrics with Prometheus
Data Drift Detection
Performance Degradation Detection
class PerformanceMonitor:
def __init__(self, baseline_accuracy, alert_threshold=0.05):
self.baseline = baseline_accuracy
self.alert_threshold = alert_threshold
def check_performance(self, y_true, y_pred):
"""Check if performance has degraded"""
from sklearn.metrics import accuracy_score
current_acc = accuracy_score(y_true, y_pred)
degradation = (self.baseline - current_acc) / self.baseline
# Alert if >5% degradation
if degradation > self.alert_threshold:
self.send_alert(
f"Performance degradation: {degradation:.1%}",
f"Accuracy dropped from {self.baseline:.3f} to {current_acc:.3f}",
severity="critical"
)
# Trigger retraining
trigger_retraining()
return {
'current_accuracy': current_acc,
'baseline_accuracy': self.baseline,
'degradation': degradation,
'alert_triggered': degradation > self.alert_threshold
}
def send_alert(self, title, message, severity="warning"):
"""Send alert (email, Slack, PagerDuty, etc.)"""
pass
# Use it
monitor = PerformanceMonitor(baseline_accuracy=0.95, alert_threshold=0.05)
# Check after getting predictions
results = monitor.check_performance(y_test, y_pred)Monitoring Dashboard
# Example: Grafana dashboard queries
# Query 1: Prediction latency over time
SELECT
timestamp,
avg(ml_prediction_latency_seconds) as latency
FROM metrics
WHERE model_version = 'v1.0'
GROUP BY time(1m)
# Query 2: Prediction volume
SELECT
timestamp,
sum(ml_predictions_total) as total_predictions
FROM metrics
WHERE model_version = 'v1.0'
GROUP BY time(1h)
# Query 3: Model accuracy trend
SELECT
timestamp,
ml_model_accuracy
FROM metrics
WHERE model_version = 'v1.0'
ORDER BY timestampAlerting Strategies
Alert Rules
Alert Fatigue Prevention
- Set reasonable thresholds (not too sensitive)
- Group related alerts
- Require consecutive failures (not single anomaly)
- Time-based alert deduplication (max 1 alert per hour)
Monitoring for Specific Use Cases
Classification Model Monitoring
Metrics
- Class distribution (any class disappearing?)
- Precision/recall per class
- Confusion matrix changes
- Calibration (predicted proba vs actual)
Alerts
- Minority class disappears
- Precision drops >X%
- Model overconfident (high proba, wrong predictions)
Regression Model Monitoring
Metrics
- Residual distribution
- Mean absolute error
- Root mean squared error
- Prediction range vs actual range
Alerts
- RMSE increased >X%
- Predictions biased (mean error != 0)
- Outlier predictions (outside expected range)
Ranking/Recommendation Monitoring
Metrics
- NDCG@10 (ranking quality)
- Click-through rate
- Conversion rate
- Recommendation diversity
Alerts
- CTR dropped >X%
- Diversity decreased (recommending same items)
- User satisfaction score decreased
Summary
Monitoring ensures production reliability:
- Track performance metrics
- Monitor data quality
- Detect drift (data and model)
- Alert on issues
- Trigger retraining when needed
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for ML Model Monitoring & Observability.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, mlops, model, monitoring, ml model monitoring & observability
Related Machine Learning Topics