Skip to content

Lesson 13 ยท Parameters, Metrics and Plots

DVC Parameters and params.yaml

Store settings in params.yaml, declare them as stage parameters so DVC reruns only when needed, read them in Python and compare values with dvc params diff.

A model's behavior depends on settings: a learning rate, a maximum depth, a date cutoff. If those numbers are buried in code, you cannot see which values produced a result. DVC parameters move them into a file and make them part of the pipeline's state.

A parameters file

By default DVC reads params.yaml in the project root.

YAML
# params.yaml
prepare:
  min_length: 5
  test_share: 0.2
  seed: 42
train:
  model: logistic
  max_iter: 200

JSON, TOML and Python files can also hold parameters. YAML is the common choice because it supports comments and nesting.

Declare which stage uses what

YAML
stages:
  prepare:
    cmd: python src/prepare.py
    deps:
      - src/prepare.py
      - data/raw.csv
    params:
      - prepare.min_length
      - prepare.test_share
      - prepare.seed
    outs:
      - data/split/
  train:
    cmd: python src/train.py
    deps:
      - src/train.py
      - data/split/
    params:
      - train
    outs:
      - models/model.pkl

Listing train tracks the whole group; listing prepare.seed tracks one key. For another file, use a nested form:

YAML
    params:
      - config/eval.yaml:
          - threshold

You can also track every key in a file by naming the file with no key list (- config/eval.yaml:). From the command line, use -p, for example dvc stage add -p prepare.seed,train.max_iter ....

Why list parameters instead of adding params.yaml to deps? With parameters, changing train.max_iter affects only stages that track it. If the whole file were a dependency, any edit would rerun every stage that lists it.

Read the values in Python

DVC does not inject values into your script. Your code reads the same file. With PyYAML installed:

Python
# Read-only: load parameters in a real project
import yaml

with open("params.yaml") as f:
    params = yaml.safe_load(f)["train"]
print(params["max_iter"])

Use safe_load, which does not build arbitrary Python objects. dvc.api.params_show() is another way to read parameters from Python. Alternatively, pass values as command-line arguments through templating, as in the previous chapter.

Common mistake: listing a parameter in dvc.yaml while the script still has the number hardcoded. DVC reruns the stage when you edit the YAML, but nothing changes in the result. Make sure the code really reads the value.

Compare parameters

Terminal
# Read-only: what changed in settings?
dvc params diff                 # workspace versus last commit
dvc params diff v1.0 HEAD       # between two revisions

The output is a table of changed keys with old and new values, which helps explain why a score moved.

Try the idea

This stand-in flattens nested settings into dotted keys, then decides which stages a change affects. Real DVC does this using the declarations in dvc.yaml and the values in dvc.lock.

Python Stand-in: which stages does a parameter change affect?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good parameter habits

  • Name values by meaning, such as test_share, not value2.
  • Include seeds so runs can repeat.
  • Group by stage, which keeps lists in dvc.yaml short.
  • Never put secrets here. Parameters are committed to Git and shown in comparisons and reports. Use environment variables or a secret store for keys.
  • Avoid huge values. Parameters are settings, not data.
  • Validate values in the script and fail early with a clear message.

Parameters versus other inputs

Item Put it in
Learning rate, thresholds, seeds params.yaml
Training data DVC-tracked files or stage outputs
Code deps of the stage
API keys Environment or secret manager
Library versions A lock file such as requirements.txt, listed as a dependency if changes should rerun stages

Parameters also power experiments: you can override them from the command line, so you do not edit the file for every trial. The experiments chapter shows how.

A worked example

Suppose your scores fell after a merge, and you want to know whether settings changed. Run:

Terminal
# Read-only: compare settings with the main branch
dvc params diff main

An illustrative result might show that train.max_iter moved from 400 to 200 and prepare.min_length from 5 to 3. Now you have two candidates to test. Change one back, run an experiment, and compare the metrics. Then try the other. This is much faster than reading through every commit.

Defaults and documentation

Comments in params.yaml are the cheapest documentation you will ever write. Explain the unit and the allowed range:

YAML
prepare:
  min_length: 5      # characters; reviews shorter than this are dropped
  test_share: 0.2    # between 0 and 1

Avoid hidden defaults in code. If your script uses params.get("max_iter", 100), a missing key silently gives 100, and the pipeline will not tell you. It is safer to read required keys directly, so a typo fails at once. Check ranges early too: a test_share of 20 instead of 0.2 should stop the stage with a clear error.

Renaming parameters

When you rename a key, update the code, dvc.yaml and params.yaml together in one commit. Old experiments and commits keep the old name, so comparisons across the rename show one key disappearing and another appearing. Mention the rename in the commit message so that nobody mistakes it for a real change of value.

Parameters and reviews

A change in params.yaml is small in a diff but can change results completely, so review it with the same care as code. A good pull request says why a value changed and shows the effect on metrics. For example: "Raised min_length from 3 to 5 because very short reviews were mostly noise; validation F1 changed as shown in the table."

If several people tune the same file, merge conflicts become common. Resolve them deliberately: do not simply keep your own value. Ask which value the other person tested, and rerun the pipeline after the merge so that the lock file and the metrics match the final settings. When in doubt, run both values as experiments and compare them under the same data.

It also helps to separate settings that change results from settings that only change convenience, such as the number of log messages. Only the first group needs to be tracked as parameters.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why list individual parameters instead of adding params.yaml to deps?

    Parameter tracking is finer than tracking the whole file.
  2. 2. You change train.max_iter but your script has max_iter hardcoded. What happens?

    DVC does not inject values. Your code must read them.
  3. 3. Which command compares parameter values between revisions?

    It reports changed keys and their values.
  4. 4. Which is the wrong place for an API key?

    Parameters are committed to Git and shown in comparisons.

Summary

  • params.yaml holds settings, and stages declare the keys they use.
  • DVC reruns a stage when its tracked values change.
  • Your code must read the values itself.
  • dvc params diff shows what changed between versions.
  • Keep secrets out of parameter files.

Next: Track Metrics with DVC

Your progress is saved in this browser. No account needed.