ML lifecycle management with MLflow. Track experiments, package models, manage registries, and deploy models. Use for ML operations, experiment tracking, and model deployment.
ML lifecycle management with MLflow. Track experiments, package models, manage registries, and deploy models. Use for ML operations, experiment tracking, and model deployment.
MLflow Skill
Complete guide for MLflow - ML lifecycle platform.
Quick Reference
Components
Component
Description
Tracking
Log experiments
Projects
Package ML code
Models
Model packaging
Registry
Model versioning
Serving
Model deployment
CLI Commands
mlflow run . # Run project
mlflow ui # Start UI
mlflow models serve -m model # Serve model
mlflow server # Start tracking server
1. Installation
# Core
pip install mlflow
# With extras
pip install mlflow[extras]
# Specific integrations
pip install mlflow[gateway] # AI Gateway
pip install mlflow[genai] # GenAI tracking
# Create run manually
run = mlflow.start_run(run_name="my-run")
try:
mlflow.log_param("param1", "value1")
mlflow.log_metric("metric1", 0.9)
finally:
mlflow.end_run()
# Get run info
run_id = run.info.run_id
print(f"Run ID: {run_id}")
# Resume runwith mlflow.start_run(run_id=run_id):
mlflow.log_metric("additional_metric", 0.95)
Nested Runs
with mlflow.start_run(run_name="parent"):
mlflow.log_param("parent_param", "value")
for i inrange(3):
with mlflow.start_run(run_name=f"child_{i}", nested=True):
mlflow.log_param("child_param", i)
mlflow.log_metric("child_metric", i * 0.1)
3. Tracking Server
Start Server
# Local file store
mlflow server --host 0.0.0.0 --port 5000
# With database backend
mlflow server \
--backend-store-uri postgresql://user:pass@localhost/mlflow \
--default-artifact-root s3://mlflow-artifacts/ \
--host 0.0.0.0 \
--port 5000
# With SQLite
mlflow server \
--backend-store-uri sqlite:///mlflow.db \
--default-artifact-root ./mlruns \
--host 0.0.0.0
Connect to Server
import mlflow
mlflow.set_tracking_uri("http://localhost:5000")
# Or via environment# export MLFLOW_TRACKING_URI=http://localhost:5000
Docker Compose
services:mlflow:image:ghcr.io/mlflow/mlflow:latestports:-"5000:5000"environment:-MLFLOW_BACKEND_STORE_URI=postgresql://mlflow:mlflow@postgres/mlflow-MLFLOW_DEFAULT_ARTIFACT_ROOT=s3://mlflow-artifacts/-AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}-AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}command:>
mlflow server
--host 0.0.0.0
--port 5000
depends_on:-postgrespostgres:image:postgres:15environment:-POSTGRES_USER=mlflow-POSTGRES_PASSWORD=mlflow-POSTGRES_DB=mlflowvolumes:-postgres_data:/var/lib/postgresql/datavolumes:postgres_data:
4. Model Logging
Log Scikit-learn Model
from sklearn.ensemble import RandomForestClassifier
import mlflow.sklearn
model = RandomForestClassifier()
model.fit(X_train, y_train)
with mlflow.start_run():
# Log model
mlflow.sklearn.log_model(
model,
artifact_path="model",
registered_model_name="my-rf-model"
)
# With signaturefrom mlflow.models import infer_signature
signature = infer_signature(X_train, model.predict(X_train))
mlflow.sklearn.log_model(model, "model", signature=signature)
Log PyTorch Model
import mlflow.pytorch
with mlflow.start_run():
mlflow.pytorch.log_model(
pytorch_model=model,
artifact_path="model",
conda_env="conda.yaml",
code_paths=["./src"]
)
# From run
model = mlflow.sklearn.load_model(f"runs:/{run_id}/model")
# From registry
model = mlflow.sklearn.load_model("models:/my-model/1")
model = mlflow.sklearn.load_model("models:/my-model/Production")
# As PyFunc
model = mlflow.pyfunc.load_model(f"runs:/{run_id}/model")
predictions = model.predict(X_test)
5. Model Registry
Register Model
# During logging
mlflow.sklearn.log_model(
model,
"model",
registered_model_name="my-model"
)
# After logging
result = mlflow.register_model(
model_uri=f"runs:/{run_id}/model",
name="my-model"
)
# Set alias (MLflow 2.0+)
client.set_registered_model_alias(
name="my-model",
alias="champion",
version=1
)
# Load by alias
model = mlflow.pyfunc.load_model("models:/my-model@champion")
# Delete alias
client.delete_registered_model_alias(
name="my-model",
alias="champion"
)
6. Model Serving
Local Serving
# Serve from run
mlflow models serve -m runs:/<run_id>/model -p 5001
# Serve from registry
mlflow models serve -m models:/my-model/Production -p 5001
# With environment
mlflow models serve -m models:/my-model/1 -p 5001 --env-manager=conda
# Local
mlflow run . -P learning_rate=0.001 -P epochs=50
# From Git
mlflow run https://github.com/user/repo -P param=value
# Specific entry point
mlflow run . -e validate -P model_path=./model
# With environment
mlflow run . --env-manager=conda