在 DVC 比較指標與圖表

Machine Learning 的 CI/CD

Ravi Bhadauria

Machine Learning Engineer

設定 DVC YAML 檔

  • 設定 DVC YAML 檔以跨實驗追蹤指標
  • outs 變更為
stages:
  preprocess:
    ...
  train:
    ...
    outs:
    - metrics.json
    - confusion_matrix.png
  • 改為 metrics
stages:
  preprocess:
    ...
  train:
    ...
    outs:
    - confusion_matrix.png

metrics: - metrics.json: cache: false
Machine Learning 的 CI/CD

查詢與比較 DVC 指標

-> dvc metrics show

Path accuracy f1_score precision recall metrics.json 0.947 0.8656 0.988 0.7702

調整超參數並重新執行 dvc repro

-> dvc metrics diff

Path Metric HEAD workspace Change metrics.json accuracy 0.947 0.9995 0.0525 metrics.json f1_score 0.8656 0.9989 0.1333 metrics.json precision 0.988 0.9993 0.0113 metrics.json recall 0.7702 0.9986 0.2284
1 https://dvc.org/doc/command-reference/metrics
Machine Learning 的 CI/CD

設定 DVC Github Action

  • 加入 setup-dvc GitHub Action
  • 以 DVC pipeline 取代執行 Python 指令碼
steps:
  ...
  - name: Setup DVC
    uses: iterative/setup-dvc@v1

- name: Run DVC pipeline run: dvc repro
Machine Learning 的 CI/CD

設定 DVC Github Action

- name: Write CML report
  env:
    REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  run: |
    # 印出目前分支的指標
    dvc metrics show --md >> report.md

# 與 main 分支比較指標 git fetch --prune dvc metrics diff --md main >> report.md
# 建立 CML 報告 cml comment create report.md
Machine Learning 的 CI/CD

Pipeline 實際運作

Pull request 留言中顯示與 main 分支相比的指標差異畫面擷圖

Machine Learning 的 CI/CD

DVC 的圖表類型

  • scatter - 散佈圖
  • linear - 互動式線圖
  • simple - 非互動、可自訂線圖
  • smooth - 平滑線圖
  • confusion - 混淆矩陣
  • confusion_normalized - 正規化至 <0, 1> 的混淆矩陣
  • bar_horizontal - 水平長條圖
  • bar_horizontal_sorted - 依長條大小排序的水平長條圖
1 https://dvc.org/doc/user-guide/experiment-management/visualizing-plots#plot-templates-data-series-only
Machine Learning 的 CI/CD

設定 DVC YAML 以產生圖表

stages:
  train:
    ...
    plots:
    - predictions.csv: # 預測結果檔名
        template: confusion # 圖表樣式

x: predicted_label # CSV 中的 X 軸欄位 y: true_label # CSV 中的 Y 軸欄位 x_label: 'Predicted label' y_label: 'True label' title: Confusion matrix
cache: false # 儲存在 Git
Machine Learning 的 CI/CD

繪製混淆矩陣

-> dvc plots show predictions.csv
file:///path/to/index.html

DVC 產生的混淆矩陣顯示圖

Machine Learning 的 CI/CD

比較混淆矩陣

-> dvc plots diff --target predictions.csv main
file:///path/to/index.html

DVC 產生的混淆矩陣差異圖

Machine Learning 的 CI/CD

比較 ROC 曲線

# Python 變更
y_proba = model.predict_proba(X_test)
fpr, tpr, _ = roc_curve(y_test, 
                        y_proba[:, 1])
# dvc.yaml 變更
plots:
- roc_curve.csv:
    template: simple
    x: fpr
    y: tpr
    x_label: 'False Positive Rate'
    y_label: 'True Positive Rate'
    title: ROC curve
    cache: false

DVC 產生的 ROC 曲線差異圖

Machine Learning 的 CI/CD

一起來練習吧!

Machine Learning 的 CI/CD

Preparing Video For Download...