譯者 | 李睿
審校 | 重樓
MLflow是一個開源平臺,專門用于處理機器學習過程的整個生命周期。本文介紹的綜合指南將從初學者開始,逐步提升至高級專家的水平,將涵蓋使用Python代碼時的所有重要功能。通過這個綜合指南,將全面了解MLflow,并能夠管理實驗、打包代碼、管理模型以及部署模型。
MLflow簡介
設置MLflow
從“MLflow跟蹤”到“查詢實驗”
MLflow是涵蓋機器學習過程生命周期的重要工具;該范圍由實驗、可再現(xiàn)性和部署組成。以下是MLflow主要組件的概述:
- MLflow跟蹤:用于記錄和查詢實驗。
- MLflow項目:打包機器學習代碼,使其可重用和可復制。
- MLflow模型:部署和管理模型。
- MLflow模型注冊表:專為管理模型而定制的存儲庫。
安裝
以下的代碼用于使用pip安裝MLflow:
Shell:
1 !pip install mlflow
設置跟蹤服務器
以下代碼設置了一個MLflow跟蹤服務器,其中包含用于后端存儲的SQLite,以及用于項目的目錄./mlflow.db和./artifacts。
Shell:
1 !mlflow server --backend-store-uri sqlite:///mlflow.db
--default-artifact-root ./artifacts
MLflow可以用于記錄和查詢實驗。日志記錄需要運行一個程序,并且要查詢實驗,運行以下代碼行:
Python:
1 import mlflow
2
3 with mlflow.start_run(): # Start a decorator
4 mlflow.log_param("param1", 5) # Log a parameter
5 mlflow.log_metric("metric1", 0.85) # Log a metric
6 mlflow.log_artifact("path/to/artifact") # Log an artifact
示例用例
端到端項目
Python:
1 runs = mlflow.search_runs()
2print(runs)
MLflow項目
MLflow項目是組織和打包代碼的一種方式。項目只是一個帶有MLproject文件的目錄。
(1)創(chuàng)建MLproject文件
以下是一個MLproject文件的例子:
Python:
1 name: MyProject
2
3 conda_env: conda.yaml
4
5 entry_points:
6 main:
7 parameters:
8 param1: { type: int, default: 5 }
9 command: "python train.py --param1 {param1}"
(2)運行項目
要運行一個項目,使用mlflow run命令:
Shell:
1 mlflow run . -P param1=10
MLflow模型
MLflow模型是打包機器學習模型的標準方法。其想法是,使用MLflow以許多不同的格式保存模型,例如Python、R甚至Java。
(1)保存模型
以下是在Python中保存模型的方法:
Python:
1 from sklearn.ensemble import RandomForestClassifier
2
3 model = RandomForestClassifier()
4 model.fit(X_train, y_train)
5
6 mlflow.sklearn.log_model(model, "model")
7
(2)加載模型
以下是是加載已經(jīng)保存模型的方法:
Python:
1 model = mlflow.sklearn.load_model("runs://model")
2 predictions = model.predict(X_test)
MLflow模型注冊表
MLflow模型注冊表是管理模型的中心存儲庫。
(1)注冊模型
為了注冊一個模型,你需要先記錄它,然后才能注冊:
Python:
1 result = mlflow.register_model("runs://model", "MyModel")
(2)管理模型版本
然后,可以通過在不同階段(例如Staging和Production)之間進行轉換來管理模型的不同版本:
Python:
1 from mlflow.tracking import MlflowClient
2
3 client = MlflowClient()
4
5 client.transition_model_version_stage(
6 name="MyModel",
7 version=1,
8 stage="Production"
9 )
高級功能和集成
(1)與GenAI集成
MLflow可以很好地支持GenAI模型,包括OpenAI、Transformer和LangChain。以下是如何記錄和部署OpenAI模型的示例:
Python:
1 import mlflow.openai
2
3with mlflow.start_run():
4 response = openai.Completion.create(
5 model="text-davinci-003",
6 prompt="Translate the following English text to French: '{}'",
7 max_tokens=60
8 )
9 mlflow.openai.log_model(response, "openai-model")
(2)提示工程界面
MLflow的提示工程用戶界面(UI)允許交互式地開發(fā)和評估提示。
(3)部署
使用MLflow很容易部署模型。例如,可以使用MLflow的REST API來服務一個模型:
Shell:
1 mlflow models serve -m runs://model --port 1234
MLflow的示例用例
用例1:超參數(shù)調(diào)優(yōu)的實驗跟蹤
當為機器學習模型調(diào)優(yōu)超參數(shù)時,跟蹤每個實驗的參數(shù)和結果以了解最佳模型配置是很重要的。如果第一次使用MLflow,那么在進一步討論這個用例之前,下面的步驟將指導人們安裝MLflow庫。
假設有一個隨機森林分類器,有一些超參數(shù)需要調(diào)優(yōu):
Python:
1 import mlflow
2 import mlflow.sklearn
3 from sklearn.ensemble import RandomForestClassifier
4 from sklearn.model_selection import train_test_split
5 from sklearn.datasets import load_iris
6 from sklearn.metrics import accuracy_score
7
8 # Loading the data
9 data = load_iris()
10 X_train, X_test, y_train, y_test = train_test_split(data.data,
data.target, test_size=0.2)
11
12 # Combining the hyperparameters we would like to test
13 n_estimators = [10, 50, 100]
14 max_depth = [5, 10, 20]
15
16 # Starting the MLflow experiment
17 mlflow.set_experiment("RandomForest_Hyperparameter_Tuning")
YAML:
1 conda_env: conda.yaml
2
3 entry_points:
4 train:
5 parameters:
6 n_estimators: { type: int, default: 100 }
7 max_depth: { type: int, default: 6 }
8 command: "python train.py {n_estimators} {max_depth}"
步驟1:創(chuàng)建Conda環(huán)境
創(chuàng)建名為conda.yaml的文件,并添加如下內(nèi)容:
YAML:
1 name: wine_quality
2 dependencies:
3 - python=3.7
4 - pip
5 - scikit-learn
6 - pandas
7 - mlflow
然后執(zhí)行如下命令創(chuàng)建conda環(huán)境。
YAML:
1 conda env create -f conda.yaml
步驟2:執(zhí)行培訓腳本
創(chuàng)建一個名為train.py的文件,并添加以下腳本來實現(xiàn)訓練邏輯:
Python:
1 import mlflow
2 from sklearn.ensemble import RandomForestClassifier
3 from sklearn.metrics import accuracy_score
4
5 def load_data():
6 # Load and preprocess the wine quality data
7 return X_train, X_test, y_train, y_test
8
9 n_estimators = [100, 200, 300]
10 max_depth = [6, 8, 10]
11
12 for n in n_estimators:
13 for depth in max_depth:
14 with mlflow.start_run():
15 # Train model
16 model = RandomForestClassifier(n_estimators=n, max_depth=depth)
17 model.fit(X_train, y_train)
18
19 # Log parameters and metrics
20 mlflow.log_param("n_estimators", n)
21 mlflow.log_param("max_depth", depth)
22 predictions = model.predict(X_test)
23 accuracy = accuracy_score(y_test, predictions)
24 mlflow.log_metric("accuracy", accuracy)
25
26 # Log model
27 mlflow.sklearn.log_model(model, "model")
在這個腳本中,采用不同的超參數(shù)訓練多個隨機森林分類器并記錄結果。將load_data()函數(shù)替換為加載和預處理實際葡萄酒質量數(shù)據(jù)的代碼。
YAML:
1 conda_env: conda.yaml
2
3 entry_points:
4 main:
5 parameters:
6 n_estimators: { type: int, default: 100 }
7 max_depth: { type: int, default: 10 }
8 command: "python train.py --n_estimators {n_estimators} --max_depth
{max_depth}"
已經(jīng)創(chuàng)建了train.py腳本來訓練和記錄示例模型的結果?,F(xiàn)在,將創(chuàng)建以下文件來執(zhí)行MLflow運行:
- conda.yaml文件指定conda環(huán)境。
- train.py文件指定入口點。
- 修改現(xiàn)有的load_data.py和winequality_dataset.py文件,以糾正路徑規(guī)范中的錯誤。
步驟3:定義Conda環(huán)境
創(chuàng)建conda.yaml指定環(huán)境依賴項:
YAML:
1 name: wine_quality_env
2 channels:
3 - defaults
4 dependencies:
5 - python=3.8
6 - scikit-learn
7 - pandas
8 - mlflow
步驟4:編寫訓練腳本
創(chuàng)建train.py腳本來訓練模型并記錄結果:
Python
1 import argparse
2 import pandas as pd
3 import mlflow
4 import mlflow.sklearn
5 from sklearn.ensemble import RandomForestClassifier
6 from sklearn.metrics import accuracy_score
7 from sklearn.model_selection import train_test_split
8
9 def main(n_estimators, max_depth):
10 # Load data
11 data = pd.read_csv("data/winequality-red.csv", sep=';')
12 X = data.drop("quality", axis=1)
13 y = data["quality"]
14 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
15
16 # Train model
17 model = RandomForestClassifier(n_estimators=n_estimators,
max_depth=max_depth)
18 model.fit(X_train, y_train)
19
20 # Log parameters and metrics
21 with mlflow.start_run():
22 mlflow.log_param("n_estimators", n_estimators)
23 mlflow.log_param("max_depth", max_depth)
24 predictions = model.predict(X_test)
25 accuracy = accuracy_score(y_test, predictions)
26 mlflow.log_metric("accuracy", accuracy)
27
28 # Log model
29 mlflow.sklearn.log_model(model, "model")
30
31 if __name__ == "__main__":
32 parser = argparse.ArgumentParser()
33 parser.add_argument("--n_estimators", type=int, default=100)
34 parser.add_argument("--max_depth", type=int, default=10)
35 args = parser.parse_args()
36 main(args.n_estimators, args.max_depth)
步驟5:運行項目
使用mlflow Run命令運行項目:
Shell:
1 mlflow run . -P n_estimators=200 -P max_depth=15
步驟6:注冊和部署模型
運行項目后,可以注冊模型并部署:
Python:
1 from mlflow.tracking import MlflowClient
2
3 client = MlflowClient()
4 run_id = ""
5 model_uri = f"runs:/{run_id}/model"
6 model_details = client.create_registered_model("WineQualityModel")
7
8 # Register model
9 client.create_model_version(
10 name="WineQualityModel",
11 source=model_uri,
12 run_id=run_id
13 )
用以下的命令創(chuàng)建模型的服務版本:
Shell:
1 mlflow models serve -m models:/WineQualityModel/1
步驟7:做出預測
可以通過發(fā)送HTTP請求來進行預測。以下是如何使用請求庫實現(xiàn)這一目的:
Python:
1 import requests
2 import json
3
4 url = "http://127.0.0.1:5001/invocations"
5 data = {
6 "columns": [
7 "fixed acidity", "volatile acidity", "citric acid", "residual sugar",
8 "chlorides", "free sulfur dioxide", "total sulfur dioxide", "density",
9 "pH", "sulphates", "alcohol"
10 ],
11 "data": [[7.4, 0.7, 0.0, 1.9, 0.076, 11.0, 34.0, 0.9978, 3.51, 0.56,
9.4]]
12 }
13
14 response = requests.post(
15 url,
16 data=json.dumps(data),
17 headers={"Content-Type": "application/json"}
18 )
19
20 print(response.json())
結論
在這份指南中,通過一系列示例和一個綜合項目演示了MLflow的應用。在掌握了所有必要的信息之后,通過提高機器學習項目管理過程的效率和功能來最大限度地提高MLflow的效率??梢詫⑻峁┑捻椖孔鳛槲磥眄椖亢拖敕ǖ幕A。需要注意的是,這里提供的信息是官方文檔的簡明版本。有關更全面的信息,可以參閱MLflow官方指南,該指南概述了Python中的關鍵概念和有用的示例。
原文標題:A Comprehensive Guide to MLflow for Machine Learning Lifecycle Management,作者:Harsh Daiya
鏈接:https://dzone.com/articles/from-novice-to-advanced-in-mlflow-a-comprehensive。