首页 > 解决方案 > MlFlow 如何手动读取指标

问题描述

我尝试以这种方式读取指标:

 data, info = mlflow.get_run(run_id)
 print(data[1].metrics)
 # example of output: {'loss': 0.01}

但它只得到最后一个值。是否可以手动读取特定指标的所有步骤?

标签: pythonmetricsmlflow

解决方案


我遇到了同样的问题,并且能够使用 using 获取指标的所有值mlflow.tracking.MlflowClient().get_metric_history。这将返回您使用记录的每个值mlflow.log_metric(key, value)

快速示例(未经测试)

import mlflow
trackingDir = 'file:///....'
registryDir = 'file:///...'
runID = 'my run id'
metricKey = 'loss'

client = mlflow.tracking.MlflowClient(
            tracking_uri=trackingDir,
            registry_uri=registryDir,
        )

metrics = client.get_metric_history(runID, metricKey)

从文档

get_metric_history(run_id, key)[source] 返回与给定指标记录的所有值相对应的指标对象列表。

参数 run_id – 运行的唯一标识符

key – 运行中的指标名称

如果已记录,则返回 mlflow.entities.Metric 实体的列表,否则为空列表

from mlflow.tracking import MlflowClient

def print_metric_info(history):
    for m in history:
        print("name: {}".format(m.key))
        print("value: {}".format(m.value))
        print("step: {}".format(m.step))
        print("timestamp: {}".format(m.timestamp))
        print("--")

# Create a run under the default experiment (whose id is "0"). Since this is low-level
# CRUD operation, the method will create a run. To end the run, you'll have
# to explicitly end it. 
client = MlflowClient() 
experiment_id = "0" 
run = client.create_run(experiment_id) 
print("run_id:{}".format(run.info.run_id))
print("--")

# Log couple of metrics, update their initial value, and fetch each
# logged metrics' history. 
for k, v in [("m1", 1.5), ("m2", 2.5)]:
    client.log_metric(run.info.run_id, k, v, step=0)
    client.log_metric(run.info.run_id, k, v + 1, step=1)
    print_metric_info(client.get_metric_history(run.info.run_id, k))
client.set_terminated(run.info.run_id) 

推荐阅读