使用 GitHub Actions 訓練模型

Machine Learning 的 CI/CD

Ravi Bhadauria

Machine Learning Engineer

資料集:澳洲天氣預測

  • 二元分類
    • 預測明日是否下雨
  • 5 個類別特徵
    • Location
    • WindGustDir
    • WindDir9am
    • WindDir3pm
    • RainToday
  • 17 個數值特徵
    • MinTemp
    • MaxTemp
    • Rainfall
    • Evaporation
    • ...
    • WindGustSpeed
    • Cloud3pm
    • Temp9am
    • RISK_MM
1 https://www.kaggle.com/datasets/rever3nd/weather-data
Machine Learning 的 CI/CD

建模流程

  • 資料前處理
    • 將類別特徵轉為數值
    • 填補特徵的遺漏值
    • 特徵縮放
  • 隨機森林分類器
    • max_depth = 2n_estimators = 50
  • 測試資料的標準評估指標
    • 效能圖
      • 混淆矩陣圖
Machine Learning 的 CI/CD

資料準備:目標編碼(target encoding)

def target_encode_categorical_features(
    df: pd.DataFrame, categorical_columns: List[str], target_column: str
) -> pd.DataFrame:
    encoded_data = df.copy()

    # Iterate through categorical columns
    for col in categorical_columns:
        # Calculate mean target value for each category
        encoding_map = df.groupby(col)[target_column].mean().to_dict()

        # Apply target encoding
        encoded_data[col] = encoded_data[col].map(encoding_map)

    return encoded_data
1 https://maxhalford.github.io/blog/target-encoding/
Machine Learning 的 CI/CD

填補與縮放

def impute_and_scale_data(df_features: pd.DataFrame) -> pd.DataFrame:
    # Impute data with mean strategy
    imputer = SimpleImputer(strategy="mean")
    X_preprocessed = imputer.fit_transform(df_features.values)

    # Scale and fit with zero mean and unit variance
    scaler = StandardScaler()
    X_preprocessed = scaler.fit_transform(X_preprocessed)

    return pd.DataFrame(X_preprocessed, columns=df_features.columns)
Machine Learning 的 CI/CD

訓練

  • 訓練/測試切分
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
  data.drop(TARGET_COLUMN), data[TARGET_COLUMN], random_state=1993)
  • 隨機森林分類器
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(
  max_depth=2, n_estimators=50, random_state=1993)
clf.fit(X_train, y_train)
Machine Learning 的 CI/CD

評估指標

from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score

# Calculate predictions
y_pred = model.predict(X_test)

# Calculate accuracy accuracy = accuracy_score(y_test, y_pred)
# Calculate precision precision = precision_score(y_test, y_pred)
# Calculate recall recall = recall_score(y_test, y_pred)
# Calculate f1 score f1 = f1_score(y_test, y_pred)
1 https://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics
Machine Learning 的 CI/CD

圖表

from sklearn.metrics import ConfusionMatrixDisplay
ConfusionMatrixDisplay.from_estimator(model, X_test, y_test,cmap=plt.cm.Blues)

在測試集上計算的混淆矩陣圖

Machine Learning 的 CI/CD

GitHub Actions 工作流程

模型訓練持續整合流程圖

  • 持續機器學習(CML)
    • 機器學習的 CI/CD 工具
    • GitHub Actions 整合
      • 佈建訓練機器
      • 執行訓練與評估
      • 比較實驗
      • 監控資料集
      • 視覺化報告
1 https://cml.dev/ 2 https://martinfowler.com/bliki/FeatureBranch.html
Machine Learning 的 CI/CD

CML 指令

# Enable setup-cml action to be used later
- uses: iterative/setup-cml@v1
- name: Train model
  run: |
    # Your ML workflow goes here
    pip install -r requirements.txt
    python3 train.py
1 https://www.markdownguide.org/basic-syntax/#images
Machine Learning 的 CI/CD

CML 指令

- name: Write CML report
  run: |
    # Add results and plots to markdown
    cat results.txt >> report.md
    echo "![training graph](./graph.png)" >> report.md

# Create comment from markdown report cml comment create report.md
env: REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Machine Learning 的 CI/CD

輸出

Pull request 頁面螢幕擷取畫面,顯示由 CML 產生的留言

Machine Learning 的 CI/CD

一起來練習吧!

Machine Learning 的 CI/CD

Preparing Video For Download...