Ir para o conteúdo

Lição 17 · Experiments

Compare, Apply and Share DVC Experiments

Compare DVC experiments with exp show and exp diff, restore a winner with exp apply or exp branch, share runs with exp push and pull, and clean up old ones.

After a few runs you need answers: which one was best, what changed, and how do you keep it? This lesson covers comparing, promoting, sharing and cleaning up experiments.

The experiments table

Terminal
# Read-only: a table of experiments
dvc exp show
dvc exp show --only-changed
dvc exp show --sort-by accuracy --sort-order desc
dvc exp show --md

The table lists your workspace, baseline and experiments with their parameters and metrics. Useful flags:

Flag Effect
--only-changed Hide columns that never differ
--drop <regex> Remove matching columns
--keep <regex> Protect columns from removal
--sort-by, --sort-order Order experiments that share a parent commit
--md, --csv, --json Export for reports or scripts

dvc exp diff compares experiments and shows the parameter and metric differences between them.

Compare fairly

Before you pick a winner, check:

  1. Did all runs use the same data version and test split?
  2. Is the difference larger than normal seed-to-seed variation?
  3. Do other metrics, runtime or model size get worse?
  4. Did you choose using validation data, keeping the test set for a final check?

If you try hundreds of settings, one may look good by chance. The more you search, the more careful you should be.

Promote a winner

To bring an experiment into your workspace:

Terminal
# Read-only: keep the best run
dvc exp apply forest-longer
git status
git diff                       # review what changed
git add params.yaml dvc.lock reports/
git commit -m "Use forest model with 600 iterations"
dvc push

dvc exp apply restores the experiment's files, including parameters, metrics and tracked outputs. Conflicting workspace changes are overwritten, so commit or stash your work first. Look at git status and add the files that changed.

Alternatively, create a branch from an experiment, which suits a pull request:

Terminal
# Read-only: turn an experiment into a branch
dvc exp branch forest-longer try/forest
git push origin try/forest
dvc push

Share experiments

Experiments are stored locally. To share them:

Terminal
# Read-only: share with your team
dvc exp push origin forest-longer
dvc exp list origin
dvc exp pull origin forest-longer

dvc exp push sends the experiment to the Git remote and also uploads the cached data to your DVC remote, unless you add --no-cache. dvc exp list origin shows experiments for your current commit by default; add -A for all commits. A setting named exp.auto_push can push automatically after dvc exp run or dvc exp save.

Pushing experiments is a form of publishing: check that parameters, plots and data do not include secrets or restricted information.

Clean up

Terminal
# Read-only: remove experiments
dvc exp remove old-failed-run
dvc exp remove --all
dvc exp remove -g origin old-failed-run

-g removes from a Git remote. The --rev and -n flags select experiments based on commits, and --keep inverts that selection. Removing experiments does not by itself delete their cached data. Storage is reclaimed with dvc gc, discussed in the troubleshooting chapter.

Try the idea

This stand-in ranks experiments by a validation score but rejects runs that break a rule. The numbers are invented.

Python Stand-in: choose a winner with rules

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.

The run with the highest score is not the winner here, because it used different data. Fair comparison comes first.

Common mistake: running dvc exp apply with uncommitted work in the same files. Conflicting changes are overwritten. Commit or stash first, then apply.

After you merge

When a winning experiment is committed, update your notes: what you changed, why, and which data version you used. Then push the data and delete experiments you no longer need so the table stays useful. A tag on the commit makes the released model easy to find.

Turn a table into a decision

A table of numbers does not decide anything by itself. Write the rule before you look at the results, for example: "Choose the highest validation F1 among runs under 100 MB, using data version v3, unless the improvement over baseline is below 0.005." A rule written in advance stops you from moving the goalposts after seeing an attractive number.

Then record the decision in the pull request:

  • the experiment name and the baseline commit;
  • the main metrics, with the difference from baseline;
  • other costs you considered, such as model size or training time;
  • what you did not test.

The last line is easy to skip and very useful later. It tells a future teammate which ideas are still open.

Reproduce the winner

Before you ship a winning run, reproduce it from a clean state: check out the commit that contains the applied experiment, run dvc pull, and run dvc repro. If the pipeline reports everything is current and the outputs are present, the recorded state is complete. If you force a full rerun and the metrics change noticeably, look for randomness, unpinned libraries or an undeclared input.

Working with a teammate's experiment

When a colleague asks you to look at an experiment, pull it and read before you apply it. List what is available with dvc exp list origin, pull the one you need, and compare it with your baseline using dvc exp show and dvc exp diff. Check which parameters changed and whether the code changed too. A surprising score often has an ordinary explanation, such as a different split.

If you decide to apply it, do so on a clean branch. That keeps your own work separate and makes it easy to abandon the attempt. After applying, run dvc status and dvc repro --dry to confirm that the pipeline state is consistent. When something is missing, ask your colleague to push the data rather than recreating it yourself, so you both look at exactly the same files.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which command restores an experiment's results into your workspace?

    Apply restores the experiment's files. Review and commit afterward.
  2. 2. What does dvc exp push send by default?

    Use --no-cache to skip the data upload.
  3. 3. Which command creates a Git branch from an experiment?

    This is useful for pull requests.
  4. 4. Two runs used different data versions. How should you treat their scores?

    A fair comparison needs the same evaluation data.

Summary

  • dvc exp show and dvc exp diff compare parameters and metrics.
  • Check data versions and variation before choosing a winner.
  • Promote runs with dvc exp apply or dvc exp branch, then commit.
  • Share with dvc exp push and pull.
  • Remove old experiments, and reclaim storage separately.

Next: Queues, Grid Search and Hydra

Seu progresso fica salvo neste navegador. Não precisa de conta.