Sub topic 13
Tracking Experiments
This chapter assumes basic Python, command-line, Git, and model-training knowledge. You should also know how to load a configuration file and calculate validation metrics. The goal is to record enough information to explain why one training run differs from another.
What an experiment record contains
An experiment is one complete training run with a defined input, configuration, code version, and output. Tracking experiments means saving these details together:
- Parameters, such as learning rate, batch size, and model depth.
- Metrics, such as validation loss, accuracy, or F1 score.
- Dataset identity, such as a version, checksum, or pipeline output.
- Code version, usually a Git commit.
- Environment information, such as the Python and library versions.
- Artifact locations, such as the saved model and evaluation report.
A metric without its parameters is difficult to interpret. A model score without the dataset version may be impossible to reproduce.
A small tracking record
For a small project, a newline-delimited JSON file can be enough. Each line represents one run. Larger teams often use a tracking service such as MLflow, but the data model is the same.
import json
from datetime import datetime, timezone
from pathlib import Path
def record_run(path, *, run_id, params, metrics, code_commit, data_version, artifacts):
record = {
"run_id": run_id,
"started_at": datetime.now(timezone.utc).isoformat(),
"params": params,
"metrics": metrics,
"code_commit": code_commit,
"data_version": data_version,
"artifacts": artifacts,
}
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as file:
file.write(json.dumps(record) + "\n")
record_run(
"runs/history.jsonl",
run_id="2026-11-30-baseline",
params={"learning_rate": 0.001, "batch_size": 32},
metrics={"validation_accuracy": 0.87},
code_commit="a13f9c2",
data_version="processed-2026-11-29",
artifacts={"model": "models/2026-11-30-baseline.pkl"},
)The run_id should be unique and stable. Include a human-readable label, but do not use the label as the only identity. A timestamp, generated identifier, or commit-based identifier prevents accidental overwrites.
Comparing runs safely
Compare runs only when their evaluation procedure is consistent. Use the same validation split, metric definition, and preprocessing steps. A higher score can be misleading if one run used a different dataset or accidentally included validation data during training.
Useful comparisons include:
| Question | Fields to compare |
|---|---|
| Did the hyperparameter help? | Parameters and validation metrics |
| Did the data change the result? | Dataset version and metrics |
| Can the result be rebuilt? | Code commit, environment, and artifacts |
Common errors include logging metrics without units, replacing records instead of appending them, and saving a model without its preprocessing configuration. Keep credentials and private data out of experiment logs.
Exercise
A run achieved higher accuracy after changing both the learning rate and the dataset. Can you claim that the learning rate caused the improvement?
Show answer
No. Change one factor at a time, or run a controlled experiment that records both dataset versions and compares several runs.