最近需求需要使用mlflow,去学习了下,记录。
简介
MLflow是一个开源平台,专门为了帮助机器学习的从业者和团队处理机器学习过程中的复杂性而设计的。MLflow关注机器学习项目的完整生命周期,确保每个阶段都是可管理的、可追溯的和可复现的。
MLflow目前提供了几个关键的组件:
MLflow AI Gateway:通过安全、简单的API与最先进的LLM进行交互。
MLflow LLM Evaluate:简化LLM和提示的评估。
MLflow Tracking:记录和查询实验:代码、数据、配置和结果。
MLflow Projects:将数据科学代码打包成一种格式,可以在任何平台上重现运行。
MLflow Models:在不同的服务环境中部署机器学习模型。
Model Registry:在一个中心仓库中存储、注释、发现和管理模型。
FE启动
(前提你需要有python环境和pip)
先安装mlflow
pip install mlflow
然后就可以直接启动sever
mlflow server --host 127.0.0.1 --port 8080
这个端口随便你,只要是你可用的端口就行。
启动完成之后打开浏览器输入localhost:8080 你的端口是什么就是什么,我是8080而已
就可以看到他的UI。
代码使用
创建实验
这里的实验类似于我们的project,独立的实验可以方便进行管理和查看
from mlflow import MlflowClient
client = MlflowClient(tracking_uri="http://127.0.0.1:8080")
all_experiments = client.search_experiments()
default_experiment = [
{"name": experiment.name, "lifecycle_stage": experiment.lifecycle_stage}
for experiment in all_experiments
if experiment.name == "Default"
][0]
# Provide an Experiment description that will appear in the UI
experiment_description = (
"This is the grocery forecasting project. "
"This experiment contains the produce models for apples."
)
# Provide searchable tags that define characteristics of the Runs that
# will be in this Experiment
experiment_tags = {
"project_name": "grocery-forecasting",
"store_dept": "produce",
"team": "stores-ml",
"project_quarter": "Q3-2023",
"mlflow.note.content": experiment_description,
}
# Create the Experiment, providing a unique name
produce_apples_experiment = client.create_experiment(
name="Apple_Models", tags=experiment_tags
)
在FE里面可以看到 刚刚我们创建的实验。
查看实验
from mlflow import MlflowClient
from pprint import pprint
client = MlflowClient(tracking_uri="http://127.0.0.1:8080")
all_experiments = client.search_experiments()
pprint(all_experiments)
这样可以返回所有我们已经存在的实验,关于pprint()可以看我另一篇blog https://blog.csdn.net/Damien_J_Scott/article/details/134603880
搜索实验
只需要加上filter_string,属性名=‘value’就可以了,但需要注意的是,如果这个属性名是你自定义的,比如我的 mlflow.note.content 就需要 tag.'mlflow.note.content',先从tag点出来,然后用单引号引起来,如果需要模糊搜索就把 = 换成 like 并且用 % 当通配符进行搜索就行了。
from mlflow import MlflowClient
from pprint import pprint
client = MlflowClient(tracking_uri="http://127.0.0.1:8080")
apples_experiment1 = client.search_experiments(
filter_string="name = 'Apple_Models'"
)
pprint(vars(apples_experiment1[0]))
apples_experiment = client.search_experiments(
filter_string="tags.`mlflow.note.content` like 'This is the grocery%'"
)
pprint(vars(apples_experiment[0]))
Model准备
import mlflow
from mlflow.models import infer_signature
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Load the Iris dataset
X, y = datasets.load_iris(return_X_y=True)
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Define the model hyperparameters
params = {
"solver": "lbfgs",
"max_iter": 1000,
"multi_class": "auto",
"random_state": 8888,
}
# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
# Predict on the test set
y_pred = lr.predict(X_test)
# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
这里训练好了一个逻辑回归的模型。
Model记录
import mlflow
from mlflow.models import infer_signature
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Load the Iris dataset
X, y = datasets.load_iris(return_X_y=True)
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Define the model hyperparameters
params = {
"solver": "lbfgs",
"max_iter": 1000,
"multi_class": "auto",
"random_state": 8888,
}
# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
# Predict on the test set
y_pred = lr.predict(X_test)
# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
#2nd part code
# Set our tracking server uri for logging
mlflow.set_tracking_uri(uri="http://127.0.0.1:8080")
# Create a new MLflow Experiment
mlflow.set_experiment("MLflow Quickstart")
# Start an MLflow run
with mlflow.start_run():
# Log the hyperparameters
mlflow.log_params(params)
# Log the loss metric
mlflow.log_metric("accuracy", accuracy)
# Set a tag that we can use to remind ourselves what this run was for
mlflow.set_tag("Training Info", "Basic LR model for iris data")
# Infer the model signature
signature = infer_signature(X_train, lr.predict(X_train))
# Log the model
model_info = mlflow.sklearn.log_model(
sk_model=lr,
artifact_path="iris_model",
signature=signature,
input_example=X_train,
registered_model_name="tracking-quickstart",
)
这里在原有的代码基础上加入了模型记录的代码,其实也可以把训练模型和其他逻辑的代码放进 start_run 里面,但是官方不建议这么做,因为如果你训练或者其他逻辑代码报错有什么问题,会导致之前出现空或者无效记录,就需要手动去UI里面进行清理了
这里设置链接的方式使用的是 mlflow.set_tracking_uri(uri="http://127.0.0.1:8080"),
其实还有一种方式 client = MlflowClient(tracking_uri="http://127.0.0.1:8080"),他们的区别就是如下:
统而言之就是,client方式更加灵活,可以一份代码里面有多个跟踪服务器,另一种适合一份代码只有一个跟踪服务器来使用。
Client 记录信息
这边更新下用client记录训练和model的一些信息
from mlflow import MlflowClient, start_run
from mlflow.entities import Param,RunTag,Metric
import mlflow
from mlflow.models import infer_signature
from mlflow.models.model import ModelInfo
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
self.client:MlflowClient=MlflowClient(tracking_uri="http://127.0.0.1:8080")
X, y = datasets.load_iris(return_X_y=True)
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Define the model hyperparameters
params = {
"solver": "lbfgs",
"max_iter": 1000,
"multi_class": "auto",
"random_state": 8888,
}
# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
# Predict on the test set
y_pred = lr.predict(X_test)
# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
experiment=mlflow.search_experiments(filter_string="name = 'Test_Model'").pop()
run = self.client.create_run(experiment._experiment_id)
#self.client.log_param(run.info.run_id,**params)
#self.client.log_metric(run.info.run_id,"accuracy", accuracy)
#self.client.set_tag(run.info.run_id, "model", "Logistic Regression")
params_list = [Param(key=key, value=str(value)) for key, value in params.items()]
metrics_list=[Metric( key="accuracy",value=accuracy,timestamp=int(time.time() * 1000),step=1,)]
tags_list=[RunTag("model","Logistic Regression")]
self.client.log_batch(run_id=run.info.run_id,params=params_list,metrics=metrics_list,tags=tags_list)
signature = infer_signature(X_train, lr.predict(X_train))
model_info=self.client.log_model(run.info.run_id, "model", lr, signature=signature)
self.client.set_terminated(run.info.run_id)
return model_info.run_id
关于 client.set_terminated()这个方法
set_terminated是一个用于终止运行的方法,它可以在MLflow中将运行的状态设置为终止(terminated)。运行的状态可以是以下三种之一:
运行中(running):表示运行正在进行中,可以记录参数、指标、标签和模型。
终止(terminated):表示运行已经结束,不可以再记录任何数据。
失败(failed):表示运行出现了错误,不可以再记录任何数据。
当您使用mlflow.start_run()开始一个运行时,运行的状态默认是运行中(running)。当您使用mlflow.end_run()结束一个运行时,运行的状态会自动变为终止(terminated)。
这里面我把client.log_param和metric还有tag注了是因为哪个log_batch就可以一次性把这三个都记录,但是传入值必须规范,parm需要传入[Param]之类的。
调用Model
import mlflow
from mlflow.models import infer_signature
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Load the Iris dataset
X, y = datasets.load_iris(return_X_y=True)
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Define the model hyperparameters
params = {
"solver": "lbfgs",
"max_iter": 1000,
"multi_class": "auto",
"random_state": 8888,
}
# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
# Predict on the test set
y_pred = lr.predict(X_test)
# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
print(accuracy)
# Set our tracking server uri for logging
mlflow.set_tracking_uri(uri="http://127.0.0.1:8080")
# Create a new MLflow Experiment
mlflow.set_experiment("MLflow Quickstart")
# Start an MLflow run
with mlflow.start_run():
# Log the hyperparameters
mlflow.log_params(params)
# Log the loss metric
mlflow.log_metric("accuracy", accuracy)
# Set a tag that we can use to remind ourselves what this run was for
mlflow.set_tag("Training Info", "Basic LR model for iris data")
# Infer the model signature
signature = infer_signature(X_train, lr.predict(X_train))
# Log the model
model_info = mlflow.sklearn.log_model(
sk_model=lr,
artifact_path="iris_model",
signature=signature,
input_example=X_train,
registered_model_name="tracking-quickstart",
)
print(f'{model_info.model_uri}')
# Load the model back for predictions as a generic Python Function model
loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)
predictions = loaded_model.predict(X_test)
iris_feature_names = datasets.load_iris().feature_names
result = pd.DataFrame(X_test, columns=iris_feature_names)
result["actual_class"] = y_test
result["predicted_class"] = predictions
print(result[:4])
这里在之前基础上加了调用模型的代码
FE查看
我们返回到FE中,可以看到1这里 RUN Name 会随机生成,如果你想要指定特殊的名字就可以这样
with mlflow.start_run(run_name="test1"):
pass
还有就是每有个一 with mlflow.start_run()就会有一条这个记录,可以看到3这里是没有model的,因为我测试的时候就是一个空的with mlflow.start_run()
点击某一个run name就可以进入详情页
红线这里就是model的url,可以直接通过这个url调用到该model,
左边文件栏里面可以看到具体记录了哪些文件。有pkl的模型,还有示例输入,还有该模型需要的依赖等等。
发布模型
我们也可以直接发布模型,它可以docker或者k8s容器发布,我这边就介绍最简单的独立发布。
首先我们需要配置环境变量MLFLOW_TRACKING_URI,值为你本地mlflow server启动的地址,默认情况就是http://127.0.0.1:8080。
然后是保证你mlflow中是有记录模型的,然后执行下面命令(需要有flask环境,没有的话需要pip install flask)
mlflow models serve -m models:/{model_name}/{version} --no-conda -p 5001 -h 0.0.0.0
mlflow models serve命令是用来在本地部署一个MLflow模型的。它会启动一个Flask服务器,提供一个REST API来预测模型的输出。这上面的参数来指定了模型的位置,端口号,主机名,以及是否使用conda环境:
-m 或 --model-uri:模型的URI,可以是本地文件系统,S3,Azure ML等。
-p 或 --port:服务器的端口号,默认是5000。
-h 或 --host:服务器的主机名,默认是127.0.0.1。
–no-conda:如果指定了这个参数,那么不会使用conda环境来运行模型,而是使用当前的Python环境。
model_name mlflow server里面可以看到
执行命令之后看到这个输出代表启动成功了
接下来就是测试了,
mlflow models serve 命令部署模型到本地,只需要访问一个 api,就是 /invocations。这个 api 用于接收模型的输入数据,并返回预测结果。不需要自己定义这个 api,它是由 mlflow 自动生成的。
可以用curl或者postman进行测试
curl -X POST -H "Content-Type:application/json" --data '{"input":[[0,0]]}' http://localhost:5001/invocations
如果想知道是否正常运行的model service,可以call它的/ping routepoint
如果返回是200的话代表模型正常运行,如果其他状态码就表明有问题了。
根据我的学习进度还在更新中....
参考链接: https://mlflow.org/docs/latest/getting-started/intro-quickstart/index.html