Sub topic 9
Running and Comparing Experiments with DVC
This chapter assumes that you know basic Git commands, can edit YAML, and already have a DVC pipeline with a training stage. The training stage should read parameters from params.yaml and write metrics such as accuracy or loss to a metrics file.
An experiment is one model run with a particular set of inputs and parameters. DVC experiments let you try parameter changes without creating a permanent Git commit for every run. You can then compare the results and keep the best version.
Prepare a tracked baseline
Start with a clean committed pipeline. A baseline is the reference run against which you compare later experiments.
dvc repro
git add dvc.yaml dvc.lock params.yaml metrics.json
git commit -m "Record baseline training run"
git tag baselineThe dvc repro command runs stages whose dependencies changed. The dvc.lock file records the exact hashes of those dependencies and outputs. Committing the lock file makes the baseline reproducible.
Your parameter file might contain:
train:
n_estimators: 100
max_depth: 5The training stage must list the parameters it uses:
stages:
train:
cmd: python src/train.py data/prepared models/model.pkl
deps:
- src/train.py
- data/prepared
params:
- train.n_estimators
- train.max_depth
outs:
- models/model.pkl
metrics:
- metrics.json:
cache: falseWhen a listed parameter changes, DVC knows that the training stage may produce different results.
Run an experiment
Use dvc exp run to create a temporary experiment from the current workspace. The -S option sets a parameter for that run.
dvc exp run -S train.n_estimators=200 -S train.max_depth=8DVC runs the affected stages and stores the experiment's metadata. The experiment is associated with a special Git reference, but your main branch does not gain a new commit. This keeps exploratory work separate from the stable project history.
You can override several values in one command:
dvc exp run \
-S train.n_estimators=300 \
-S train.max_depth=10The command can also use a parameter file or a parameter set prepared in advance. Keep the parameter names consistent with params.yaml and dvc.yaml.
View and compare results
List experiments with dvc exp show.
dvc exp showThe output includes each experiment's name, parameter values, and metrics. DVC normally displays the workspace and recent experiments in a table. Use this view to identify promising combinations before applying one.
To compare the current workspace or a commit with another revision, use dvc metrics diff.
dvc metrics diff baselineA metrics comparison may look conceptually like this:
| Revision | Accuracy | Loss |
|---|---|---|
| baseline | 0.86 | 0.42 |
| experiment | 0.89 | 0.35 |
The meaning of an improvement depends on the metric. Higher accuracy is usually better, while lower loss is usually better. Do not select a model from one metric without checking validation design, data leakage, and performance on a separate test set.
Keep the winning experiment
After finding a good experiment, inspect it before making it permanent. Apply it to the workspace with its experiment name.
dvc exp apply exp-abc123
dvc repro
git diffThe exact experiment name appears in the output of dvc exp show. Applying an experiment updates tracked files such as params.yaml, dvc.lock, and generated outputs. Review the changes, run validation, and commit them:
git add params.yaml dvc.yaml dvc.lock metrics.json
git commit -m "Adopt tuned training parameters"If an experiment is not useful, leave it un-applied or remove it with the DVC experiment management commands supported by your installed version. The remote data cache remains separate from the experiment record, so clean up storage deliberately rather than deleting files by hand.
Exercise
Why does dvc exp run -S train.max_depth=8 avoid creating a normal Git commit?
Show answer
It records the parameter change and resulting outputs in a temporary DVC experiment reference, allowing comparison before the change is committed to the main project history.