背景
mlflow是Databrick開(kāi)源的機(jī)器學(xué)習(xí)管理平臺(tái)故俐,它很好的解藕了算法訓(xùn)練和算法模型服務(wù),使得算法工程師專(zhuān)注于模型的訓(xùn)練榨馁,而不需要過(guò)多的關(guān)注于服務(wù)的,
而且在我們公司已經(jīng)有十多個(gè)服務(wù)穩(wěn)定運(yùn)行了兩年多。
搭建
mlflow的搭建主要是mlflow tracking server的搭建,tracking server主要是用于模型的元數(shù)據(jù)以及模型的數(shù)據(jù)存儲(chǔ)
我們這次以minio作為模型數(shù)據(jù)的存儲(chǔ)后臺(tái)涛癌,mysql作為模型元數(shù)據(jù)的存儲(chǔ),因?yàn)檫@種模式能滿足線上的需求送火,不僅僅是用于測(cè)試
minio的搭建
參考我之前的文章MinIO的搭建使用拳话,并且創(chuàng)建名為mlflow的bucket,便于后續(xù)操作-
mlflow的搭建
- conda的安裝
參照install conda,根據(jù)自己的系統(tǒng)安裝不同的conda環(huán)境 - mlfow tracking server安裝
# 創(chuàng)建conda環(huán)境 并安裝 python 3.6 conda create -n mlflow-1.11.0 python==3.6 #激活conda環(huán)境 conda activate mlflow-1.11.0 # 安裝mlfow tracking server python需要的依賴包 pip install mlflow==1.11.0 pip install mysqlclient pip install boto3
- mlflow tracking server的啟動(dòng)
暴露出minio url以及需要的ID和KEY种吸,因?yàn)閙lflow tracking server在上傳模型文件時(shí)需要 export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY export MLFLOW_S3_ENDPOINT_URL=http://localhost:9001 mlflow server \ --backend-store-uri mysql://root:AO,h07ObIeH-@localhost/mlflow_test \ --host 0.0.0.0 -p 5002 \ --default-artifact-root s3://mlflow
訪問(wèn)localhost:5002, 就能看到如下界面:
- conda的安裝
使用
拷貝以下的wine.py文件
import os
import warnings
import sys
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import ElasticNet
import mlflow.sklearn
def eval_metrics(actual, pred):
rmse = np.sqrt(mean_squared_error(actual, pred))
mae = mean_absolute_error(actual, pred)
r2 = r2_score(actual, pred)
return rmse, mae, r2
if __name__ == "__main__":
warnings.filterwarnings("ignore")
np.random.seed(40)
# Read the wine-quality csv file (make sure you're running this from the root of MLflow!)
wine_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "wine-quality.csv")
data = pd.read_csv(wine_path)
# Split the data into training and test sets. (0.75, 0.25) split.
train, test = train_test_split(data)
# The predicted column is "quality" which is a scalar from [3, 9]
train_x = train.drop(["quality"], axis=1)
test_x = test.drop(["quality"], axis=1)
train_y = train[["quality"]]
test_y = test[["quality"]]
alpha = float(sys.argv[1]) if len(sys.argv) > 1 else 0.5
l1_ratio = float(sys.argv[2]) if len(sys.argv) > 2 else 0.5
mlflow.set_tracking_uri("http://localhost:5002")
client = mlflow.tracking.MlflowClient()
mlflow.set_experiment('http_metrics_test')
with mlflow.start_run():
lr = ElasticNet(alpha=alpha, l1_ratio=l1_ratio, random_state=42)
lr.fit(train_x, train_y)
predicted_qualities = lr.predict(test_x)
(rmse, mae, r2) = eval_metrics(test_y, predicted_qualities)
print("Elasticnet model (alpha=%f, l1_ratio=%f):" % (alpha, l1_ratio))
print(" RMSE: %s" % rmse)
print(" MAE: %s" % mae)
print(" R2: %s" % r2)
mlflow.log_param("alpha", alpha)
mlflow.log_param("l1_ratio", l1_ratio)
mlflow.log_metric("rmse", rmse)
mlflow.log_metric("r2", r2)
mlflow.log_metric("mae", mae)
mlflow.sklearn.log_model(lr, "model")
注意:
1.mlflow.set_tracking_uri("http://localhost:5002")
設(shè)置為剛才啟動(dòng)的mlflow tracking server的地址
2.mlflow.set_experiment('http_metrics_test')
設(shè)置實(shí)驗(yàn)的名字
3.安裝該程序所依賴的python包
4.如果不是在同一個(gè)conda環(huán)境中弃衍,還得執(zhí)行
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export MLFLOW_S3_ENDPOINT_URL=http://localhost:9001
便于python客戶端上傳模型文件以及模型元數(shù)據(jù)
直接執(zhí)行 python wine.py 如果成功,訪問(wèn)mlflow tracking server ui下有如下
點(diǎn)擊 2020-10-30 10:34:38坚俗,如下:
啟動(dòng)mlflow 算法服務(wù)
在同一個(gè)conda環(huán)境中執(zhí)行命令
export MLFLOW_TRACKING_URI=http://localhost:5002
mlflow models serve -m runs:/e69aed0b22fb45debd115dfc09dbc75a/model -p 1234 --no-conda
其中e69aed0b22fb45debd115dfc09dbc75a為mlflow tracking server ui中的run id
如遇到ModuleNotFoundError: No module named 'sklearn'
執(zhí)行 pip install scikit-learn==0.19.1
遇到ModuleNotFoundError: No module named 'scipy'
執(zhí)行pip install scipy
請(qǐng)求訪問(wèn)該model啟動(dòng)的服務(wù):
curl -X POST -H "Content-Type:application/json; format=pandas-split" --data '{"columns":["alcohol", "chlorides", "citric acid", "density", "fixed acidity", "free sulfur dioxide", "pH", "residual sugar", "sulphates", "total sulfur dioxide", "volatile acidity"],"data":[[12.8, 0.029, 0.48, 0.98, 6.2, 29, 3.33, 1.2, 0.39, 75, 0.66]]}' http://127.0.0.1:1234/invocations
輸出 [5.455573233630147]
則表明該模型服務(wù)成功部署
至此主要簡(jiǎn)單的mlflow使用就完成了镜盯,如果還有mlflow不支持的算法,可以參照自定義model