Python MlFLow如何手动读取度量值

Python MlFLow如何手动读取度量值,python,metrics,mlflow,Python,Metrics,Mlflow,我尝试以这种方式阅读度量: data, info = mlflow.get_run(run_id) print(data[1].metrics) # example of output: {'loss': 0.01} 但它只得到最后一个值。可以手动读取特定度量的所有步骤吗?我残酷地解决了它:我读取带有特定[run\u id]的特定[metric\u name]的原始文件 path = f'./mlruns/0/[run_id]/metrics/[metric_name]' with op

我尝试以这种方式阅读度量:

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

但它只得到最后一个值。可以手动读取特定度量的所有步骤吗?

我残酷地解决了它:我读取带有特定[run\u id]的特定[metric\u name]的原始文件

path = f'./mlruns/0/[run_id]/metrics/[metric_name]'
with open(path) as f:
    content = f.readlines()
metrics_for_step = [float(x.split(' ')[1]) for x in content]

我残酷地解决了这个问题:我读取了带有specific[run\u id]的specific[metric\u name]的原始文件

path = f'./mlruns/0/[run_id]/metrics/[metric_name]'
with open(path) as f:
    content = f.readlines()
metrics_for_step = [float(x.split(' ')[1]) for x in content]

是的,您可以使用
MlffowClient API
获取实验和运行信息。它以字典的形式返回MLflow数据结构,您对其进行迭代以提取listcomp中需要的内容。下面是一个例子:


def print_experiment_details(experiment_id, run_id):
    """
    Method to print experiment run info and a specific run details
    :param experiment_id: MLflow experiment ID
    :param run_id: MLflow run ID within an experiment
    :return: none
    """
    print("Finished MLflow Run with run_id {} and experiment_id {}".format(run_id, experiment_id))

    # Use MlflowClient API to list experiments and run info
    client = MlflowClient()
    print("=" * 80)
    # Get a list of all experiments
    print("List of all Experiments")
    print("=" * 80)
    [print(pprint.pprint(dict(exp), indent=4))
     for exp in client.list_experiments()]
    print("=" * 80)
    print(f"List Run info for run_id={run_id}")
    print(pprint.pprint(dict(mlflow.get_run(run_id))))
这将产生:

Running local model registry=sqlite:///mlruns.db
Finished MLflow Run with run_id 3f3b827dd6814649a2f84ebae09b26c6 and experiment_id 0
================================================================================
List of all Experiments
================================================================================
{   'artifact_location': './mlruns/0',
    'experiment_id': '0',
    'lifecycle_stage': 'active',
    'name': 'ODSC_TUTORIALS',
    'tags': {   'mlflow.note.content': 'This is experiment for getting started '
                                       'with MLflow ...'}}
None
================================================================================
List Run info for run_id=3f3b827dd6814649a2f84ebae09b26c6
{'data': <RunData: metrics={'metric_1': 0.9236238251076615,
 'metric_2': 1.6732389715754346,
 'metric_3': 2.249979396736294}, params={'n_estimators': '3', 'random_state': '42'}, tags={'mlflow.log-model.history': '[{"run_id": "3f3b827dd6814649a2f84ebae09b26c6", '
                             '"artifact_path": "sklearn-model", '
                             '"utc_time_created": "2020-03-18 '
                             '22:25:33.083332", "flavors": {"python_function": '
                             '{"loader_module": "mlflow.sklearn", '
                             '"python_version": "3.7.5", "data": "model.pkl", '
                             '"env": "conda.yaml"}, "sklearn": '
                             '{"pickled_model": "model.pkl", '
                             '"sklearn_version": "0.22.2.post1", '
                             '"serialization_format": "cloudpickle"}}}]',
 'mlflow.note.content': 'This Run is for getting started with MLflow ...',
 'mlflow.runName': 'LOCAL_REGISTRY',
 'mlflow.source.git.commit': '0a3c6a3739deab77631318eca7fb9690b6dbad66',
 'mlflow.source.name': '/Users/julesdamji/gits/tutorials/mlflow/labs/00_get_started.py',
 'mlflow.source.type': 'LOCAL',
 'mlflow.user': 'julesdamji'}>,
 'info': <RunInfo: artifact_uri='./mlruns/0/3f3b827dd6814649a2f84ebae09b26c6/artifacts', end_time=1584570333841, experiment_id='0', lifecycle_stage='active', run_id='3f3b827dd6814649a2f84ebae09b26c6', run_uuid='3f3b827dd6814649a2f84ebae09b26c6', start_time=1584570332914, status='FINISHED', user_id='julesdamji'>}

