The MLOps Challenge
Moving ML models from notebooks to production requires robust pipelines for training, versioning, and deployment. MLflow provides a comprehensive platform for this.
MLflow Components
- Tracking: Log experiments, parameters, metrics
- Projects: Package ML code for reproducibility
- Models: Manage and deploy models
- Registry: Central model store with versioning
Experiment Tracking
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score
mlflow.set_tracking_uri("http://mlflow-server:5000")
mlflow.set_experiment("customer-churn")
with mlflow.start_run():
# Log parameters
mlflow.log_param("n_estimators", 100)
mlflow.log_param("max_depth", 10)
# Train model
model = RandomForestClassifier(n_estimators=100, max_depth=10)
model.fit(X_train, y_train)
# Log metrics
predictions = model.predict(X_test)
mlflow.log_metric("accuracy", accuracy_score(y_test, predictions))
mlflow.log_metric("f1_score", f1_score(y_test, predictions))
# Log model
mlflow.sklearn.log_model(model, "model")Pipeline Definition
# MLproject file
name: customer-churn
conda_env: conda.yaml
entry_points:
preprocess:
parameters:
input_path: path
command: "python preprocess.py --input {input_path}"
train:
parameters:
n_estimators: {type: int, default: 100}
max_depth: {type: int, default: 10}
command: "python train.py --n_estimators {n_estimators} --max_depth {max_depth}"
evaluate:
parameters:
model_uri: str
command: "python evaluate.py --model_uri {model_uri}"Model Registry
# Register model
result = mlflow.register_model(
"runs:/abc123/model",
"customer-churn-model"
)
# Transition to production
client = mlflow.tracking.MlflowClient()
client.transition_model_version_stage(
name="customer-churn-model",
version=1,
stage="Production"
)Model Deployment
REST API Serving
# Serve model as REST API
mlflow models serve -m models:/customer-churn-model/Production -p 5001Docker Deployment
# Build Docker image
mlflow models build-docker -m models:/customer-churn-model/Production -n churn-model
# Run container
docker run -p 5001:8080 churn-modelInference Code
import requests
import json
def predict(features):
response = requests.post(
"http://localhost:5001/invocations",
headers={"Content-Type": "application/json"},
data=json.dumps({"inputs": features})
)
return response.json()Automated Retraining
from airflow import DAG
from airflow.operators.python import PythonOperator
def retrain_model():
# Run MLflow project
mlflow.projects.run(
uri=".",
entry_point="train",
parameters={"n_estimators": 100}
)
dag = DAG('ml_retrain', schedule_interval='@weekly')
retrain = PythonOperator(
task_id='retrain_model',
python_callable=retrain_model,
dag=dag
)Best Practices
- Version everything: data, code, models
- Automate testing for model quality
- Monitor model drift in production
- Implement gradual rollouts (canary deployments)
- Document model cards for each version
Conclusion
MLOps brings DevOps practices to machine learning. MLflow provides the foundation for reproducible, scalable ML pipelines in production.