在 DVC 中比较指标与图表

面向机器学习的 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
面向机器学习的 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
面向机器学习的 CI/CD

配置 DVC GitHub Action

  • 添加 setup-dvc GitHub Action
  • 用 DVC 流水线替换运行 Python 脚本
steps:
  ...
  - name: Setup DVC
    uses: iterative/setup-dvc@v1

- name: 运行 DVC 流水线 run: dvc repro
面向机器学习的 CI/CD

配置 DVC GitHub Action

- name: 写入 CML 报告
  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
面向机器学习的 CI/CD

流水线实战

拉取请求评论中显示与 main 分支对比的指标差异的截图

面向机器学习的 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
面向机器学习的 CI/CD

为图表配置 DVC YAML

stages:
  train:
    ...
    plots:
    - predictions.csv: # 存放预测的文件名
        template: confusion # 图表样式

x: predicted_label # CSV 中的 X 轴列名 y: true_label # CSV 中的 Y 轴列名 x_label: '预测标签' y_label: '真实标签' title: 混淆矩阵
cache: false # 保存在 Git 中
面向机器学习的 CI/CD

绘制混淆矩阵

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

DVC 生成的混淆矩阵图表显示

面向机器学习的 CI/CD

对比混淆矩阵

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

DVC 生成的混淆矩阵差异图

面向机器学习的 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: '假阳性率'
    y_label: '真阳性率'
    title: ROC 曲线
    cache: false

DVC 生成的 ROC 曲线差异图

面向机器学习的 CI/CD

Passons à la pratique !

面向机器学习的 CI/CD

Preparing Video For Download...