DVC 的安装与初始化

DVC 数据版本管理入门

Ravi Bhadauria

Machine Learning Engineer

安装

  • DVC 是一个 Python 包
    • 可用 pip 全局安装
$ pip install dvc
  • 建议在虚拟环境中安装
  • 确保已安装 Git
DVC 数据版本管理入门

验证安装

$ dvc version
DVC version: 3.40.1 (pip)

Platform: Python 3.9.16 on macOS-14.2.1-arm64-arm-64bit
Config: Global: /Users/<username>/Library/Application Support/dvc System: /Library/Application Support/dvc
Repo: dvc, git
1 https://dvc.org/doc/command-reference/version
DVC 数据版本管理入门

初始化 DVC

  • 确保已初始化 Git
$ git init
Initialized empty Git repository in /path/to/repo/.git/
  • 在仓库中初始化 DVC
$ dvc init
Initialized DVC repository.

You can now commit the changes to git.
1 https://dvc.org/doc/command-reference/init
DVC 数据版本管理入门

DVC 隐藏文件

  • 初始化会创建需用 Git 跟踪的内部文件
$ git status
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   .dvc/.gitignore
    new file:   .dvc/config
    new file:   .dvcignore
  • 提交这些更改
$ git commit -m "initialized dvc"
DVC 数据版本管理入门

.dvcignore 文件

  • 类似于 .gitignore 文件

    • 语法相同
    • 指定 DVC 忽略的文件/目录
  • 适用于大量无关数据文件

    • 提升 DVC 操作速度
1 https://dvc.org/doc/user-guide/project-structure/dvcignore-files 2 https://git-scm.com/docs/gitignore
DVC 数据版本管理入门

示例

# .dvcignore
# 忽略 data 目录下的所有文件
data/*

# 但不忽略 'data/data.csv' !data/data.csv
# 忽略所有 .tmp 文件 *.tmp
1 https://dvc.org/doc/user-guide/project-structure/dvcignore-files
DVC 数据版本管理入门

检查被忽略的文件

  • 使用命令 dvc check-ignore
$ dvc check-ignore data/file.txt
data/file.txt
  • 搭配 -d 查看详情
$ dvc check-ignore -d data/file.txt
.dvcignore:3:data/*    data/file.txt
1 https://dvc.org/doc/command-reference/check-ignore
DVC 数据版本管理入门

小结

  • pip install dvc 安装 DVC
  • 验证 DVC 的版本、平台等
    • dvc version
  • 在工作区初始化 DVC
    • dvc init
    • 先初始化 Git
  • 使用 .dvcignore 指定排除项
    • .gitignore 类似,语法相同
    • 检查某文件是否被排除
      • dvc check-ignore <filename>
DVC 数据版本管理入门

让我们来练习!

DVC 数据版本管理入门

Preparing Video For Download...