Sub topic 3
Tracking parameters and metrics
This chapter assumes you have a DVC project with a working dvc.yaml pipeline, a remote configured, and basic Git tags. You should also be comfortable reading YAML and running dvc repro.
Tracking parameters and metrics
Parameters are the settings a training run reads, such as train.max_depth. Metrics are the numbers a run produces, such as accuracy. DVC stores parameters in params.yaml and lists them under a stage's params: key. Metrics go in a file such as metrics.json, and the stage marks that file under metrics:.
# params.yaml
train:
n_estimators: 100
max_depth: 5Compare two runs with dvc metrics diff. It shows the change in each metric between your working copy and the last commit.
Running and comparing experiments
dvc exp run runs a stage with a parameter override and saves the result as an experiment, without creating a Git commit.
dvc exp run -S train.max_depth=8
dvc exp showdvc exp show lists each experiment with its parameters and metrics in one table. Keep an experiment you want by running dvc exp apply on it, then committing.
Versioning trained models
Declare the model file as an output of the training stage. Then commit dvc.lock and tag the release, so Git records which model hash belongs to which version.
dvc repro
git add dvc.yaml dvc.lock params.yaml metrics.json
git commit -m "Train model with max_depth 5"
git tag v1.0Reusing data across repositories
dvc get downloads a file from another DVC repository. dvc import does the same and also records the source, so dvc update can refresh the copy later.
dvc get https://github.com/org/data-repo data/raw.csv
dvc import https://github.com/org/data-repo data/raw.csvWorking in a team and in CI
Teammates and CI jobs run git clone, then dvc pull to fetch data from the remote, then dvc repro. Store remote credentials in the CI secret store, never in Git.
Managing storage with dvc gc
Old cached versions accumulate over time. dvc gc removes cache entries that no version refers to.
> Ask yourself before running it: dvc gc --workspace keeps only data used by the current workspace. Push anything you need to keep to the remote first, because removed cache entries cannot be recovered locally.
Note: the block above uses a callout, written as shown below.
Exercise
Which command reruns only the stages whose inputs changed, and which command compares metrics between your workspace and the last commit?
Show answer
dvc repro reruns changed stages. dvc metrics diff compares metrics against the last commit.