譯者 | 李睿
審校 | 重樓
MLflow是一個(gè)開源平臺(tái),專門用于處理機(jī)器學(xué)習(xí)過程的整個(gè)生命周期。本文介紹的綜合指南將從初學(xué)者開始,逐步提升至高級(jí)專家的水平,將涵蓋使用Python代碼時(shí)的所有重要功能。通過這個(gè)綜合指南,將全面了解MLflow,并能夠管理實(shí)驗(yàn)、打包代碼、管理模型以及部署模型。
MLflow簡介
設(shè)置MLflow
從“MLflow跟蹤”到“查詢實(shí)驗(yàn)”
MLflow是涵蓋機(jī)器學(xué)習(xí)過程生命周期的重要工具;該范圍由實(shí)驗(yàn)、可再現(xiàn)性和部署組成。以下是MLflow主要組件的概述:
- MLflow跟蹤:用于記錄和查詢實(shí)驗(yàn)。
- MLflow項(xiàng)目:打包機(jī)器學(xué)習(xí)代碼,使其可重用和可復(fù)制。
- MLflow模型:部署和管理模型。
- MLflow模型注冊(cè)表:專為管理模型而定制的存儲(chǔ)庫。
安裝
以下的代碼用于使用pip安裝MLflow:
Shell:
1 !pip install mlflow
設(shè)置跟蹤服務(wù)器
以下代碼設(shè)置了一個(gè)MLflow跟蹤服務(wù)器,其中包含用于后端存儲(chǔ)的SQLite,以及用于項(xiàng)目的目錄./mlflow.db和./artifacts。
Shell:
1 !mlflow server --backend-store-uri sqlite:///mlflow.db
--default-artifact-root ./artifacts
MLflow可以用于記錄和查詢實(shí)驗(yàn)。日志記錄需要運(yùn)行一個(gè)程序,并且要查詢實(shí)驗(yàn),運(yùn)行以下代碼行:
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
示例用例
端到端項(xiàng)目
Python:
1 runs = mlflow.search_runs()
2print(runs)
MLflow項(xiàng)目
MLflow項(xiàng)目是組織和打包代碼的一種方式。項(xiàng)目只是一個(gè)帶有MLproject文件的目錄。
(1)創(chuàng)建MLproject文件
以下是一個(gè)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)運(yùn)行項(xiàng)目
要運(yùn)行一個(gè)項(xiàng)目,使用mlflow run命令:
Shell:
1 mlflow run . -P param1=10
MLflow模型
MLflow模型是打包機(jī)器學(xué)習(xí)模型的標(biāo)準(zhǔn)方法。其想法是,使用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模型注冊(cè)表
MLflow模型注冊(cè)表是管理模型的中心存儲(chǔ)庫。
(1)注冊(cè)模型
為了注冊(cè)一個(gè)模型,你需要先記錄它,然后才能注冊(cè):
Python:
1 result = mlflow.register_model("runs://model", "MyModel")
(2)管理模型版本
然后,可以通過在不同階段(例如Staging和Production)之間進(jìn)行轉(zhuǎn)換來管理模型的不同版本:
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 )
高級(jí)功能和集成
(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ā)和評(píng)估提示。
(3)部署
使用MLflow很容易部署模型。例如,可以使用MLflow的REST API來服務(wù)一個(gè)模型:
Shell:
1 mlflow models serve -m runs://model --port 1234
MLflow的示例用例
用例1:超參數(shù)調(diào)優(yōu)的實(shí)驗(yàn)跟蹤
當(dāng)為機(jī)器學(xué)習(xí)模型調(diào)優(yōu)超參數(shù)時(shí),跟蹤每個(gè)實(shí)驗(yàn)的參數(shù)和結(jié)果以了解最佳模型配置是很重要的。如果第一次使用MLflow,那么在進(jìn)一步討論這個(gè)用例之前,下面的步驟將指導(dǎo)人們安裝MLflow庫。
假設(shè)有一個(gè)隨機(jī)森林分類器,有一些超參數(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í)行培訓(xùn)腳本
創(chuàng)建一個(gè)名為train.py的文件,并添加以下腳本來實(shí)現(xiàn)訓(xù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")
在這個(gè)腳本中,采用不同的超參數(shù)訓(xùn)練多個(gè)隨機(jī)森林分類器并記錄結(jié)果。將load_data()函數(shù)替換為加載和預(yù)處理實(shí)際葡萄酒質(zhì)量數(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腳本來訓(xùn)練和記錄示例模型的結(jié)果。現(xiàn)在,將創(chuàng)建以下文件來執(zhí)行MLflow運(yùn)行:
- conda.yaml文件指定conda環(huán)境。
- train.py文件指定入口點(diǎn)。
- 修改現(xiàn)有的load_data.py和winequality_dataset.py文件,以糾正路徑規(guī)范中的錯(cuò)誤。
步驟3:定義Conda環(huán)境
創(chuàng)建conda.yaml指定環(huán)境依賴項(xiàng):
YAML:
1 name: wine_quality_env
2 channels:
3 - defaults
4 dependencies:
5 - python=3.8
6 - scikit-learn
7 - pandas
8 - mlflow
步驟4:編寫訓(xùn)練腳本
創(chuàng)建train.py腳本來訓(xùn)練模型并記錄結(jié)果:
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:運(yùn)行項(xiàng)目
使用mlflow Run命令運(yùn)行項(xiàng)目:
Shell:
1 mlflow run . -P n_estimators=200 -P max_depth=15
步驟6:注冊(cè)和部署模型
運(yùn)行項(xiàng)目后,可以注冊(cè)模型并部署:
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)建模型的服務(wù)版本:
Shell:
1 mlflow models serve -m models:/WineQualityModel/1
步驟7:做出預(yù)測
可以通過發(fā)送HTTP請(qǐng)求來進(jìn)行預(yù)測。以下是如何使用請(qǐng)求庫實(shí)現(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())
結(jié)論
在這份指南中,通過一系列示例和一個(gè)綜合項(xiàng)目演示了MLflow的應(yīng)用。在掌握了所有必要的信息之后,通過提高機(jī)器學(xué)習(xí)項(xiàng)目管理過程的效率和功能來最大限度地提高M(jìn)Lflow的效率。可以將提供的項(xiàng)目作為未來項(xiàng)目和想法的基礎(chǔ)。需要注意的是,這里提供的信息是官方文檔的簡明版本。有關(guān)更全面的信息,可以參閱MLflow官方指南,該指南概述了Python中的關(guān)鍵概念和有用的示例。
原文標(biāo)題:A Comprehensive Guide to MLflow for Machine Learning Lifecycle Management,作者:Harsh Daiya
鏈接:https://dzone.com/articles/from-novice-to-advanced-in-mlflow-a-comprehensive。