設計可重現的實驗

Developing Machine Learning Models for Production

Sinan Ozdemir

Data Scientist and Author

可重現的實驗

  • 確保準確與可靠。
  • 更容易複製結果。
  • 促進更好的協作。
  • 降低 ML 模型偏誤風險。
  • 強化研究流程的完整性。
Developing Machine Learning Models for Production

MLflow

一個用於追蹤與管理機器學習實驗的開源平台。MLflow 可用來:

  • 建立可重現的 ML 管線
  • 追蹤與管理:
    • 套件相依性
    • 程式碼版本
    • 實驗設定
  • 允許多人存取實驗

mlflow

1 https://www.databricks.com/blog/2018/06/05/introducing-mlflow-an-open-source-machine-learning-platform.html
Developing Machine Learning Models for Production

使用 MLflow 的範例

# standard scikit-learn imports
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# new imports from MLflow
import mlflow
import mlflow.sklearn
Developing Machine Learning Models for Production

使用 MLflow 的範例(續)

with mlflow.start_run():  # Start an MLflow run assuming we have data prepared

    # Build and train model
    rf = RandomForestClassifier()
    rf.fit(X_train, y_train)

    # Log parameters and model information
    mlflow.log_param("n_estimators", rf.n_estimators)
    mlflow.sklearn.log_model(rf, "model")

    y_pred = rf.predict(X_test)  # Evaluate model
    accuracy = accuracy_score(y_test, y_pred)
    mlflow.log_metric("accuracy", accuracy)  # log the test accuracy metric
Developing Machine Learning Models for Production

追蹤程式碼

  • 使用 MLflow 紀錄程式碼版本與變更
  • 比較不同程式碼版本
  • 辨識產生特定結果所用的程式碼版本
  • 輕鬆重現實驗
  • 便於偵錯與除錯
Developing Machine Learning Models for Production

模型登錄庫

  • 集中式模型與中繼資料儲存庫
  • MLflow 可用來記錄、儲存並比較不同模型版本
  • 重現整個 ML 管線
  • 用於模型比較
  • 確保模型的準確與可靠

storage

Developing Machine Learning Models for Production

實驗的可重現性

  • 追蹤並記錄輸入資料、程式碼與設定
  • 重現整個 ML 管線
  • 建立對機器學習結果的信任
  • 讓他人能驗證並延伸你的工作
  • 確保不同執行之間結果一致

Developing Machine Learning Models for Production

重新檢視文件撰寫

  • 文件對可重現 ML 至關重要,應包含:
    • 模型的輸入資料。
    • 產生模型所用的程式碼。
    • 實驗中使用的各項設定。
    • 實驗結果(選擇了哪個模型等)
  • 文件應可被存取
  • 作為實驗的完整紀錄
Developing Machine Learning Models for Production

一起來練習吧!

Developing Machine Learning Models for Production

Preparing Video For Download...