MLOps极致细节:8. MLFlow REST API 实例(附代码)

本文提供了一个使用MLFlow REST API的代码示例,包括创建实验、运行、设置参数、标签和指标等操作。代码可在Visual Studio Code中运行,需要预先安装Anaconda3。首先启动MLFlow服务器,然后在另一终端运行example.py,展示如何通过REST API与MLFlow交互。代码逻辑包括检查实验是否存在、创建实验和运行,以及记录参数、标签和指标。
摘要由CSDN通过智能技术生成

MLOps极致细节:8. MLFlow REST API 实例(附代码)

在这个博客中,我们将介绍MLFlow REST API的代码,以及一些函数的使用,包括新建mlflow run,新建experiment,从experiment id/name中获得相关experiment信息,设置Tag/param/metric信息,设置log batch信息等等。MLFlow REST API的详细介绍参见博客MLOps极致细节:7. MLFlow REST API 功能介绍及应用

此博客的代码可以在这里直接下载,基于MLFlow官网GitHub链接改写。关于MLFlow REST API的官方介绍参见此链接

  • 平台:Win10。
  • IDE:Visual Studio Code
  • 需要预装:Anaconda3。


1 背景介绍

一般情况下,我们可以直接使用MLflow的库来调用其中的功能模块,但也有一些情况,我们不希望使用MLflow库,或者我们并不是用Python来作为开发语言,那么MLflow REST API也是一种不错的选择。MLflow REST API允许您创建、列出、获取experiment和run,并记录parameters,metrics以及artifacts。API托管在MLflow跟踪服务器上的/api路由下。

2 代码运行

首先将代码下载到本地:git clone https://gitee.com/yichaoyyds/mlflow-ex-restapi-basic.git

新建一个terminal,打开之后,最好先新建一个文件夹,然后输入

mlflow server

新建林我改一个terminal,打开之后,在mlflow-ex-restapi-basic的文件夹下输入:

python example.py

如果你是第一次尝试运行,你可能会看到terminal中的日志如下:

MLFlow RestAPI Example.
experiment_info:  None
Create new experiment.
Successfully create new experiment.
Successfully create run with run id: 850898a283554adcb60411ee7e126807
Successfully logged parameter.
Successfully set tag.
Successfully logged parameter
Successfully logged batch

如果不是第一次运行,terminal中的日志大致如下:

experiment_info:  {'experiment_id': '5', 'name': 'test2', 'artifact_location': './mlruns/5', 'lifecycle_stage': 'active'}
experiment has already been created.
Successfully create run with run id: a8bbe6717fbc473d8605eb6aae0afe1d
Successfully logged parameter.
Successfully set tag.
Successfully logged parameter
Successfully logged batch

3 代码解读

3.1 server的连接

首先我们需要启动一个server,mlflow背后用的是flask,所以通过mlflow server这个指令,系统就启动了一个本地的server,hostname=127.0.0.1,port=5000。这两个是默认值。如果希望有更多输入,可以参考:

mlflow server \
    --host 0.0.0.0 \
    --port 8889 \
    --serve-artifacts \
    --artifacts-destination s3://my-mlflow-bucket/ \
    --artifacts-only

当我们这个server运行起来后,在另一个terminal运行example.py才有效,否则就会出现如下error:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries 
exceeded with url: /api/2.0/mlflow/experiments/get-by-name?experiment_name=test3 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001E2AC630640>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))

3.2 代码逻辑

我们测试的代码存放于example.py中。相关的一些参数存于config.txt文件:

[main]
hostname=127.0.0.1
port=5000
experiment-name=test3

由于这个案例比较简单,所以也就这么几个参数。注意,这里的hostname和port需要和实际运行的server相匹配。

example.py代码中,我们首先会实例化MLflowTrackingRestApi这个类:mlflow_rest = MLflowTrackingRestApi(hostname, port, experiment_name)。这个类实际上就包含了所有MLFlow REST API的函数了。之前的博客:MLOps极致细节:7. MLFlow REST API 功能介绍及应用对其有详细的介绍。

__init__函数中,我们设置了base url,并且检查当前experiment name是否已经被创建过,如果没有的话,创建这个experiment。然后在这个experiment里创建一个新的run。

def __init__(self, hostname="127.0.0.1", port=5000, experiment_name=None):
    self.base_url = "http://" + hostname + ":" + str(port) + "/api/2.0/mlflow"
    experiment_name = str(experiment_name)
    experiment_info = self.get_experiment_by_name(experiment_name)
    print("experiment_info: ",experiment_info)
    if experiment_info == None:
        print("Create new experiment.")
        status_code = self.create_experiment(experiment_name = experiment_name)
        if status_code == 200: print("Successfully create new experiment.")
        else: print("experiment creation failed: {}".format(status_code))
    else:
        print("experiment has already been created.")
        self.experiment_id = experiment_info["experiment_id"]
    # Create a new run
    self.run_id = self.create_run()

实例化后,我们就把所有之前写的mlflow rest api函数都跑一遍,看一下效果:

# Log Parameter
#param = {"alpha": 0.1980}
param = {"key": "alpha", "value": 0.1980}
status_code = mlflow_rest.log_param(param)
if status_code == 200: print("Successfully logged parameter.")
else: print("Logging parameter failed: {}".format(status_code))

# Set Tag
tag = {"tag1": 1}
#tag = {"key": "tag1", "value": 1}
status_code = mlflow_rest.set_tag(tag)
if status_code == 200: print("Successfully set tag.")
else: print("Logging parameter failed: {}".format(status_code))

# Log Metric
metric = {"precision": 0.769}
# metric = {"key": "precision", "value": "0.769"}
status_code = mlflow_rest.log_metric(metric)
if status_code == 200: print("Successfully logged parameter")
else: print("Logging metric failed: {}".format(status_code))

# Log Batch
metrics = {"mse": 2500.00, "rmse": 50.00}
params = {"learning_rate": 0.01, "n_estimators": 10}
#metrics = [{"key": "mse", "value": "0.769"}, {"key": "callback", "value": "0.512"}]
#params = [{"key": "learning_rate", "value": "0.018"}, {"key": "beta", "value": "0.98"}, {"key": "gamma", "value": "512"}]
tags = []
status_code = mlflow_rest.log_batch(metrics, params, tags)
if status_code == 200:
    print("Successfully logged batch")
else:
    print("Logging batch failed: {}".format(status_code))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破浪会有时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值