首页 > 解决方案 > 如何为完成的 MLflow 运行添加更多指标?

问题描述

MLflow 运行完成后,外部脚本可以使用 pythonmlflow客户端和mlflow.get_run(run_id)方法访问其参数和指标,但Run返回的对象get_run似乎是只读的。

具体来说,.log_param .log_metric.log_artifact不能用于返回的对象get_run,引发如下错误:

AttributeError: 'Run' object has no attribute 'log_param'

如果我们尝试在 上运行任何.log_*方法mlflow,它会将它们记录到具有Default实验中自动生成的运行 ID 的新运行中。

例子:

final_model_mlflow_run = mlflow.get_run(final_model_mlflow_run_id)

with mlflow.ActiveRun(run=final_model_mlflow_run) as myrun:    
    
    # this read operation uses correct run
    run_id = myrun.info.run_id
    print(run_id)
    
    # this write operation writes to a new run 
    # (with auto-generated random run ID) 
    # in the "Default" experiment (with exp. ID of 0)
    mlflow.log_param("test3", "This is a test")
   

请注意,无论状态如何,上述问题都存在Run.info.status可以是“FINISHED”或“RUNNING”,没有任何区别)。

我想知道这种只读行为是否是设计使然(鉴于不可变建模运行提高了实验的可重复性)?我可以理解这一点,但如果所有事情都必须在一个单一的整体中完成,比如with mlflow.start_run()上下文......

标签: pythondatabricksmlflow

解决方案


正如Hans Bambel向我指出的那样,正如这里 mlflow.start_run记录的那样(与 相比mlflow.ActiveRun)接受run_id现有运行的参数。

这是一个经过测试可在 v1.13 到 v1.19 中工作的示例 - 正如您所看到的,甚至可以覆盖现有指标来纠正错误:

with mlflow.start_run(run_id=final_model_mlflow_run_id):
    
    # print(mlflow.active_run().info)
    
    mlflow.log_param("start_run_test", "This is a test")
    mlflow.log_metric("start_run_test", 1.23)
    mlflow.log_metric("start_run_test", 1.33)
    mlflow.log_artifact("/home/jovyan/_tmp/formula-features-20201103.json", "start_run_test")

推荐阅读