英文鏈接:https://mlflow.org/docs/latest/tutorial.html
本文鏈接:https://www.cnblogs.com/CheeseZH/p/11943280.html
這篇教程展示了如何:
- 訓練一個線性回歸模型
- 將訓練代碼打包成一個可復用可復現的模型格式
- 將模型部署成一個簡單的HTTP服務用於進行預測
這篇教程使用的數據來自UCI的紅酒質量數據集,主要用於根據紅酒的PH值,酸度,殘糖量等指標來評估紅酒的質量。
我們會用到什么?
- 如果使用的是MacOS,官方推薦使用python3環境。
- 安裝MLflow和scikit-learn,推薦兩種安裝方式:
- 安裝MLflow及其依賴:
pip install mlflow[extras]
- 分別安裝MLflow(
pip install mlflow
)和scikit-learn(pip install scikit-learn
)
- 安裝MLflow及其依賴:
- 安裝conda
- 我安裝的是miniconda
訓練模型
我們要訓練的線性回歸模型包含兩個超參數:alpha
和l1_ratio
。
我們使用下邊的train.py
代碼進行訓練
# The data set used in this example is from http://archive.ics.uci.edu/ml/datasets/Wine+Quality
# P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J. Reis.
# Modeling wine preferences by data mining from physicochemical properties. In Decision Support Systems, Elsevier, 47(4):547-553, 2009.
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
import mlflow.sklearn
import logging
logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__name__)
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 from the URL
csv_url =\
'http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv'
try:
data = pd.read_csv(csv_url, sep=';')
except Exception as e:
logger.exception(
"Unable to download training & test CSV, check your internet connection. Error: %s", e)
# 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
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")
這個例子用pandas,numpy,sklearn的API構建了一個簡單的機器學習模型。通過MLflow tracking APIs來記錄每次訓練的信息,比如模型超參數和模型的評價指標。這個例子還將模型進行了序列化以便后續部署。
我們用不同的超參數訓練幾次模型
python train.py 0.5 0.5
python train.py 0.4 0.4
python train.py 0.6 0.6
每次運行完訓練腳本,MLflow都會將信息保存在目錄mlruns
中。
對比模型
在mlruns
目錄的上級目錄中運行下邊的命令:
mlflow ui
然后就可以通過http://localhost:5000來查看每個版本的模型了。
我們可以通過搜索功能快速篩選感興趣的模型,比如搜索條件設置為metrics.rmse<0.8
可以將rmse小於0.8的模型找出來,如果更復雜的搜索條件,可以下載CSV文件並用其他軟件進行分析。
打包模型
我們已經寫好了訓練代碼,可以將其打包提供給其他的數據科學家來復用,或者可以進行遠程訓練。
我們根據MLflow Projects的慣例來指定代碼的依賴和代碼的入口。比如創建一個sklearn_elasticnet_wine
目錄,在這個目錄下創建一個MLproject
文件來指定項目的conda依賴和包含兩個參數alpha/l1_ratio
的入口文件。
# sklearn_elasticnet_wine/MLproject
name: tutorial
conda_env: conda.yaml
entry_points:
main:
parameters:
alpha: float
l1_ratio: {type: float, default: 0.1}
command: "python train.py {alpha} {l1_ratio}"
conda.yaml文件列舉了所有依賴:
# sklearn_elasticnet_wine/conda.yaml
name: tutorial
channels:
- defaults
dependencies:
- numpy=1.14.3
- pandas=0.22.0
- scikit-learn=0.19.1
- pip:
- mlflow
通過執行mlflow run examples/sklearn_elasticnet_wine -P alpha=0.42
可以運行這個項目,MLflow會根據conda.yaml的配置在指定的conda環境中訓練模型。
如果代碼倉庫的根目錄有MLproject
文件,也可以直接通過Github來運行,比如代碼倉庫:https://github.com/mlflow/mlflow-example。我們可以執行這個命令mlflow run git@github.com:mlflow/mlflow-example.git -P alpha=0.42
來訓練模型。
部署模型
我們通過MLflow Models來部署模型。一個MLflow Model是一種打包機器學習模型的標准格式,可以被用於多種下游工具,比如實時推理的REST API或者批量推理的Apache Spark。
在訓練代碼中,這行代碼用於保存模型(原文稱為artifact,暫且翻譯成模型產品
吧):
mlflow.sklearn.log_model(lr, "model")
我們可以在UI中通過點擊Date
鏈接來查看每次訓練的模型產品,例如:
在底部,我們可以看到通過調用mlflow.sklearn.log_model
產生了兩個文件,位於類似
目錄/Users/mlflow/mlflow-prototype/mlruns/0/7c1a0d5c42844dcdb8f5191146925174/artifacts/model
。MLmodel
元數據文件是告訴MLflow如何加載模型。model.pkl
文件是訓練好的序列化的線性回歸模型。
運行下邊的命令,可以將模型部署成本地REST服務(要確保訓練模型和部署模型所用的python版本一致,否則會報錯
):
# 需要替換成你自己的目錄
mlflow models serve -m /Users/mlflow/mlflow-prototype/mlruns/0/7c1a0d5c42844dcdb8f5191146925174/artifacts/model -p 1234
部署好服務之后,可以通過curl命令發送json序列化的pandas DataFrame來測試下。模型服務器接受的數據格式可以參考MLflow deployment tools documentation.
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
服務器會返回類似輸出:
[6.379428821398614]