Saltar al contenido

Lección 16 · Experiments

Run DVC Experiments with dvc exp run

Run DVC experiments without cluttering Git history: change parameters with set-param, name runs, use temp mode, save manual results and know what gets stored.

Machine learning work is trial and error. You try a new setting, look at the score and try another. Making a Git commit and branch for every attempt quickly becomes messy. DVC experiments record each attempt so you can compare them and keep only the good ones.

Your first experiment

Experiments work with a pipeline in dvc.yaml. Then:

Terminal
# Read-only: run with a changed parameter
dvc exp run -S train.max_iter=400
dvc exp show

dvc exp run reproduces the pipeline like dvc repro and then saves the result as an experiment. -S (--set-param) changes values in params.yaml before the run. You can repeat -S for several values, and target another file with a prefix:

Terminal
# Read-only: several overrides
dvc exp run -S train.max_iter=400 -S train.model=forest
dvc exp run -S config/eval.yaml:threshold=0.6

-S uses the basic override syntax of the Hydra library, which also lets you append keys with + and remove them with ~.

Names

DVC gives each experiment a generated name. Give meaningful ones with -n:

Terminal
# Read-only: a named run
dvc exp run -n longer-training -S train.max_iter=800

Inside the running stage, the DVC_EXP_NAME environment variable holds the name.

What is saved

DVC records the experiment's files, including code, parameter values, metrics and DVC-tracked data, and stores the experiment internally without adding commits to your branch history. You work with experiments through dvc exp commands, not by managing those internal records yourself.

One important rule: only files tracked by Git or DVC are saved. A new script that you never added to Git may not be restorable. Run git add on new code files before you experiment.

After a run, your workspace contains the results of that run. To return to the committed baseline, restore files with Git and then dvc checkout, or apply another experiment.

Run without blocking your workspace

dvc exp run --temp runs the experiment in a separate copy of the project under .dvc/tmp/exps. You can keep editing meanwhile. The copy shares the project cache, and files ignored by Git are not copied. If a needed file is untracked, stage it with git add before the run.

Save manual results

Sometimes work happens outside a pipeline, for example in a notebook. dvc exp save records the current workspace as an experiment without running anything:

Terminal
# Read-only: snapshot after manual work
dvc exp save -n notebook-baseline

Saving does not prove that the outputs match the code. Prefer a pipeline run when you want reproducibility.

How experiments relate to commits

flowchart TD
  B["Baseline commit"] --> E1["exp: max_iter 400"]
  B --> E2["exp: forest model"]
  B --> E3["exp: longer training"]
  E2 --> K["Keep: apply and commit"]

Experiments start from a baseline commit; you promote only the ones you want.

Try the idea

This stand-in runs a tiny "pipeline" for several parameter overrides and keeps a log, without touching a baseline. The score function is invented for the demonstration; it is not a real model.

Python Stand-in: a tiny experiment log

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 experiment habits

  • Commit a working baseline first, so every experiment has a clear starting point.
  • Change one thing at a time when you want to learn what caused a change.
  • Fix seeds, or run several, so you can separate signal from noise.
  • Name the important runs and delete failed junk.
  • Write down your hypothesis in a short note, not only the winning score.

Experiments are local until you share them (next lesson). They do not replace your team's notes about decisions.

Common mistake: creating a new script and running experiments before adding it to Git. The file may not be part of the experiment, so others cannot reproduce it. Stage new files first.

Experiments versus branches

Branches suit long-lived work that people review: a feature or a major change. Experiments suit many quick trials, especially parameter changes. When an experiment becomes important, turn it into a branch or commit. The next lesson shows how.

Plan a small study

Suppose you wonder whether more iterations help. A tidy mini study looks like this:

  1. Commit the baseline and record its metrics.
  2. Write your question: "Does max_iter above 200 improve validation F1?"
  3. Run three named experiments with 400, 600 and 800.
  4. Compare them with dvc exp show --only-changed.
  5. Decide, and write one sentence about why.

Because only one parameter changes, the table answers your question directly. If you had changed the model and the data at the same time, you could not say which one mattered.

Where experiments can surprise you

If a stage's output looks identical for two runs, DVC may restore results from the run cache instead of computing them again. That is usually helpful, but if your code uses random numbers without a fixed seed, you might expect a fresh result. In that case force a rerun, or better, make the randomness a parameter. A seed parameter turns "run it again" into a named, comparable experiment.

Keep a lab notebook

DVC records what ran, but not why. Add a short log in your repository or ticket: the question, the experiments you ran, the result and the decision. Three lines are enough. Weeks later, the note explains why you rejected an idea, so that nobody spends a day testing it again. It also helps you notice when you have been tuning in circles without a clear question. Review the note with a teammate before you start the next round of runs.

Before you leave for the day, check which results your workspace currently holds, so you do not commit a trial by accident.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does -S do in dvc exp run -S train.max_iter=400?

    -S, or --set-param, changes parameter values for the experiment.
  2. 2. Which files can an experiment restore?

    Add new code to Git before experimenting.
  3. 3. When is --temp useful?

    It runs in .dvc/tmp/exps, apart from your workspace.
  4. 4. What does dvc exp save do?

    It is useful after manual or notebook work, but proves less than a pipeline run.

Summary

  • dvc exp run reproduces the pipeline and records the result without new commits.
  • -S overrides parameters; -n names the experiment.
  • Only Git- and DVC-tracked files are saved.
  • --temp isolates a run; dvc exp save snapshots manual work.
  • Start from a committed baseline and change one thing at a time.

Next: Compare, Apply and Share Experiments

Tu progreso se guarda en este navegador. No necesitas una cuenta.