运行本地模型注册表=sqlite:///mlruns.db
使用运行id 3f3b827dd6814649a2f84ebae09b26c6和实验id 0完成MLflow运行
================================================================================
所有实验的清单
================================================================================
{'artifact_location':'./mlruns/0',
“实验id:“0”,
“生命周期阶段”:“活动”,
“名称”:“ODSC_教程”,
'tags':{'mlflow.note.content':'这是入门实验'
'使用MLflow…'}}
没有一个
================================================================================
列出运行id=3f3b827dd6814649a2f84ebae09b26c6的运行信息
{'data':,
'信息':}
你可以得到完整的代码


希望有帮助

是的,您可以使用
MlffowClient API
获取实验和运行信息。它以字典的形式返回MLflow数据结构,您对其进行迭代以提取listcomp中需要的内容。下面是一个例子:


def print_experiment_details(experiment_id, run_id):
    """
    Method to print experiment run info and a specific run details
    :param experiment_id: MLflow experiment ID
    :param run_id: MLflow run ID within an experiment
    :return: none
    """
    print("Finished MLflow Run with run_id {} and experiment_id {}".format(run_id, experiment_id))

    # Use MlflowClient API to list experiments and run info
    client = MlflowClient()
    print("=" * 80)
    # Get a list of all experiments
    print("List of all Experiments")
    print("=" * 80)
    [print(pprint.pprint(dict(exp), indent=4))
     for exp in client.list_experiments()]
    print("=" * 80)
    print(f"List Run info for run_id={run_id}")
    print(pprint.pprint(dict(mlflow.get_run(run_id))))
这将产生:

Running local model registry=sqlite:///mlruns.db
Finished MLflow Run with run_id 3f3b827dd6814649a2f84ebae09b26c6 and experiment_id 0
================================================================================
List of all Experiments
================================================================================
{   'artifact_location': './mlruns/0',
    'experiment_id': '0',
    'lifecycle_stage': 'active',
    'name': 'ODSC_TUTORIALS',
    'tags': {   'mlflow.note.content': 'This is experiment for getting started '
                                       'with MLflow ...'}}
None
================================================================================
List Run info for run_id=3f3b827dd6814649a2f84ebae09b26c6
{'data': <RunData: metrics={'metric_1': 0.9236238251076615,
 'metric_2': 1.6732389715754346,
 'metric_3': 2.249979396736294}, params={'n_estimators': '3', 'random_state': '42'}, tags={'mlflow.log-model.history': '[{"run_id": "3f3b827dd6814649a2f84ebae09b26c6", '
                             '"artifact_path": "sklearn-model", '
                             '"utc_time_created": "2020-03-18 '
                             '22:25:33.083332", "flavors": {"python_function": '
                             '{"loader_module": "mlflow.sklearn", '
                             '"python_version": "3.7.5", "data": "model.pkl", '
                             '"env": "conda.yaml"}, "sklearn": '
                             '{"pickled_model": "model.pkl", '
                             '"sklearn_version": "0.22.2.post1", '
                             '"serialization_format": "cloudpickle"}}}]',
 'mlflow.note.content': 'This Run is for getting started with MLflow ...',
 'mlflow.runName': 'LOCAL_REGISTRY',
 'mlflow.source.git.commit': '0a3c6a3739deab77631318eca7fb9690b6dbad66',
 'mlflow.source.name': '/Users/julesdamji/gits/tutorials/mlflow/labs/00_get_started.py',
 'mlflow.source.type': 'LOCAL',
 'mlflow.user': 'julesdamji'}>,
 'info': <RunInfo: artifact_uri='./mlruns/0/3f3b827dd6814649a2f84ebae09b26c6/artifacts', end_time=1584570333841, experiment_id='0', lifecycle_stage='active', run_id='3f3b827dd6814649a2f84ebae09b26c6', run_uuid='3f3b827dd6814649a2f84ebae09b26c6', start_time=1584570332914, status='FINISHED', user_id='julesdamji'>}

