连接您的开发环境到 MLflow
本指南将向您展示如何将开发环境连接到 MLflow 实验。您可以在本地计算机上运行 MLflow,自托管开源 MLflow 服务,或使用托管服务,例如 Databricks Managed MLflow。
先决条件
- OSS MLflow
- Databricks
- Python 环境: Python 3.9+ 并已安装 pip
- Databricks 工作区: 可访问 Databricks 工作区
本指南介绍了如何使用 Databricks 个人访问令牌。MLflow 也支持其他 Databricks 支持的身份验证方法。
设置说明
- OSS MLflow
- Databricks - 本地 IDE
- Databricks - 笔记本
步骤 1: 安装 MLflow
pip install --upgrade "mlflow>=3.1"
步骤 2: 配置跟踪
MLflow 支持不同的后端来跟踪您的实验数据。选择以下选项之一开始。有关详细的设置和配置,请参阅 自托管指南。
选项 A: 数据库 (推荐)
将跟踪 URI 设置为本地数据库 URI (例如 sqlite:///mlflow.db)。这是快速入门和本地开发推荐的选项。
import mlflow
mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("my-first-experiment")
选项 B: 文件系统
如果未指定跟踪 URI,MLflow 将自动使用本地文件存储
import mlflow
# Creates local mlruns directory for experiments
mlflow.set_experiment("my-first-experiment")
文件系统后端处于“维护模式” (Keep-the-Light-On, KTLO),不会接收 MLflow 中的大多数新功能。我们建议改用数据库后端。数据库后端很快也将成为默认选项。
选项 C: 远程跟踪服务器
按照 自托管指南 启动远程 MLflow 跟踪服务器。然后配置您的客户端以使用远程服务器
import mlflow
# Connect to remote MLflow server
mlflow.set_tracking_uri("https://:5000")
mlflow.set_experiment("my-first-experiment")
或者,您可以通过环境变量配置跟踪 URI 和实验
export MLFLOW_TRACKING_URI="https://:5000"
export MLFLOW_EXPERIMENT_NAME="my-first-experiment"
步骤 3: 验证您的连接
创建一个测试文件并运行此代码
import mlflow
# Print connection information
print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")
print(f"Active Experiment: {mlflow.get_experiment_by_name('my-first-experiment')}")
# Test logging
with mlflow.start_run():
mlflow.log_param("test_param", "test_value")
print("✓ Successfully connected to MLflow!")
步骤 4: 访问 MLflow UI
如果您正在使用本地跟踪 (选项 A 或 B),请运行以下命令,并通过 https://:5000 访问 MLflow UI。
# For Option A
mlflow server --backend-store-uri sqlite:///mlflow.db --port 5000
# For Option B
mlflow server --port 5000
如果您正在运行远程跟踪服务器 (选项 C),请在相同的 URI 访问 MLflow UI。
当使用远程跟踪服务器时,您可能会在从浏览器访问 MLflow UI 时遇到“访问被拒绝”的错误。
无效的主机头 - 检测到可能的 DNS 重新绑定攻击
导致此错误的常见原因是跟踪服务器不允许来自您源的请求,以防止点击劫持攻击。在这种情况下,您需要在跟踪服务器配置中设置 CORS 源的允许列表。有关更多详细信息,请参阅 网络安全指南。
步骤 1: 安装 MLflow
安装具有 Databricks 连接功能的 MLflow
pip install --upgrade "mlflow[databricks]>=3.1"
步骤 2: 创建 MLflow 实验
- 打开您的 Databricks 工作区
- 在左侧边栏的 **Machine Learning** 下,转到 **Experiments**
- 在 Experiments 页面顶部,点击 **New Experiment**
步骤 3: 配置身份验证
选择以下身份验证方法之一
选项 A: 环境变量
- 在您的 MLflow Experiment 中,点击 **Generate API Key**
- 复制并运行生成的代码到您的终端
export DATABRICKS_TOKEN=<databricks-personal-access-token>
export DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com
export MLFLOW_TRACKING_URI=databricks
export MLFLOW_EXPERIMENT_ID=<experiment-id>
选项 B: .env 文件
- 在您的 MLflow Experiment 中,点击 **Generate API Key**
- 将生成的代码复制到项目根目录下的
.env文件中
DATABRICKS_TOKEN=<databricks-personal-access-token>
DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com
MLFLOW_TRACKING_URI=databricks
MLFLOW_EXPERIMENT_ID=<experiment-id>
- 安装
python-dotenv包
pip install python-dotenv
- 在您的代码中加载环境变量
# At the beginning of your Python script
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
步骤 4: 验证您的连接
创建一个测试文件并运行此代码以验证您的连接
import mlflow
# Test logging to verify connection
print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")
with mlflow.start_run():
print("✓ Successfully connected to MLflow!")
步骤 1: 安装 MLflow
Databricks 运行时包括 MLflow,但为了获得最佳体验,请更新到最新版本
%pip install --upgrade "mlflow[databricks]>=3.1"
dbutils.library.restartPython()
步骤 2: 创建笔记本
创建 Databricks 笔记本将创建一个 MLflow 实验,它是您 ML 项目的容器。在 MLflow 文档 中了解有关 Experiments 的更多信息。
- 打开您的 Databricks 工作区
- 在左侧边栏顶部转到 **New**
- 点击 **Notebook**
步骤 3: 配置身份验证
在 Databricks 笔记本中工作时,无需额外的身份验证配置。该笔记本自动拥有对您的工作区和相关 MLflow 实验的访问权限。
步骤 4: 验证您的连接
在笔记本单元格中运行此代码以验证您的连接
import mlflow
# Test logging to verify connection
print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")
with mlflow.start_run():
print("✓ Successfully connected to MLflow!")