用 GitHub Actions 進行超參數調校的工作流程

Machine Learning 的 CI/CD

Ravi Bhadauria

Machine Learning Instructor

分支工作流程

  • 針對訓練與超參數調校分開的功能分支
    • 僅觸發對應的工作
    • 其他工作不應被觸發
    • if 條件實作
  • 超參數調校
    • 列印統計表供分析
    • 自動建立含參數變更的新 PR
  • 訓練
    • 在訓練 PR 中讀取新的參數檔
Machine Learning 的 CI/CD

設定條件式

超參數調校
jobs:
  hp_tune_and_publish_report:
    # Run when branch name starts with hp_tune/
    if: startsWith(github.head_ref, 'hp_tune/')

steps: ... - name: | DVC pipeline for hyperparameter tuning run: dvc repro -f hp_tune
訓練
jobs:
  train_and_publish_report:
    # Run when branch name starts with train/
    if: startsWith(github.head_ref, 'train/')

steps: ... - name: Run DVC pipeline for training run: dvc repro train
Machine Learning 的 CI/CD

設定工作流程權限

Repository Settings > Actions > General

用於設定工作流程權限的設定頁面截圖

Machine Learning 的 CI/CD

啟動超參數調校工作

  • 確保分支名稱以 hp_tune/ 作為前綴

GHA 工作流程截圖:超參數調校工作執行,訓練工作略過

Machine Learning 的 CI/CD

超參數調校工作的評量指標

PR 截圖:留言區顯示 GridSearch 結果

Machine Learning 的 CI/CD

從超參數執行建立訓練 PR

steps:
  - name: Create training branch
    env:
      REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    run: |
      # Branch name begins with train/
      export BRANCH_NAME=train/$(git rev-parse --short "${{ github.sha }}")

# Create PR for training cml pr create \ --user-email [email protected] \ --user-name HPBot \ --message "Hyperparameter tuning" \ --branch $BRANCH_NAME \ --target-branch main \ rfc_best_params.json
Machine Learning 的 CI/CD

新的訓練分支 PR

上方截圖:GHA 工作流程產生的新模型訓練 PR

下方截圖:GHA 工作流程產生的新模型訓練 PR(機器人建立)

Machine Learning 的 CI/CD

新的訓練分支 PR

新模型訓練 PR 中最佳參數檔的差異檢視截圖

Machine Learning 的 CI/CD

手動啟動訓練執行

  • GITHUB_TOKEN 無法觸發自己建立的 PR 上的工作流程

    • 避免遞迴執行
  • 因應作法

    • 使用具備適當權限的個人存取權杖(Personal access token)
      steps:
        - env:
            GITHUB_TOKEN: ${{ secrets.MY_TOKEN }}
      
    • 在 GHA pipeline 中於超參數調校後立即執行訓練工作
    • 強制 push 以觸發執行(也利於檢查)
      -> git checkout train/1f34fs
      -> git commit --amend --no-edit && git push -f
      
1 https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow
Machine Learning 的 CI/CD

啟動訓練工作

GHA 工作流程截圖:訓練工作執行,超參數調校工作略過

Machine Learning 的 CI/CD

一起來練習吧!

Machine Learning 的 CI/CD

Preparing Video For Download...