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 を追加します
  • Python スクリプトの実行を DVC パイプラインに置き換えます
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: |
    # Print metrics of current branch
    dvc metrics show --md >> report.md

# Compare metrics with main branch git fetch --prune dvc metrics diff --md main >> report.md
# Create CML report cml comment create report.md
Machine Learning のための CI/CD

実行中のパイプライン

メイン ブランチとのメトリクス差分を示すプルリクエストコメントのスクリーンショット

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: # Name of file containing predictions
        template: confusion # Style of plot

x: predicted_label # X-axis column name in csv file y: true_label # Y-axis column name in csv file x_label: 'Predicted label' y_label: 'True label' title: Confusion matrix
cache: false # Save in 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 曲線を比較する

# Changes in Python
y_proba = model.predict_proba(X_test)
fpr, tpr, _ = roc_curve(y_test, 
                        y_proba[:, 1])
# Changes in 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...