正如我在上面评论的,是的,mlflow.create_experiment() 确实允许您使用 artifact_location 参数设置工件位置。
然而,有点相关的是,使用create_experiment() 函数设置artifact_location 的问题是,一旦你创建了一个实验,如果你再次运行create_experiment() 函数,MLflow 会抛出一个错误。
我没有在文档中看到这一点,但已确认如果后端存储中已经存在实验,MlFlow 将不允许您再次运行相同的 create_experiment() 函数。在这篇文章中,MLfLow 没有check_if_exists 标志或create_experiments_if_not_exists() 函数。
为了让事情更令人沮丧,你也不能在set_experiment()函数中设置artifcact_location。
所以这是一个非常简单的解决方法,它还避免了“错误 mlflow.utils.rest_utils ...”标准输出日志记录。
:
import os
from random import random, randint
from mlflow import mlflow,log_metric, log_param, log_artifacts
from mlflow.exceptions import MlflowException
try:
experiment = mlflow.get_experiment_by_name('oof')
experiment_id = experiment.experiment_id
except AttributeError:
experiment_id = mlflow.create_experiment('oof', artifact_location='s3://mlflow-minio/sample/')
with mlflow.start_run(experiment_id=experiment_id) as run:
mlflow.set_tracking_uri('http://localhost:5000')
print("Running mlflow_tracking.py")
log_param("param1", randint(0, 100))
log_metric("foo", random())
log_metric("foo", random() + 1)
log_metric("foo", random() + 2)
if not os.path.exists("outputs"):
os.makedirs("outputs")
with open("outputs/test.txt", "w") as f:
f.write("hello world!")
log_artifacts("outputs")
如果是用户第一次创建实验,代码将遇到 AttributeError,因为experiment_id 不存在,except 代码块在创建实验时被执行。
如果是第二次、第三次等代码运行,由于实验已经存在,所以只会执行try语句下的代码。 Mlflow 现在将在您的 s3 存储桶中创建一个“样本”密钥。没有经过全面测试,但至少对我有用。