在 中mlflow,您可以使用可在 UI 中折叠的 fluent 项目 API 运行嵌套运行。例如,通过使用以下代码(有关 UI 支持,请参阅此内容):
with mlflow.start_run(nested=True):
mlflow.log_param("mse", 0.10)
mlflow.log_param("lr", 0.05)
mlflow.log_param("batch_size", 512)
with mlflow.start_run(nested=True):
mlflow.log_param("max_runs", 32)
mlflow.log_param("epochs", 20)
mlflow.log_metric("acc", 98)
mlflow.log_metric("rmse", 98)
mlflow.end_run()
Run Code Online (Sandbox Code Playgroud)
由于数据库连接问题,我想在我的应用程序中使用单个 mlflow 客户端。
我如何堆叠运行,例如用于超参数优化,使用通过创建的运行MlflowClient().create_run()?
实现起来有点复杂,但我通过研究直接使用导入时使用的Fluent Tracking Interfacemlflow找到了一种方法。
在start_run函数中你可以看到 anested_run只是通过设置特定的 tag 来定义的mlflow.utils.mlflow_tags.MLFLOW_PARENT_RUN_ID。只需将其设置run.info.run_id为父运行的值,它将在 UI 中正确显示。
这是一个例子:
from mlflow.tracking import MlflowClient
from mlflow.utils.mlflow_tags import MLFLOW_PARENT_RUN_ID
client = MlflowClient()
try:
experiment = client.create_experiment("test_nested")
except:
experiment = client.get_experiment_by_name("test_nested").experiment_id
parent_run = client.create_run(experiment_id=experiment)
client.log_param(parent_run.info.run_id, "who", "parent")
child_run_1 = client.create_run(
experiment_id=experiment,
tags={
MLFLOW_PARENT_RUN_ID: parent_run.info.run_id
}
)
client.log_param(child_run_1.info.run_id, "who", "child 1")
child_run_2 = client.create_run(
experiment_id=experiment,
tags={
MLFLOW_PARENT_RUN_ID: parent_run.info.run_id
}
)
client.log_param(child_run_2.info.run_id, "who", "child 2")
Run Code Online (Sandbox Code Playgroud)
如果您想知道:也可以使用mlflow.utils.mlflow_tags.MLFLOW_RUN_NAME标签以这种方式指定运行名称。