首页 > 解决方案 > 在容器中使用时,MLFlow 无法将我的工件正确复制到 mlruns 文件夹

问题描述

我正在使用 MLflow 记录一些模型。相应的脚本在 docker 容器中执行。

该命令mlflow.log_artifacts似乎无法正常工作,因为我在 mlruns 文件夹下看不到相应的文件。该命令虽然不返回任何错误。

EDIT1:经过进一步调查,似乎每当我挂载包含mlruns/作为 docker 卷的文件夹时就会出现问题。我用文档字符串提供的示例做了一些测试log_artifacts

import os
import json
import mlflow
# Create some files to preserve as artifacts
features = "rooms, zipcode, median_price, school_rating, transport"
data = {"state": "TX", "Available": 25, "Type": "Detached"}
# Create couple of artifact files under the directory "data"
os.makedirs("data", exist_ok=True)
with open("data/data.json", 'w', encoding='utf-8') as f:
    json.dump(data, f, indent=2)
with open("data/features.txt", 'w') as f:
    f.write(features)
# Write all files in "data" to root artifact_uri/states
with mlflow.start_run():
    mlflow.log_artifacts("data", artifact_path="states")

如果我在没有卷安装的容器中运行它,它运行得很好,即工件出现在mlruns/<exp-id>/<run-id>/artifacts/state

但是,如果我在一个包含已安装文件夹的容器中运行mlruns/它,它就不起作用,即该文件夹mlruns/<exp-id>/<run-id>/artifacts/是空的。

标签: pythondockermlflow

解决方案


我花了一段时间,但我终于发现我在这里做错了什么。

当您尝试将在容器中执行的运行添加到本地创建的现有实验时,就会出现问题。

的确,当你创建一个新的体验时,MLFlow 会生成一个meta.yaml文件看起来像这样(假设你首先在本地创建了体验)

artifact_location: file:///Users/greghor/Documents/my-cool-project/mlruns/0
experiment_id: '0'
lifecycle_stage: active
name: Default

现在假设您想在同一个实验下添加几个运行。但是由于某些原因,您不想在本地执行它们,而是在/opt/app容器的目录中执行。您已将项目目录安装为卷,因为您希望在容器关闭后保留运行结果。MLFlow 将读取meta.yml文件而不是创建新文件,并将尝试将工件复制到指向本地文件系统的路径......这将不起作用。

令人惊讶的是,MLFlow 保持沉默,而我预计会出现错误,指出未找到工件目录或类似的东西。

我认为现阶段没有简单的解决方法。如果我没记错的话,MLFlow 不会公开更改现有实验的 artifact_uri 的方法。最好的办法是确保启动运行的环境的文件系统与meta.yml文件一致。


推荐阅读