| 概念 | 说明 |
|---|---|
| Experiment | 实验集合(如“文本分类 v1”) |
| Run | 一次具体实验(一次训练) |
| Parameter | 超参数(如 learning_rate) |
| Metric | 可量化指标(如 accuracy、loss) |
| Artifact | 输出文件(模型、日志、图片) |
| Tracking URI | 实验记录存储位置 |
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")常见方式:
| 场景 | Tracking URI |
|---|---|
| 本地 | mlruns/(默认) |
| 本地服务 | http://127.0.0.1:5000 |
| 远程服务器 | http://your-server:5000 |
| 云存储 | s3://bucket/mlflow |
mlflow.set_experiment("bert-text-classification")✅ 建议:
with mlflow.start_run():
mlflow.log_param("learning_rate", 5e-5)
mlflow.log_param("epochs", 3)
mlflow.log_metric("train_loss", 0.3)
mlflow.log_metric("val_accuracy", 0.85)
mlflow.log_artifact("config.yaml")✅ 最佳实践:
mlflow.sklearn.log_model(model, "model")或 PyTorch / TensorFlow:
mlflow.pytorch.log_model(model, "model")推荐格式:
<项目名>-<任务>-<版本>例:
nlp-sentiment-v1
cv-resnet50-aug
recsys-ctr-baselinewith mlflow.start_run(run_name="lr=5e-5-bs=32"):
...import git
mlflow.log_param("git_commit", git.Repo().head.commit)
mlflow.log_artifact("requirements.txt")mlflow.set_tag("model", "BERT")
mlflow.set_tag("dataset", "IMDB")
mlflow.set_tag("status", "tuning")常用 tag:
mlflow uifrom mlflow.tracking import MlflowClient
client = MlflowClient()
runs = client.search_runs(experiment_ids=["1"])client.delete_run(run_id)或定期:
不要只用本地文件
推荐架构:
Tracking Server
├── Backend Store (MySQL / PostgreSQL)
└── Artifact Store (S3 / MinIO / NFS)示例:
mlflow server \
--backend-store-uri mysql+pymysql://user:pwd@host/mlflow \
--default-artifact-root s3://mlflow-artifacts \
--host 0.0.0.01. 新建 Experiment
2. 固定随机种子
3. 记录参数 / 指标 / 模型
4. 用 Tag 分类
5. UI 对比实验
6. 选出最优 Run
7. 注册模型(Model Registry)| 问题 | 解决 |
|---|---|
| 指标覆盖 | 每条 metric 记录不同 step |
| artifact 丢失 | artifact 放对象存储 |
| 实验混乱 | 统一命名规范 |
| 不可复现 | 记录 Git + 环境 |
mlflow.autolog()如果你愿意,我可以帮你:
你现在是在 本地实验 还是 团队/生产环境?