Sub topic 8
Tracking Parameters and Metrics
Assume you know basic Git, can run commands in a terminal, and understand YAML. You should also have a DVC project with a training stage, such as the pipeline from the previous chapter.
A parameter is an input that controls how a stage runs. Examples include the number of trees in a random forest or the maximum depth of a decision tree. A metric is a measured result, such as accuracy or root mean squared error.
DVC connects both to a pipeline stage. When a tracked parameter changes, DVC knows that the stage may produce different results. When the stage finishes, DVC records its metrics so that you can compare runs.
Declaring parameters
The default parameter file is params.yaml:
train:
n_estimators: 100
max_depth: 5The training stage selects 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: falseThe entries under params are parameter paths. train.n_estimators means the n_estimators value inside the train section. DVC includes these values when it calculates whether the stage is up to date.
Your training script must read the same values. For example, a Python script might load them with yaml.safe_load and pass them to the model. Listing a parameter in dvc.yaml does not automatically change a model that ignores that parameter.
Recording and comparing metrics
The training script can write a JSON file such as:
{
"accuracy": 0.91,
"validation_loss": 0.24
}Run the pipeline and commit its result:
dvc repro
dvc metrics show
git add dvc.yaml dvc.lock params.yaml metrics.json
git commit -m "Train baseline model"dvc repro runs the stage if its code, dependencies, parameters, or outputs changed. dvc metrics show displays the current values from the metrics file.
Change a parameter and run the pipeline again:
dvc exp run -S train.n_estimators=200
dvc exp show
dvc metrics diffThe -S option overrides a parameter for this experiment without requiring you to edit params.yaml first. DVC stores the experiment's metadata, including parameter values and metrics. dvc exp show lists experiments, while dvc metrics diff compares the current workspace with a Git revision or another reference.
| Item | Purpose | Example |
|---|---|---|
| Parameter | Controls a stage's input | `train.max_depth: 5` |
| Metric | Measures a stage's result | `accuracy: 0.91` |
| Output | File produced by a stage | `models/model.pkl` |
The working model
The relationship between inputs and results looks like this:
DVC records the exact parameter values and dependency hashes in dvc.lock. Git records the project files and pointer files. Together, they let you identify which data, code, parameters, and metrics produced a model.
Common errors
- Changing
params.yamlbut not listing the key indvc.yaml: DVC may not detect the change as a dependency of the stage. - Listing a parameter that the script does not read: The pipeline reruns, but the model may remain unchanged.
- Overwriting metrics without recording the run: Save or commit a useful baseline before starting many experiments.
- Comparing different evaluation sets: A higher score is not meaningful if the test data changed.
- Caching metrics unintentionally: Metrics are usually small, so setting
cache: falsekeeps them as ordinary workspace files that Git can commit.
Exercise
You change train.max_depth in params.yaml, but dvc repro says the pipeline is unchanged. What should you check?
Show answer
Check that train.max_depth appears under the stage's params section and that the training script actually reads that parameter.