运行本地模型注册表=sqlite:///mlruns.db
使用运行id 3f3b827dd6814649a2f84ebae09b26c6和实验id 0完成MLflow运行
================================================================================
所有实验的清单
================================================================================
{'artifact_location':'./mlruns/0',
“实验id:“0”,
“生命周期阶段”:“活动”,
“名称”:“ODSC_教程”,
'tags':{'mlflow.note.content':'这是入门实验'
'使用MLflow…'}}
没有一个
================================================================================
列出运行id=3f3b827dd6814649a2f84ebae09b26c6的运行信息
{'data':,
'信息':}
你可以得到完整的代码


希望有帮助

我遇到了同样的问题,并且能够通过使用获取度量的所有值。这将返回您使用
mlflow.log\u metric(键,值)
记录的每个值

快速示例(未经测试)

导入mlflow
跟踪目录file:///....'
注册目录file:///...'
runID='我的跑步id'
metricKey='loss'
client=mlflow.tracking.MlflowClient(
tracking_uri=trackingDir,
registry\u uri=registryDir,
)
metrics=client.get_metric_history(runID,metricKey)

获取度量值历史记录(run\u id,key)[source]返回度量值列表 与为给定度量记录的所有值相对应的对象

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

key–运行中的度量名称

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

从mlflow.tracking导入MlflowClient
def打印度量信息(历史记录):
对于历史上的m:
打印(“名称:{}”。格式(m.key))
打印(“值:{}”。格式(m.value))
打印(“步骤:{}”。格式(m.step))
打印(“时间戳:{}”。格式(m.timestamp))
打印(“--”)
#在默认实验(其id为“0”)下创建运行。因为这是低级的
#CRUD操作时,该方法将创建一个运行。要结束跑步,你必须
#明确地结束它。
client=MlflowClient()
实验\u id=“0”
运行=客户端。创建运行(实验id)
打印(“run\u id:{}”。格式(run.info.run\u id))
打印(“--”)
#记录两个度量,更新它们的初始值,并获取每个度量
#记录度量的历史记录。
对于k,v in[(“m1”,1.5),(“m2”,2.5)]:
client.log_度量(run.info.run_id,k,v,step=0)
client.log_度量(run.info.run_id,k,v+1,step=1)
打印度量信息(client.get度量历史记录(run.info.run\u id,k))
client.set\u终止(run.info.run\u id)

我遇到了同样的问题,并且能够通过使用获取度量的所有值。这将返回您使用
mlflow.log\u metric(键,值)
记录的每个值

快速示例(未经测试)

导入mlflow
跟踪目录file:///....'
注册目录file:///...'
runID='我的跑步id'
metricKey='loss'
client=mlflow.tracking.MlflowClient(
tracking_uri=trackingDir,
registry\u uri=registryDir,
)
metrics=client.get_metric_history(runID,metricKey)

获取度量值历史记录(run\u id,key)[source]返回度量值列表 与为给定度量记录的所有值相对应的对象

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

key–运行中的度量名称

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

从mlflow.tracking导入MlflowClient
def打印度量信息(历史记录):
对于历史上的m:
打印(“名称:{}”。格式(m.key))
打印(“值:{}”。格式(m.value))
打印(“步骤:{}”。格式(m.step))
打印(“时间戳:{}”。格式(m.timestamp))
打印(“--”)
#在默认实验(其id为“0”)下创建运行。因为这是低级的
#CRUD操作时,该方法将创建一个运行。要结束跑步,你必须
#明确地结束它。
client=MlflowClient()
实验\u id=“0”
运行=客户端。创建运行(实验id)
打印(“run\u id:{}”。格式(run.info.run\u id))
打印(“--”)
#记录两个度量,更新它们的初始值,并获取每个度量
#记录度量的历史记录。
对于k,v in[(“m1”,1.5),(“m2”,2.5)]:
client.log\u度量