Lompat ke konten

Pelajaran 25 ยท Projects and Reference

Project: A Reproducible Training Pipeline

Build a DVC pipeline with prepare, train and evaluate stages, parameters and metrics, run experiments, pick a winner and add checks you can use in CI.

Now combine everything: a three-stage pipeline that predicts whether a support ticket is "slow" to resolve. The model is deliberately tiny so you can focus on DVC. It uses invented data and simple Python.

The plan

flowchart LR
  raw["data/raw.csv"] --> prepare
  prepare --> train
  prepare --> evaluate
  train --> evaluate
  evaluate --> m["reports/metrics.json"]

Prepare splits the data, train learns one rule per topic, evaluate scores held-out rows.

Parameters and pipeline

YAML
# params.yaml
prepare:
  seed: 7
  test_share: 0.25
  slow_minutes: 120
train:
  min_count: 1
YAML
# dvc.yaml
stages:
  prepare:
    cmd: python src/prepare.py
    deps: [src/prepare.py, data/raw.csv]
    params: [prepare]
    outs: [data/split]
  train:
    cmd: python src/train.py
    deps: [src/train.py, data/split]
    params: [train]
    outs: [models/model.json]
  evaluate:
    cmd: python src/evaluate.py
    deps: [src/evaluate.py, data/split, models/model.json]
    outs:
      - reports/metrics.json:
          cache: false

metrics:
  - reports/metrics.json

Your scripts need PyYAML to read params.yaml (pip install pyyaml). prepare labels a row as slow when its minutes exceed slow_minutes, and splits the rows. Every value a script reads is listed in that stage's params. Honest lists matter.

The logic, runnable

Here is the heart of all three scripts as one stand-in program. It is plain Python, not DVC: it shows what each stage computes. The "model" predicts, for each topic, whether tickets are usually slow.

Python Stand-in: prepare, train and evaluate in one file

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.

Notice the majority baseline: the score you get by guessing the more common class in the test set every time. A model that cannot beat it adds little. Also note that changing slow_minutes changes the meaning of the label, so accuracies from different thresholds are not directly comparable. That is a real lesson for experiment tables.

Build it for real

  1. Create a repository and run dvc init.
  2. Write data/raw.csv with a small script (based on make_raw), then run dvc add data/raw.csv.
  3. Split the stand-in into src/prepare.py, src/train.py and src/evaluate.py. Each reads params.yaml if it needs settings, reads its inputs and writes its declared outputs. Create output folders in the script.
  4. Add dvc.yaml and params.yaml as above, then run dvc repro.
  5. Commit dvc.yaml, dvc.lock, params.yaml, code, .dvc files and metrics. Add a remote and run dvc push.
Terminal
# Read-only: run and inspect
dvc dag
dvc repro
dvc metrics show
dvc repro          # nothing changed, so nothing should run

Run experiments

Terminal
# Read-only: try settings that keep the label meaning fixed
dvc exp run -S train.min_count=5
dvc exp run --queue -S 'train.min_count=1,10,50'
dvc queue start
dvc exp show --only-changed

Keep slow_minutes fixed while comparing, because it defines the target. With only three topics, min_count may barely change the score; noticing that a setting has no effect is a useful result too. Add your own features and parameters, then apply a chosen run with dvc exp apply and commit.

Add checks

Write a CI job or local script that:

  1. runs dvc data status --not-in-remote;
  2. runs dvc repro --dry and looks for stages that would run;
  3. compares metrics with dvc metrics diff main --md;
  4. fails if accuracy drops by more than an agreed amount, or if the model does not beat the majority baseline.

See Run DVC in CI for the workflow file.

Acceptance checklist

  • Running dvc repro twice runs nothing the second time.
  • Changing train.min_count makes train rerun, and prepare is skipped. evaluate reruns only if the model file changes.
  • Editing src/evaluate.py reruns only evaluate.
  • A fresh clone plus dvc pull gives the same outputs.
  • The README explains the label definition and the data origin.

Extensions

  • Write accuracy per topic to a CSV and add a plots entry for it.
  • Use foreach to train one model per region.
  • Add a validation split and keep the test set for the final report.

Common mistake: choosing the best experiment by test accuracy over and over. The test score then stops being an honest estimate. Select on validation data and use the test set once at the end.

Read your results critically

When the pipeline runs, look beyond the accuracy number. The model learns one rule per topic, so its ceiling is set by how much the topic alone says about resolution time. With only three topics, many settings of min_count produce the same model. That is why the metrics may not move at all, and why a flat result should send you to the features, not to more tuning.

Ask these questions after each run:

  • How far above the majority baseline is the score?
  • How many test rows are there, and how much would one row change the score?
  • Does the model fail on one topic in particular?
  • Would a different split seed change the conclusion?

Try the last one as an experiment: vary prepare.seed over a few values and compare. If accuracy swings by several points between seeds, small differences between model settings are not meaningful. This is one of the most useful habits you can take from the project, and DVC makes it cheap because every run is recorded.

Explain your pipeline

Finish by writing five or six lines in the README: what the label means, where the data comes from, what each stage does, which parameters matter, and how to reproduce the result. A teammate who can follow those lines from a clean clone has proof that your project is reproducible.

Share the finished project

When the checklist passes, tag the commit, push the data and the tag, and ask someone else to reproduce it. Ask them to note every step that was unclear. Each note is a small fix to your README or your pipeline, and the fixes make the next project start faster.

Keep the finished repository as a template. Replace the invented data and the tiny model with your own, and keep the same stage boundaries until you have a reason to change them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does prepare track its parameter group?

    A stage must declare every value its script reads.
  2. 2. You change only train.min_count. What happens to prepare?

    Only stages that track the changed value, and their affected descendants, rerun.
  3. 3. What is the majority baseline for?

    If a model does no better than always guessing the common class, it is not helping.
  4. 4. Why not compare accuracy across different slow_minutes values directly?

    Different thresholds define different targets.

Summary

  • A three-stage pipeline with parameters and metrics is reproducible and testable.
  • Declare each value a script reads, so reruns are correct.
  • Compare experiments only when the target and test data match.
  • Beat a simple baseline before trusting a model.
  • Checks for pushed data and metric drops belong in CI.

Next: DVC Glossary and Cheat Sheet

Progres Anda disimpan di browser ini. Tidak perlu akun.