MLflow 入门教程:四大组件实战
1. MLflow 的四大支柱
Tracking ──── 记录参数/指标/模型 artifact
Projects ──── 打包训练代码为可复现单元
Models ──── 统一模型格式,跨部署平台
Registry ──── 模型版本管理与审批流转
2. Tracking:手动 vs 自动
autolog(最简单)
mlflow.autolog() # 支持 PyTorch、TF、XGBoost、LightGBM、spaCy...
手动记录(更灵活)
with mlflow.start_run(run_name="my_run"):
mlflow.log_param("learning_rate", 0.01)
mlflow.log_param("batch_size", 32)
mlflow.log_metric("accuracy", 0.95, step=1)
mlflow.log_metric("accuracy", 0.96, step=2) # 支持 step 画出曲线
mlflow.log_artifact("confusion_matrix.png") # 保存任何文件
嵌套 Run(Hyperparameter Search)
with mlflow.start_run(run_name="grid_search"):
for lr in [0.1, 0.01, 0.001]:
with mlflow.start_run(run_name=f"lr_{lr}", nested=True):
mlflow.log_param("lr", lr)
# train...
3. Models:保存与加载
# 保存模型(自动推断 flavor)
signature = infer_signature(X_train, model.predict(X_train))
mlflow.sklearn.log_model(model, "model", signature=signature)
# 加载模型
loaded_model = mlflow.sklearn.load_model("runs:/abc123/model")
# 或使用 registry 中的模型
model = mlflow.pyfunc.load_model("models:/my_model/Production")
4. Registry:版本管理
from mlflow.tracking import MlflowClient
client = MlflowClient()
# 注册模型
client.create_registered_model("breast_cancer_classifier")
client.create_model_version("breast_cancer_classifier", "runs:/abc123/model", "v1")
# 模型晋升
client.transition_model_version_stage(
name="breast_cancer_classifier",
version="1",
stage="Production",
archive_existing_versions=True, # 打掉旧 Production
)
Stage 流转:None → Staging → Production → Archived
5. 模型签名(Signature)
from mlflow.models.signature import infer_signature
signature = infer_signature(X_sample, model.predict(X_sample))
mlflow.log_model(model, "model", signature=signature)
# 签名保证:部署时自动验证输入 schema
6. 搜索实验(程序化)
runs = mlflow.search_runs(
experiment_ids=["1"],
filter_string="metrics.accuracy > 0.9 and params.max_depth < '10'",
order_by=["metrics.accuracy DESC"],
)
print(runs[["params.max_depth", "metrics.accuracy"]])
7. MLflow 部署选项
mlflow models serve -m models:/my_model/1 -p 1234
mlflow models build-docker -m models:/my_model/1 -n my-model
思考题
- MLflow Tracking 和 Weights & Biases (wandb) 的核心差异是什么?
- 模型 Registry 的 Staging → Production 过渡中应该加什么验证步骤?
- 为什么需要模型签名?推理时能防止什么类型的错误?