使用 MLflow 追踪服务器进行远程实验追踪
在本教程中,您将学习如何使用 MLflow Tracking Server 为团队开发设置 MLflow Tracking 环境。
使用 MLflow Tracking Server 进行远程实验跟踪有许多好处
- 协作:多个用户可以向同一端点记录运行,并查询其他用户记录的运行和模型。
- 共享结果:跟踪服务器还提供一个 Tracking UI 端点,团队成员可以在其中轻松探索彼此的结果。
- 集中访问:跟踪服务器可以作为元数据和工件的远程访问代理运行,从而更容易保护数据访问并审计访问记录。
它是如何工作的?
下图描绘了将远程 MLflow Tracking Server 与 PostgreSQL 和 S3 结合使用的架构
当您开始向 MLflow Tracking Server 记录运行时,会发生以下情况
-
第一部分 a 和 b:
- MLflow 客户端创建一个 RestStore 实例,并发送 REST API 请求来记录 MLflow 实体
- 跟踪服务器创建一个 SQLAlchemyStore 实例,并连接到远程主机,将跟踪信息(例如指标、参数、标签等)插入数据库。
-
第一部分 c 和 d:
- 客户端的检索请求会从配置的 SQLAlchemyStore 表中返回信息
-
第二部分 a 和 b:
- 客户端通过
HttpArtifactRepository发送工件的记录事件,将文件写入 MLflow Tracking Server - 然后,跟踪服务器会将这些文件写入配置的对象存储位置,并进行假设角色认证。
- 客户端通过
-
第二部分 c 和 d:
- 通过服务器启动时配置的相同授权认证,可以为用户请求从配置的后端存储中检索工件。
- 工件通过
HttpArtifactRepository接口通过跟踪服务器传递给最终用户
开始使用
前言
在实际的生产部署环境中,您将拥有多个远程主机来运行跟踪服务器和数据库,如上图所示。然而,在本教程中,我们将只使用一台机器运行不同端口上的多个 Docker 容器,通过更简单的评估教程设置来模拟远程环境。我们还将使用 MinIO(一种 S3 兼容的对象存储)作为工件存储,这样您就不需要 AWS 账户来运行本教程。
第 1 步 - 获取 MLflow 和附加依赖项
MLflow 在 PyPI 上可用。此外,使用 Python 访问 PostgreSQL 和 S3 需要 pyscopg2 和 boto3。如果您尚未在系统中安装它们,可以使用以下命令进行安装:
pip install mlflow psycopg2 boto3
第 2 步 - 设置远程数据存储
MLflow Tracking Server 可以与各种数据存储交互,以存储实验和运行数据以及工件。在本教程中,我们将使用 Docker Compose 启动两个容器,每个容器模拟实际环境中的远程服务器。
- 作为后端存储的 PostgreSQL 数据库。
- 作为工件存储的 MinIO 服务器。
安装 Docker 和 Docker Compose
这些 Docker 步骤仅用于教程目的。MLflow 本身不依赖于 Docker。
按照官方说明安装 Docker 和 Docker Compose。然后,运行 docker --version 和 docker-compose --version 以确保它们已正确安装。
创建 compose.yaml
创建一个名为 compose.yaml 的文件,内容如下:
version: "3.7"
services:
# PostgreSQL database
postgres:
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mlflowdb
ports:
- 5432:5432
volumes:
- ./postgres-data:/var/lib/postgresql/data
# MinIO server
minio:
image: minio/minio
expose:
- "9000"
ports:
- "9000:9000"
# MinIO Console is available at https://:9001
- "9001:9001"
environment:
MINIO_ROOT_USER: "minio_user"
MINIO_ROOT_PASSWORD: "minio_password"
healthcheck:
test: timeout 5s bash -c ':> /dev/tcp/127.0.0.1/9000' || exit 1
interval: 1s
timeout: 10s
retries: 5
command: server /data --console-address ":9001"
# Create a bucket named "bucket" if it doesn't exist
minio-create-bucket:
image: minio/mc
depends_on:
minio:
condition: service_healthy
entrypoint: >
bash -c "
mc alias set minio http://minio:9000 minio_user minio_password &&
if ! mc ls minio/bucket; then
mc mb minio/bucket
else
echo 'bucket already exists'
fi
"
启动容器
从 compose.yaml 文件所在的同一目录运行以下命令以启动容器。这将使 PostgreSQL 和 Minio 服务器的容器在后台运行,并创建一个名为 "bucket" 的新存储桶。
docker compose up -d
第 3 步 - 启动跟踪服务器
在实际环境中,您将有一个远程主机来运行跟踪服务器,但在本教程中,我们将仅使用本地机器作为远程机器的模拟替代品。
配置访问
为了让跟踪服务器访问远程存储,需要配置必要的凭据。
export MLFLOW_S3_ENDPOINT_URL=https://:9000 # Replace this with remote storage endpoint e.g. s3://my-bucket in real use cases
export AWS_ACCESS_KEY_ID=minio_user
export AWS_SECRET_ACCESS_KEY=minio_password
您可以在 支持的存储 中找到有关如何为其他存储配置凭据的说明。
启动跟踪服务器
分别使用 --backend-store-uri 和 --artifacts-store-uri 选项来指定后端存储和工件存储。
mlflow server \
--backend-store-uri postgresql://user:password@localhost:5432/mlflowdb \
--artifacts-destination s3://bucket \
--host 0.0.0.0 \
--port 5000
在实际环境中,请将 localhost 替换为数据库服务器的远程主机名或 IP 地址。
第 4 步:记录到跟踪服务器
一旦跟踪服务器正在运行,您可以通过将 MLflow Tracking URI 设置为跟踪服务器的 URI 来向其记录运行。或者,您可以使用 mlflow.set_tracking_uri() API 来设置跟踪 URI。
export MLFLOW_TRACKING_URI=http://127.0.0.1:5000 # Replace with remote host name or IP address in an actual environment
然后像往常一样使用 MLflow 跟踪 API 运行您的代码。以下代码在 diabetes 数据集上运行 scikit-learn RandomForest 模型的训练。
import mlflow
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_diabetes
from sklearn.ensemble import RandomForestRegressor
mlflow.autolog()
db = load_diabetes()
X_train, X_test, y_train, y_test = train_test_split(db.data, db.target)
# Create and train models.
rf = RandomForestRegressor(n_estimators=100, max_depth=6, max_features=3)
rf.fit(X_train, y_train)
# Use the model to make predictions on the test dataset.
predictions = rf.predict(X_test)
第 5 步:在跟踪 UI 中查看记录的运行
我们的伪远程 MLflow Tracking Server 也在同一端点上托管 Tracking UI。在具有远程跟踪服务器的实际部署环境中也是如此。您可以通过在浏览器中导航到 http://127.0.0.1:5000(在实际环境中替换为远程主机名或 IP 地址)来访问 UI。
第 6 步:下载工件
MLflow Tracking Server 还充当工件访问的代理主机。通过 models:/、mlflow-artifacts:/ 等代理 URI 可以访问工件,使开发人员无需管理直接访问的凭据或权限即可访问此位置。
import mlflow
model_id = "YOUR_MODEL_ID" # You can find model ID in the Tracking UI
# Download artifact via the tracking server
mlflow_artifact_uri = f"models:/{model_id}"
local_path = mlflow.artifacts.download_artifacts(mlflow_artifact_uri)
# Load the model
model = mlflow.sklearn.load_model(local_path)
下一步是什么?
现在您已经学会了如何设置 MLflow Tracking Server 进行远程实验跟踪!您还可以探索一些更高级的主题