溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mlflow的搭建使用是怎樣的

發(fā)布時間:2021-12-10 18:02:19 來源:億速云 閱讀:350 作者:柒染 欄目:大數(shù)據(jù)

mlflow的搭建使用是怎樣的,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

背景

mlflow是Databrick開源的機器學習管理平臺,它很好的解藕了算法訓練和算法模型服務,使得算法工程師專注于模型的訓練,而不需要過多的關注于服務的,
而且在我們公司已經(jīng)有十多個服務穩(wěn)定運行了兩年多。

搭建

mlflow的搭建主要是mlflow tracking server的搭建,tracking server主要是用于模型的元數(shù)據(jù)以及模型的數(shù)據(jù)存儲
我們這次以minio作為模型數(shù)據(jù)的存儲后臺,mysql作為模型元數(shù)據(jù)的存儲,因為這種模式能滿足線上的需求,不僅僅是用于測試

  • minio的搭建
    MinIO的搭建使用,并且創(chuàng)建名為mlflow的bucket,便于后續(xù)操作

  • mlflow的搭建

    # 創(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

     

    暴露出minio url以及需要的ID和KEY,因為mlflow tracking server在上傳模型文件時需要   
    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


    訪問localhost:5002, 就能看到如下界面:
    mlflow的搭建使用是怎樣的

    • mlflow tracking server的啟動

    • conda的安裝
      參照install conda,根據(jù)自己的系統(tǒng)安裝不同的conda環(huán)境

    • mlfow tracking server安裝

使用

拷貝以下的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") 設置為剛才啟動的mlflow tracking server的地址
2.mlflow.set_experiment('http_metrics_test') 設置實驗的名字
3.安裝該程序所依賴的python包
4.如果不是在同一個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 如果成功,訪問mlflow tracking server ui下有如下
mlflow的搭建使用是怎樣的 點擊 2020-10-30 10:34:38,如下:
mlflow的搭建使用是怎樣的 mlflow的搭建使用是怎樣的

啟動mlflow 算法服務

在同一個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

請求訪問該model啟動的服務:

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] 則表明該模型服務成功部署

看完上述內(nèi)容,你們掌握mlflow的搭建使用是怎樣的的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI