Sub topic 10
Versioning trained models
Prerequisites and the versioning problem
Assume you know basic Git commands, DVC commands, YAML, and how a Python training script creates a model file. A trained model is an artifact: a file produced by a computation. For example, a scikit-learn model might be saved as models/model.pkl.
Git can store this file, but large binary files make repositories slow and difficult to clone. Git also does not record the dataset, parameters, code, and environment that produced the model in one connected workflow. DVC, or Data Version Control, adds that connection.
DVC stores the model content in its cache and records a content hash in a small metadata file or pipeline definition. Git versions the metadata. Together, a Git commit and the DVC-tracked artifacts identify one reproducible model version.
| Item | What it records |
|---|---|
| Git commit | Code, pipeline definitions, parameters, and DVC metadata |
| DVC cache or remote | The actual model file |
| `metrics.json` | Evaluation results for the model |
| `dvc.lock` | Exact hashes of inputs and outputs used by a pipeline run |
Make the model a pipeline output
The clearest approach is to make the training stage write the model to a path listed under outs. DVC then knows that the model belongs to that stage.
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: falseWhen you run dvc repro, DVC executes the training command when an input has changed. It records the resulting model hash in dvc.lock. A later run can reuse the stage when its dependencies and parameters are unchanged.
Commit the pipeline definition, lock file, parameters, and metrics to Git:
dvc repro
git add dvc.yaml dvc.lock params.yaml metrics.json
git commit -m "Train baseline model"
git tag model-v1
dvc pushThe Git tag gives the model version a human-readable name. The tag itself does not contain the model bytes. dvc push uploads those bytes to the configured DVC remote.
Restore and compare model versions
To inspect an older model, switch Git to the commit or tag that describes it. Then ask DVC to update workspace files to match that metadata.
git checkout model-v1
dvc checkoutIf the model is not in the local cache, download it first:
dvc pull
dvc checkoutgit checkout changes the pointer files and pipeline metadata. dvc checkout materializes the corresponding model file in the workspace. Without the second command, your working directory may still contain the newer model.
A model file alone is usually insufficient for reliable comparison. Record metrics such as accuracy, F1 score, or validation loss in a small JSON file. Because metrics are text, setting cache: false keeps them directly available to Git. Compare the current commit with an older one using:
dvc metrics diff model-v1Common errors and trade-offs
Do not overwrite a model and assume Git can identify its origin. Run the pipeline, commit the changed DVC metadata, and push the new artifact. Do not commit large model files directly unless the repository policy explicitly supports it.
A model can be reproducible in DVC while still producing different results if the training environment is different. Pin dependencies and record important environment details separately. Also remember that DVC tracks file content, not whether the model is safe, fair, or useful in production. Model validation remains your responsibility.
Exercise
After checking out an older Git tag, the workspace still contains the newer model. What command should you run, and what does it do?
Show answer
Run dvc checkout. It reads the DVC metadata selected by Git and restores the matching model from the local cache. Run dvc pull first if that version is not cached locally.