如何向已完成的 MLflow 运行添加更多指标?

mir*_*phd 6 python databricks 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'
Run Code Online (Sandbox Code Playgroud)

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

例子:

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 Code Online (Sandbox Code Playgroud)

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

我想知道这种只读行为是否是设计使然(假设不可变的建模运行可以提高实验的再现性)?我可以理解这一点,但如果一切都必须在像上下文这样的单一整体中完成,那么它也违背了代码模块化with mlflow.start_run()......

mir*_*phd 5

正如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")
Run Code Online (Sandbox Code Playgroud)