如何在本地正确启动 MLflow Tracking Server 并设置实验

10次阅读

如何在本地正确启动 MLflow Tracking Server 并设置实验

本文详解解决“cannot define a mlflow experiment”错误的核心原因:未启动本地 mlflow 后端服务;提供完整启动命令、验证步骤、常见坑点及最小可运行示例。

MLflow 的 mlflow.set_experiment() 报错(如 Max retries exceeded、Connection refused)绝大多数情况下并非权限或配置问题,而是根本未运行 MLflow Tracking Server。你代码中调用的 mlflow.set_tracking_uri(“https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365”) 是在尝试连接一个 HTTP 服务端,但该服务默认并不存在——它需要你手动启动。

✅ 正确步骤:先启服务,再写代码

1. 启动 MLflow Tracking Server(关键!)

在终端(命令行)中执行以下命令:

mlflow server    --host 127.0.0.1    --port 8080    --backend-store-uri sqlite:///mlflow.db    --default-artifact-root ./mlruns
  • –backend-store-uri: 使用 SQLite 存储元数据(实验、运行、参数等),首次运行会自动创建 mlflow.db 文件
  • –default-artifact-root: 指定模型、日志、图表等二进制文件的本地存储路径(./mlruns)
  • 服务启动后,你会看到类似 Running the mlflow tracking server at https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365 的提示,并保持运行状态(不要关闭该终端)

? 验证服务是否就绪:打开浏览器访问 https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365,应能加载 MLflow ui(空界面即表示成功)。

2. 运行你的 python 脚本(无需修改)

确保服务器已运行后,再执行原始代码:

import mlflow from mlflow.models import infer_signature import pandas as pd from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score  # ✅ 必须在 mlflow server 运行后执行 mlflow.set_tracking_uri("https://www.php.cn/link/1ce5e897cda6aeb211dffe8d514f4365") mlflow.set_experiment("MLflow Quickstart")  # 示例训练与记录(可选,用于验证全流程) with mlflow.start_run():     X, y = datasets.make_classification(n_samples=1000, n_features=5, random_state=42)     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)      model = LogisticRegression()     model.fit(X_train, y_train)     y_pred = model.predict(X_test)      mlflow.log_param("solver", "liblinear")     mlflow.log_metric("accuracy", accuracy_score(y_test, y_pred))     mlflow.sklearn.log_model(model, "model")

⚠️ 常见注意事项

  • 不要混用 file:// 和 http:// URI:file:///path/to/mlruns 是无服务的本地模式(跳过 server),而 http://… 必须搭配 mlflow server;二者不可混用。
  • 端口冲突? 若 8080 被占用,改用其他端口(如 –port 5000),并同步更新 set_tracking_uri。
  • windows 防火墙/杀软拦截? 极少数情况会阻止本地连接,临时禁用测试即可。
  • Databricks 替代方案? 如需免运维,可改用 mlflow.start_run() + file URI 快速起步(无 UI),或注册 Databricks Community Edition(需耐心等待审核邮件)。

✅ 总结

“Cannot define a MLflow experiment” 的本质是客户端找不到服务端——不是认证失败,而是服务未启动。牢记口诀:先 mlflow server,再 set_tracking_uri,最后 set_experiment。完成这三步,本地 MLflow 追踪即刻可用。

text=ZqhQzanResources